aGrUM  0.20.2
a C++ library for (probabilistic) graphical models
cast_unicode_inl.h File Reference

Copyright 2005-2020 Pierre-Henri WUILLEMIN() & Christophe GONZALES() info_at_agrum_dot_org. More...

#include <iostream>
#include <locale>
#include <sstream>
#include <string>
#include <agrum/tools/core/cast_unicode.h>
+ Include dependency graph for cast_unicode_inl.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

INLINE std::wstring widen (const std::string &str)
 Cast a std::string into a std::wstring. More...
 
INLINE std::string narrow (const std::wstring &str)
 Cast a std::wstring into a std::string. More...
 

Detailed Description

Copyright 2005-2020 Pierre-Henri WUILLEMIN() & Christophe GONZALES() info_at_agrum_dot_org.

This library is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with this library. If not, see http://www.gnu.org/licenses/.

Unicode helper functions.

Author
Christophe GONZALES() and Pierre-Henri WUILLEMIN()

Definition in file cast_unicode_inl.h.

Function Documentation

◆ narrow()

INLINE std::string narrow ( const std::wstring &  str)

Cast a std::wstring into a std::string.

Parameters
strThe std::wstring to cast.
Returns
Returns the std::string of str.

Definition at line 51 of file cast_unicode_inl.h.

51  {
52  std::ostringstream stm;
53  const std::ctype< char >& ctfacet
54  = std::use_facet< std::ctype< char > >(stm.getloc());
55 
56  for (std::size_t i = 0; i < str.size(); ++i) {
57  stm << ctfacet.narrow(str[i], 0);
58  }
59 
60  return stm.str();
61 }

◆ widen()

INLINE std::wstring widen ( const std::string &  str)

Cast a std::string into a std::wstring.

Parameters
strThe std::string to cast.
Returns
Returns the std::wstring of str.

Definition at line 39 of file cast_unicode_inl.h.

39  {
40  std::wostringstream wstm;
41  const std::ctype< wchar_t >& ctfacet
42  = std::use_facet< std::ctype< wchar_t > >(wstm.getloc());
43 
44  for (size_t i = 0; i < str.size(); ++i) {
45  wstm << ctfacet.widen(str[i]);
46  }
47 
48  return wstm.str();
49 }