#pragma region CPL License /* Nuclex Native Framework Copyright (C) 2002-2023 Nuclex Development Labs This library is free software; you can redistribute it and/or modify it under the terms of the IBM Common Public License as published by the IBM Corporation; either version 1.0 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 IBM Common Public License for more details. You should have received a copy of the IBM Common Public License along with this library */ #pragma endregion // CPL License #ifndef NUCLEX_SUPPORT_TEXT_LEXICALCAST_H #define NUCLEX_SUPPORT_TEXT_LEXICALCAST_H #include "Nuclex/Support/Config.h" #include "Nuclex/Support/Text/StringConverter.h" // UTF-8 and wide char conversion #include // for std::string #include // for std::uint8_t namespace Nuclex { namespace Support { namespace Text { // ------------------------------------------------------------------------------------------- // /// Lexically casts from a string to a non-string data type /// Type into which the string will be converted /// String that will be converted /// The value converted to the specified string /// /// /// This cast offers a portable way to convert between numeric and string types without /// resorting to cumbersome sprintf() constructs or relying on deprecated functions /// such as gcvt() or itoa(). /// /// /// /// No iostreams dependency /// Ignores system locale /// Full float-string-float round tripping /// /// /// template inline TTarget lexical_cast(const char *from) = delete; // ------------------------------------------------------------------------------------------- // /// Lexically casts between a string and non-string data type /// Type into which the value will be converted /// Type that will be converted /// Value that will be converted /// The value converted to the specified type /// /// /// This cast offers a portable way to convert between numeric and string types without /// resorting to cumbersome sprintf() constructs or relying on deprecated and functions /// such as gcvt() or itoa(). /// /// /// Lexical casts are guaranteed to completely ignore system locale and any other /// localization settings. Primitive types can be converted without pulling in iostreams /// (which is a bit of a heavyweight part of the SC++L). /// /// template inline TTarget lexical_cast(const TSource &from) = delete; // ------------------------------------------------------------------------------------------- // /// Lexically casts from a string to a non-string data type /// Type into which the string will be converted /// String that will be converted /// The value converted to the specified string /// /// /// This cast offers a portable way to convert between numeric and string types without /// resorting to cumbersome sprintf() constructs or relying on deprecated functions such /// as gcvt() or itoa(). /// /// /// Lexical casts are guaranteed to completely ignore system locale and any other /// localization settings. Primitive types can be converted without pulling in iostreams /// (which is a bit of a heavyweight part of the SC++L). /// /// template inline TTarget wlexical_cast(const wchar_t *from) { return lexical_cast(StringConverter::Utf8FromWide(from)); } // ------------------------------------------------------------------------------------------- // namespace Private { /// /// Performs a lexical_cast where one side is a UTF-16/UTF-32 string (std::wstring) /// /// Target type for the lexical cast /// Source type for the lexical cast template struct LexicalCastWithConversionHelper { /// Casts from the source type to the target type /// Value that will be cast or a UTF-16 or UTF-32 string /// Resulting UTF-16 or UTF-32 string or value inline static TTarget _(const TSource &from) = delete; }; /// /// Performs a lexical_cast but converts the input string from UTF-16 or UTF-32 /// /// Target type for the lexical cast template struct LexicalCastWithConversionHelper { /// Lexically casts from a UTF-16 or UTF-32 string to the target type /// UTF-16 or UTF-32 string to cast to the target type /// The resulting value inline static TTarget _(const std::wstring &from) { return lexical_cast(StringConverter::Utf8FromWide(from)); } }; /// /// Performs a lexical_cast but converts the resulting string to UTF-16 or UTF-32 // /// Source type for the lexical cast template struct LexicalCastWithConversionHelper { /// Casts from the source type to a UTF-16 or UTF-32 string /// Value that will be cast to a UTF-16 or UTF-32 string /// Resulting UTF-16 or UTF-32 string inline static std::wstring _(const TSource &from) { return StringConverter::WideFromUtf8(lexical_cast(from)); } }; } // namespace Private /// Lexically casts between a string and non-string data type /// Type into which the value will be converted /// Type that will be converted /// Value that will be converted /// The value converted to the specified type /// /// /// This cast offers a portable way to convert between numeric and string types without /// resorting to cumbersome sprintf() constructs or relying on deprecated functions such /// as gcvt() or itoa(). /// /// /// Lexical casts are guaranteed to completely ignore system locale and any other /// localization settings. It also performs all conversion without pulling in iostreams /// (which is a bit of a heavyweight part of the SC++L). /// /// template inline TTarget wlexical_cast(const TSource &from) { return Private::LexicalCastWithConversionHelper::_(from); } // ------------------------------------------------------------------------------------------- // /// Converts a boolean value into a string /// Boolean value that will be converted /// A string containing the printed boolean value template<> NUCLEX_SUPPORT_API std::string lexical_cast<>(const bool &from); // ------------------------------------------------------------------------------------------- // /// Converts a string into a boolean value /// String that will be converted /// The boolean value parsed from the specified string template<> NUCLEX_SUPPORT_API bool lexical_cast<>(const char *from); // ------------------------------------------------------------------------------------------- // /// Converts a string into a boolean value /// String that will be converted /// The boolean value parsed from the specified string template<> NUCLEX_SUPPORT_API bool lexical_cast<>(const std::string &from); // ------------------------------------------------------------------------------------------- // /// Converts an 8 bit unsigned integer into a string /// 8 bit unsigned integer that will be converted /// A string containing the printed 8 bit unsigned integer value template<> NUCLEX_SUPPORT_API std::string lexical_cast<>(const std::uint8_t &from); // ------------------------------------------------------------------------------------------- // /// Converts a string into an 8 bit unsigned integer /// String that will be converted /// The 8 bit unsigned integer parsed from the specified string template<> NUCLEX_SUPPORT_API std::uint8_t lexical_cast<>(const char *from); // ------------------------------------------------------------------------------------------- // /// Converts a string into an 8 bit unsigned integer /// String that will be converted /// The 8 bit unsigned integer parsed from the specified string template<> NUCLEX_SUPPORT_API std::uint8_t lexical_cast<>(const std::string &from); // ------------------------------------------------------------------------------------------- // /// Converts an 8 bit unsigned integer into a string /// 8 bit unsigned integer that will be converted /// A string containing the printed 8 bit unsigned integer value template<> NUCLEX_SUPPORT_API std::string lexical_cast<>(const std::int8_t &from); // ------------------------------------------------------------------------------------------- // /// Converts a string into an 8 bit signed integer /// String that will be converted /// The 8 bit signed integer parsed from the specified string template<> NUCLEX_SUPPORT_API std::int8_t lexical_cast<>(const char *from); // ------------------------------------------------------------------------------------------- // /// Converts a string into an 8 bit signed integer /// String that will be converted /// The 8 bit signed integer parsed from the specified string template<> NUCLEX_SUPPORT_API std::int8_t lexical_cast<>(const std::string &from); // ------------------------------------------------------------------------------------------- // /// Converts an 16 bit unsigned integer into a string /// 16 bit unsigned integer that will be converted /// A string containing the printed 16 bit unsigned integer value template<> NUCLEX_SUPPORT_API std::string lexical_cast<>(const std::uint16_t &from); // ------------------------------------------------------------------------------------------- // /// Converts a string into a 16 bit unsigned integer /// String that will be converted /// The 16 bit unsigned integer parsed from the specified string template<> NUCLEX_SUPPORT_API std::uint16_t lexical_cast<>(const char *from); // ------------------------------------------------------------------------------------------- // /// Converts a string into a 16 bit unsigned integer /// String that will be converted /// The 16 bit unsigned integer parsed from the specified string template<> NUCLEX_SUPPORT_API std::uint16_t lexical_cast<>(const std::string &from); // ------------------------------------------------------------------------------------------- // /// Converts an 16 bit signed integer into a string /// 16 bit signed integer that will be converted /// A string containing the printed 16 bit signed integer value template<> NUCLEX_SUPPORT_API std::string lexical_cast<>(const std::int16_t &from); // ------------------------------------------------------------------------------------------- // /// Converts a string into a 16 bit signed integer /// String that will be converted /// The 16 bit signed integer parsed from the specified string template<> NUCLEX_SUPPORT_API std::int16_t lexical_cast<>(const char *from); // ------------------------------------------------------------------------------------------- // /// Converts a string into a 16 bit signed integer /// String that will be converted /// The 16 bit signed integer parsed from the specified string template<> NUCLEX_SUPPORT_API std::int16_t lexical_cast<>(const std::string &from); // ------------------------------------------------------------------------------------------- // /// Converts a 32 bit unsigned integer into a string /// 32 bit unsigned integer that will be converted /// A string containing the printed 32 bit unsigned integer value template<> NUCLEX_SUPPORT_API std::string lexical_cast<>(const std::uint32_t &from); // ------------------------------------------------------------------------------------------- // /// Converts a string into a 32 bit unsigned integer /// String that will be converted /// The 32 bit unsigned integer parsed from the specified string template<> NUCLEX_SUPPORT_API std::uint32_t lexical_cast<>(const char *from); // ------------------------------------------------------------------------------------------- // /// Converts a string into a 32 bit unsigned integer /// String that will be converted /// The 32 bit unsigned integer parsed from the specified string template<> NUCLEX_SUPPORT_API std::uint32_t lexical_cast<>(const std::string &from); // ------------------------------------------------------------------------------------------- // /// Converts a 32 bit signed integer into a string /// 32 bit signed integer that will be converted /// A string containing the printed 32 bit signed integer value template<> NUCLEX_SUPPORT_API std::string lexical_cast<>(const std::int32_t &from); // ------------------------------------------------------------------------------------------- // /// Converts a string into a 32 bit signed integer /// String that will be converted /// The 32 bit signed integer parsed from the specified string template<> NUCLEX_SUPPORT_API std::int32_t lexical_cast<>(const char *from); // ------------------------------------------------------------------------------------------- // /// Converts a string into a 32 bit signed integer /// String that will be converted /// The 32 bit signed integer parsed from the specified string template<> NUCLEX_SUPPORT_API std::int32_t lexical_cast<>(const std::string &from); // ------------------------------------------------------------------------------------------- // /// Converts a 64 bit unsigned integer into a string /// 64 bit unsigned integer that will be converted /// A string containing the printed 64 bit unsigned integer value template<> NUCLEX_SUPPORT_API std::string lexical_cast<>(const std::uint64_t &from); // ------------------------------------------------------------------------------------------- // /// Converts a string into a 64 bit unsigned integer /// String that will be converted /// The 64 bit unsigned integer parsed from the specified string template<> NUCLEX_SUPPORT_API std::uint64_t lexical_cast<>(const char *from); // ------------------------------------------------------------------------------------------- // /// Converts a string into a 64 bit unsigned integer /// String that will be converted /// The 64 bit unsigned integer parsed from the specified string template<> NUCLEX_SUPPORT_API std::uint64_t lexical_cast<>(const std::string &from); // ------------------------------------------------------------------------------------------- // /// Converts a 64 bit signed integer into a string /// 64 bit signed integer that will be converted /// A string containing the printed 64 bit signed integer value template<> NUCLEX_SUPPORT_API std::string lexical_cast<>(const std::int64_t &from); // ------------------------------------------------------------------------------------------- // /// Converts a string into a 64 bit signed integer /// String that will be converted /// The 64 bit signed integer parsed from the specified string template<> NUCLEX_SUPPORT_API std::int64_t lexical_cast<>(const char *from); // ------------------------------------------------------------------------------------------- // /// Converts a string into a 64 bit signed integer /// String that will be converted /// The 64 bit signed integer parsed from the specified string template<> NUCLEX_SUPPORT_API std::int64_t lexical_cast<>(const std::string &from); // ------------------------------------------------------------------------------------------- // /// Converts a floating point value into a string /// Floating point value that will be converted /// A string containing the printed floating point value template<> NUCLEX_SUPPORT_API std::string lexical_cast<>(const float &from); // ------------------------------------------------------------------------------------------- // /// Converts a string into a floating point value /// String that will be converted /// The floating point value parsed from the specified string template<> NUCLEX_SUPPORT_API float lexical_cast<>(const char *from); // ------------------------------------------------------------------------------------------- // /// Converts a string into a floating point value /// String that will be converted /// The floating point value parsed from the specified string template<> NUCLEX_SUPPORT_API float lexical_cast<>(const std::string &from); // ------------------------------------------------------------------------------------------- // /// Converts a double precision floating point value into a string /// Double precision Floating point value that will be converted /// A string containing the printed double precision floating point value template<> NUCLEX_SUPPORT_API std::string lexical_cast<>(const double &from); // ------------------------------------------------------------------------------------------- // /// Converts a string into a double precision floating point value /// String that will be converted /// The floating point value parsed from the specified string template<> NUCLEX_SUPPORT_API double lexical_cast<>(const char *from); // ------------------------------------------------------------------------------------------- // /// Converts a string into a double precision floating point value /// String that will be converted /// The floating point value parsed from the specified string template<> NUCLEX_SUPPORT_API double lexical_cast<>(const std::string &from); // ------------------------------------------------------------------------------------------- // }}} // namespace Nuclex::Support::Text #endif // NUCLEX_SUPPORT_TEXT_LEXICALCAST_H