4.6.0
Freundlich's C++ toolkit
|
String-related functions and types.
In C++ there are two main character types: char
and wchar_t
. char
is always 1 byte long, the size of wchar_t
is implementation-defined. In Windows, it's 2, in Linux, it's often 4. Consequently, there are two types of character literals: narrow and wide literals, i.e.
Depending on the operating system, one of these character types is the "native" one. System functions like CreateFile
in Windows accept wchar_t
. In Linux, calls like open
accept char
. Along with the different types go different encodings. Windows uses UTF-16 for (wchar_t
) strings such as the file name given to CreateFile
. Linux uses whatever encoding the current locale specifies. On most systems, this is UTF-8 for char
strings.
When you're designing a library that frequently calls system functions, it's most efficient to use a string format that suits the current operating system. With that in mind, fcppt has a typedef called fcppt::char_type. This typedef is wchar_t
on Windows systems and char
on POSIX systems. It's configurable, however, so using wchar_t
on POSIX is possible, too (see the ENABLE_NARROW_STRING
CMake option). Based on that, there's fcppt::string which is a std::basic_string
of fcppt::char_type.
Unfortunately, making the character type configurable has some ramifications. For example, you cannot simply write:
This will break if fcppt::string is a wide string. To portably wrap literals, use the FCPPT_TEXT macro:
There are other standard library structures such as cout,clog,cin
which you cannot reliably use together with fcppt::string. fcppt provides alternatives for these (see the members of the string module in the documentation for a list of all related functions and structures).
The following functions can be used to convert from and to strings:
Function | Conversion description |
---|---|
fcppt::from_std_string | std::string → fcppt::string |
fcppt::from_std_wstring | std::wstring → fcppt::optional::object<fcppt::string> |
fcppt::to_std_string | fcppt::string → fcppt::optional::object<std::string> |
fcppt::to_std_wstring | fcppt::string → std::string |
Each of the above functions uses fcppt::string_conv_locale, which returns std::locale("")
. The semantics of this locale are described, for example, in the setlocale
manpage. Under Linux, this leads to the inspection of the locale environment variables (LC_ALL, LANG
etc.). The default locale for an application is the (rather unlocalized) "C" locale. We feel that using the "native" locale as the default makes more sense, since the C locale has no information about the system's native encoding. Converting std::string
to std::wstring
on a typical Linux machine means converting UTF-8 to UTF-32. The C locale, however, has no information about these encodings. If you need to use a different locale, you can use the locale-variants: fcppt::from_std_string_locale, fcppt::from_std_wstring_locale, fcppt::to_std_string_locale and fcppt::to_std_wstring_locale.
Finally, here is a small example:
To read in or write out files using the system's native character type, you can use the typedefs in the fcppt::io namespace, namely fcppt::filesystem::ifstream, fcppt::filesystem::ofstream , and so on.
There are also preprocessor macros, currently FCPPT_PP_STRINGIZE and FCPPT_PP_FILE to provide compatibility with fcppt::string. FCPPT_PP_STRINGIZE stringizes its argument to an fcppt::string literal. In the same fashion, FCPPT_PP_FILE adapts the standard macro FILE
to return an fcppt::string literal.
Lexical conversions deal with conversions from and to strings. In C++ this is done using operator<<
and operator>>
. fcppt wraps these into the following functions:
Function | Description |
---|---|
fcppt::extract_from_string | basic_string<CharT> → fcppt::optional::object<T> |
fcppt::output_to_std_string | T → std::string |
fcppt::output_to_std_wstring | T → std::wstring |
fcppt::output_to_fcppt_string | T → fcppt::string |
fcppt::output_to_string | T → basic_string<CharT> |
C++ offers standard exceptions in the <stdexcept>
header. All of them use std::string
which does not go well together with fcppt::string. fcppt instead defines its own exception base class: fcppt::exception. Although it derives from std::exception
, its what()
function returns a default message only. To get the error message of an fcppt::exception, use fcppt::exception::string.
Classes | |
class | fcppt::exception |
The base class for all exceptions. More... | |
Macros | |
#define | FCPPT_TEXT(x) |
A macro to wrap a narrow character literal so it results in a fcppt::char_type[] array. | |
Typedefs | |
using | fcppt::char_type = fcppt::detail::char_type |
The char_type used for text. | |
using | fcppt::io::istream = std::basic_istream<fcppt::char_type> |
Typedef to either std::istream or std::wistream, depending on fcppt::char_type. | |
using | fcppt::io::istringstream = std::basic_istringstream<fcppt::char_type> |
Typedef to either std::istringstream or std::wistringstream, depending on fcppt::char_type. | |
using | fcppt::io::ostream = std::basic_ostream<fcppt::char_type> |
Typedef to either std::ostream or std::wostream, depending on fcppt::char_type. | |
using | fcppt::io::ostringstream = std::basic_ostringstream<fcppt::char_type> |
Typedef to either std::ostringstream or std::wostringstream, depending on fcppt::char_type. | |
using | fcppt::io::stringstream = std::basic_stringstream<fcppt::char_type> |
Typedef to either std::stringstream or std::wstringstream, depending on fcppt::char_type. | |
using | fcppt::optional_std_string = fcppt::optional::object<std::string> |
An optional std string. | |
using | fcppt::optional_string = fcppt::optional::object<fcppt::string> |
An optional string. | |
using | fcppt::string = std::basic_string<fcppt::char_type> |
The string type used for text. | |
using | fcppt::string_view = std::basic_string_view<fcppt::char_type> |
The string view type used for text. | |
using | fcppt::filesystem::fstream = std::basic_fstream<fcppt::char_type> |
Typedef to basic_fstream , depending on fcppt::char_type. | |
using | fcppt::filesystem::ifstream = std::basic_ifstream<fcppt::char_type> |
Typedef to basic_ifstream , depending on fcppt::char_type. | |
using | fcppt::filesystem::ofstream = std::basic_ofstream<fcppt::char_type> |
Typedef to basic_ofstream , depending on fcppt::char_type. | |
Functions | |
FCPPT_DETAIL_SYMBOL fcppt::string | fcppt::error::strerrno () |
A wrapper around std::strerror(strerrno) | |
FCPPT_DETAIL_SYMBOL fcppt::string | fcppt::error::strerror (int errnum) |
A wrapper around std::strerror | |
FCPPT_DETAIL_SYMBOL fcppt::string | fcppt::error_code_to_string (std::error_code const &) |
Converts an error code to an fcppt::string. | |
template<typename Dest , fcppt::concepts::string Source> | |
fcppt::optional::object< Dest > | fcppt::extract_from_string (Source &&_source) |
Convert a string to a different type using fcppt::insert_extract_locale. | |
template<typename Dest , fcppt::concepts::string Source> | |
fcppt::optional::object< Dest > | fcppt::extract_from_string_fmt (Source &&_source, std::ios_base::fmtflags const _flags) |
Convert a string to a different type using fcppt::insert_extract_locale and format flags. | |
template<typename Dest , fcppt::concepts::string Source> | |
fcppt::optional::object< Dest > | fcppt::extract_from_string_locale_fmt (Source &&_source, std::locale const &_locale, fcppt::optional::object< std::ios_base::fmtflags > const _flags) |
Convert a string to a different type using a locale. | |
FCPPT_DETAIL_SYMBOL fcppt::string | fcppt::from_std_string (std::string_view) |
Convert from std::string to fcppt::string using fcppt::string_conv_locale. | |
FCPPT_DETAIL_SYMBOL fcppt::string | fcppt::from_std_string_locale (std::string_view, std::locale const &) |
Convert from std::string to fcppt::string using a custom locale. | |
FCPPT_DETAIL_SYMBOL fcppt::optional_string | fcppt::from_std_wstring (std::wstring_view) |
Convert from std::wstring to fcppt::string using fcppt::string_conv_locale. | |
FCPPT_DETAIL_SYMBOL fcppt::optional_string | fcppt::from_std_wstring_locale (std::wstring_view, std::locale const &) |
Convert from std::wstring to fcppt::string using a custom locale. | |
FCPPT_DETAIL_SYMBOL std::locale | fcppt::insert_extract_locale () |
Returns the default locale to use when converting from or to strings. | |
FCPPT_DETAIL_SYMBOL fcppt::io::ostream & | fcppt::io::cerr () |
Returns either std::cerr or std::wcerr, depending on fcppt::char_type. | |
FCPPT_DETAIL_SYMBOL fcppt::io::istream & | fcppt::io::cin () |
Returns either std::cin or std::wcin, depending on fcppt::char_type. | |
FCPPT_DETAIL_SYMBOL fcppt::io::ostream & | fcppt::io::clog () |
Returns either std::clog or std::wclog, depending on fcppt::char_type. | |
FCPPT_DETAIL_SYMBOL fcppt::io::ostream & | fcppt::io::cout () |
Returns either std::cout or std::wcout, depending on fcppt::char_type. | |
template<typename Ch , typename Traits > | |
fcppt::optional::object< std::basic_string< Ch, Traits > > | fcppt::io::stream_to_string (std::basic_istream< Ch, Traits > &_input) |
Reads the contents of a stream into a string. | |
FCPPT_DETAIL_SYMBOL fcppt::optional_std_string | fcppt::narrow (std::wstring_view _string) |
Converts a std::wstring to std::string, using fcppt::string_conv_locale. | |
FCPPT_DETAIL_SYMBOL fcppt::optional_std_string | fcppt::narrow_locale (std::wstring_view _string, std::locale const &_locale) |
Converts a std::wstring to std::string, using a locale. | |
template<typename Source > | |
fcppt::string | fcppt::output_to_fcppt_string (Source const &_source) |
Converts an arbitrary type to a fcppt::string, using fcppt::insert_extract_locale. | |
template<typename Source > | |
fcppt::string | fcppt::output_to_fcppt_string_locale (Source const &_source, std::locale const &_locale) |
Converts an arbitrary type to a fcppt::string, using a custom locale. | |
template<typename Source > | |
std::string | fcppt::output_to_std_string (Source const &_source) |
Convert an arbitrary type to a std::string, using fcppt::insert_extract_locale. | |
template<typename Source > | |
std::string | fcppt::output_to_std_string_locale (Source const &_source, std::locale const &_locale) |
Convert an arbitrary type to a std::string, using a custom locale. | |
template<typename Source > | |
std::wstring | fcppt::output_to_std_wstring (Source const &_source) |
Convert an arbitrary type to a std::wstring, using fcppt::insert_extract_locale. | |
template<typename Source > | |
std::wstring | fcppt::output_to_std_wstring_locale (Source const &_source, std::locale const &_locale) |
Convert an arbitrary type to a std::wstring, using a custom locale. | |
template<typename Dest , typename Source > | |
Dest | fcppt::output_to_string (Source const &_source) |
Convert an arbitrary type to a string, using fcppt::insert_extract_locale. | |
template<typename Dest , typename Source > | |
Dest | fcppt::output_to_string_locale (Source const &_source, std::locale const &_locale) |
Convert an arbitrary type to a string, using a custom locale. | |
FCPPT_DETAIL_SYMBOL std::locale | fcppt::string_conv_locale () |
Returns the default locale to use when converting between strings. | |
FCPPT_DETAIL_SYMBOL fcppt::optional::object< int > | fcppt::system (fcppt::string const &) |
Calls std::system . | |
FCPPT_DETAIL_SYMBOL fcppt::optional_std_string | fcppt::to_std_string (fcppt::string_view) |
Convert from fcppt::string to std::string using fcppt::string_conv_locale. | |
FCPPT_DETAIL_SYMBOL fcppt::optional_std_string | fcppt::to_std_string_locale (fcppt::string_view, std::locale const &) |
Convert from fcppt::string to std::string using a custom locale. | |
FCPPT_DETAIL_SYMBOL std::wstring | fcppt::to_std_wstring (fcppt::string_view) |
Convert from fcppt::string to std::wstring using fcppt::string_conv_locale. | |
FCPPT_DETAIL_SYMBOL std::wstring | fcppt::to_std_wstring_locale (fcppt::string_view, std::locale const &) |
Convert from fcppt::string to std::wstring using a custom locale. | |
FCPPT_DETAIL_SYMBOL std::wstring | fcppt::widen (std::string_view) |
Converts a std::string to std::wstring, using fcppt::string_conv_locale. | |
FCPPT_DETAIL_SYMBOL std::wstring | fcppt::widen_locale (std::string_view, std::locale const &) |
Converts a std::string to std::wstring , using a locale. | |
#define FCPPT_TEXT | ( | x | ) |
A macro to wrap a narrow character literal so it results in a fcppt::char_type[] array.
See fcppt.string for more information.
using fcppt::char_type = fcppt::detail::char_type |
The char_type used for text.
See fcppt.string for more information.
using fcppt::filesystem::fstream = std::basic_fstream<fcppt::char_type> |
Typedef to basic_fstream
, depending on fcppt::char_type.
using fcppt::filesystem::ifstream = std::basic_ifstream<fcppt::char_type> |
Typedef to basic_ifstream
, depending on fcppt::char_type.
using fcppt::io::istream = std::basic_istream<fcppt::char_type> |
Typedef to either std::istream or std::wistream, depending on fcppt::char_type.
See fcppt.string for a motivation for this.
using fcppt::io::istringstream = std::basic_istringstream<fcppt::char_type> |
Typedef to either std::istringstream or std::wistringstream, depending on fcppt::char_type.
See fcppt.string for a motivation for this.
using fcppt::filesystem::ofstream = std::basic_ofstream<fcppt::char_type> |
Typedef to basic_ofstream
, depending on fcppt::char_type.
using fcppt::optional_std_string = fcppt::optional::object<std::string> |
An optional std string.
An optional string.
using fcppt::io::ostream = std::basic_ostream<fcppt::char_type> |
Typedef to either std::ostream or std::wostream, depending on fcppt::char_type.
See fcppt.string for a motivation for this.
using fcppt::io::ostringstream = std::basic_ostringstream<fcppt::char_type> |
Typedef to either std::ostringstream or std::wostringstream, depending on fcppt::char_type.
See fcppt.string for a motivation for this.
using fcppt::string = std::basic_string<fcppt::char_type> |
The string type used for text.
See fcppt.string for more information about this type.
using fcppt::string_view = std::basic_string_view<fcppt::char_type> |
The string view type used for text.
See fcppt.string for more information about this type.
using fcppt::io::stringstream = std::basic_stringstream<fcppt::char_type> |
Typedef to either std::stringstream or std::wstringstream, depending on fcppt::char_type.
See fcppt.string for a motivation for this.
FCPPT_DETAIL_SYMBOL fcppt::io::ostream & fcppt::io::cerr | ( | ) |
Returns either std::cerr or std::wcerr, depending on fcppt::char_type.
See fcppt.string for a motivation for this.
FCPPT_DETAIL_SYMBOL fcppt::io::istream & fcppt::io::cin | ( | ) |
Returns either std::cin or std::wcin, depending on fcppt::char_type.
See fcppt.string for a motivation for this.
FCPPT_DETAIL_SYMBOL fcppt::io::ostream & fcppt::io::clog | ( | ) |
Returns either std::clog or std::wclog, depending on fcppt::char_type.
See fcppt.string for a motivation for this.
FCPPT_DETAIL_SYMBOL fcppt::io::ostream & fcppt::io::cout | ( | ) |
Returns either std::cout or std::wcout, depending on fcppt::char_type.
See fcppt.string for a motivation for this.
FCPPT_DETAIL_SYMBOL fcppt::string fcppt::error_code_to_string | ( | std::error_code const & | ) |
Converts an error code to an fcppt::string.
|
inline |
Convert a string to a different type using fcppt::insert_extract_locale.
Dest | The destination type, has to have an operator>> defined. Must have either a default constructor or must be constructible from fcppt::no_init. |
_source | The string to extract from |
|
inline |
Convert a string to a different type using fcppt::insert_extract_locale and format flags.
Dest | The destination type, has to have an operator>> defined. Must have either a default constructor or must be constructible from fcppt::no_init. |
fcppt::optional::object< Dest > fcppt::extract_from_string_locale_fmt | ( | Source && | _source, |
std::locale const & | _locale, | ||
fcppt::optional::object< std::ios_base::fmtflags > const | _flags ) |
Convert a string to a different type using a locale.
Dest | The destination type, has to have an operator>> defined. Must have either a default constructor or must be constructible from fcppt::no_init. |
Source | A string type (see fcppt::type_traits::is_string) |
FCPPT_DETAIL_SYMBOL fcppt::string fcppt::from_std_string | ( | std::string_view | ) |
Convert from std::string to fcppt::string using fcppt::string_conv_locale.
See fcppt.string for more information about this function.
FCPPT_DETAIL_SYMBOL fcppt::string fcppt::from_std_string_locale | ( | std::string_view | , |
std::locale const & | ) |
Convert from std::string to fcppt::string using a custom locale.
See fcppt.string for more information about this function.
FCPPT_DETAIL_SYMBOL fcppt::optional_string fcppt::from_std_wstring | ( | std::wstring_view | ) |
Convert from std::wstring to fcppt::string using fcppt::string_conv_locale.
See fcppt.string for more information about this function.
FCPPT_DETAIL_SYMBOL fcppt::optional_string fcppt::from_std_wstring_locale | ( | std::wstring_view | , |
std::locale const & | ) |
Convert from std::wstring to fcppt::string using a custom locale.
See fcppt.string for more information about this function.
FCPPT_DETAIL_SYMBOL std::locale fcppt::insert_extract_locale | ( | ) |
Returns the default locale to use when converting from or to strings.
This locale is the C locale. This was chosen to avoid confusion when converting, for example, "300,100" to int. Using the C locale, this will fail, since the string isn't completely consumed. Using a German locale, however, this will return "300100", since ',' is the thousand separator for this locale.
FCPPT_DETAIL_SYMBOL fcppt::optional_std_string fcppt::narrow | ( | std::wstring_view | _string | ) |
Converts a std::wstring to std::string, using fcppt::string_conv_locale.
Converts _string to std::string.
FCPPT_DETAIL_SYMBOL fcppt::optional_std_string fcppt::narrow_locale | ( | std::wstring_view | _string, |
std::locale const & | _locale ) |
Converts a std::wstring to std::string, using a locale.
Converts _string to std::string using _locale.
|
inline |
Converts an arbitrary type to a fcppt::string, using fcppt::insert_extract_locale.
Source | The type to make into a string. Has to have an operator<< defined. |
fcppt::string fcppt::output_to_fcppt_string_locale | ( | Source const & | _source, |
std::locale const & | _locale ) |
Converts an arbitrary type to a fcppt::string, using a custom locale.
Source | The type to make into a string. Has to have an operator<< defined. |
|
inline |
Convert an arbitrary type to a std::string, using fcppt::insert_extract_locale.
Source | The type to make into a string. Has to have an operator<< defined. |
|
inline |
Convert an arbitrary type to a std::string, using a custom locale.
Source | The type to make into a string. Has to have an operator<< defined. |
|
inline |
Convert an arbitrary type to a std::wstring, using fcppt::insert_extract_locale.
std::wstring fcppt::output_to_std_wstring_locale | ( | Source const & | _source, |
std::locale const & | _locale ) |
Convert an arbitrary type to a std::wstring, using a custom locale.
|
inline |
Convert an arbitrary type to a string, using fcppt::insert_extract_locale.
Dest | A string type, see fcppt::type_traits::is_string |
Source | The type to make into a string. Has to have an operator<< defined. |
Dest fcppt::output_to_string_locale | ( | Source const & | _source, |
std::locale const & | _locale ) |
Convert an arbitrary type to a string, using a custom locale.
Dest | A string type, see fcppt::type_traits::is_string. |
Source | The type to make into a string. Has to have an operator<< defined. |
fcppt::optional::object< std::basic_string< Ch, Traits > > fcppt::io::stream_to_string | ( | std::basic_istream< Ch, Traits > & | _input | ) |
Reads the contents of a stream into a string.
FCPPT_DETAIL_SYMBOL fcppt::string fcppt::error::strerrno | ( | ) |
A wrapper around std::strerror(strerrno)
A wrapper around std::strerror(strerrno)
returning the message for the current global error number.
FCPPT_DETAIL_SYMBOL fcppt::string fcppt::error::strerror | ( | int | errnum | ) |
A wrapper around std::strerror
A wrapper around std::strerror
returning the message for the error code given by errnum.
errnum The error code to return the message for
FCPPT_DETAIL_SYMBOL std::locale fcppt::string_conv_locale | ( | ) |
Returns the default locale to use when converting between strings.
FCPPT_DETAIL_SYMBOL fcppt::optional::object< int > fcppt::system | ( | fcppt::string const & | ) |
Calls std::system
.
std::system
may not be thread-safe because it may change signal settings. FCPPT_DETAIL_SYMBOL fcppt::optional_std_string fcppt::to_std_string | ( | fcppt::string_view | ) |
Convert from fcppt::string to std::string using fcppt::string_conv_locale.
See fcppt.string for more information about this function.
FCPPT_DETAIL_SYMBOL fcppt::optional_std_string fcppt::to_std_string_locale | ( | fcppt::string_view | , |
std::locale const & | ) |
Convert from fcppt::string to std::string using a custom locale.
See fcppt.string for more information about this function.
FCPPT_DETAIL_SYMBOL std::wstring fcppt::to_std_wstring | ( | fcppt::string_view | ) |
Convert from fcppt::string to std::wstring using fcppt::string_conv_locale.
See fcppt.string for more information about this function.
FCPPT_DETAIL_SYMBOL std::wstring fcppt::to_std_wstring_locale | ( | fcppt::string_view | , |
std::locale const & | ) |
Convert from fcppt::string to std::wstring using a custom locale.
See fcppt.string for more information about this function.
FCPPT_DETAIL_SYMBOL std::wstring fcppt::widen | ( | std::string_view | ) |
Converts a std::string to std::wstring, using fcppt::string_conv_locale.
Converts _string to std::wstring using _locale.
FCPPT_DETAIL_SYMBOL std::wstring fcppt::widen_locale | ( | std::string_view | , |
std::locale const & | ) |
Converts a std::string
to std::wstring
, using a locale.
Converts _string to std::wstring
using _locale.
std::runtime_error | If the conversion fails (which should not happen). |