aGrUM  0.16.0
cast_unicode_inl.h
Go to the documentation of this file.
1 
30 #ifndef CAST_UNICODE_H
31 #define CAST_UNICODE_H
32 
33 #include <iostream>
34 #include <locale>
35 #include <sstream>
36 #include <string>
37 
39 
40 INLINE std::wstring widen(const std::string& str) {
41  std::wostringstream wstm;
42  const std::ctype< wchar_t >& ctfacet =
43  std::use_facet< std::ctype< wchar_t > >(wstm.getloc());
44 
45  for (size_t i = 0; i < str.size(); ++i) {
46  wstm << ctfacet.widen(str[i]);
47  }
48 
49  return wstm.str();
50 }
51 
52 INLINE std::string narrow(const std::wstring& str) {
53  std::ostringstream stm;
54  const std::ctype< char >& ctfacet =
55  std::use_facet< std::ctype< char > >(stm.getloc());
56 
57  for (std::size_t i = 0; i < str.size(); ++i) {
58  stm << ctfacet.narrow(str[i], 0);
59  }
60 
61  return stm.str();
62 }
63 
64 #endif // CAST_UNICODE_H
INLINE std::string narrow(const std::wstring &str)
Cast a std::wstring into a std::string.
INLINE std::wstring widen(const std::string &str)
Cast a std::string into a std::wstring.
Copyright 2005-2019 Pierre-Henri WUILLEMIN et Christophe GONZALES (LIP6) {prenom.nom}_at_lip6.fr.