diff options
Diffstat (limited to 'include/cru')
-rw-r--r-- | include/cru/common/String.h | 34 | ||||
-rw-r--r-- | include/cru/common/StringToNumberConverter.h | 19 |
2 files changed, 46 insertions, 7 deletions
diff --git a/include/cru/common/String.h b/include/cru/common/String.h index 7bbbecfb..c1ea839c 100644 --- a/include/cru/common/String.h +++ b/include/cru/common/String.h @@ -11,6 +11,7 @@ #include <iterator> #include <string> #include <string_view> +#include <type_traits> #include <vector> namespace cru { @@ -212,6 +213,17 @@ class CRU_BASE_API String { Range RangeFromCodeUnitToCodePoint(Range code_unit_range) const; Range RangeFromCodePointToCodeUnit(Range code_point_range) const; + template <typename TInteger> + std::enable_if_t<std::is_signed_v<TInteger>, TInteger> ParseToInteger( + Index* processed_characters_count, unsigned flags, int base) const; + + int ParseToInt(Index* processed_characters_count = nullptr, + unsigned flags = StringToNumberFlags::kNoFlags, + int base = 0) const; + long long ParseToLongLong(Index* processed_characters_count = nullptr, + unsigned flags = StringToNumberFlags::kNoFlags, + int base = 0) const; + float ParseToFloat(Index* processed_characters_count = nullptr, unsigned flags = StringToNumberFlags::kNoFlags) const; double ParseToDouble(Index* processed_characters_count = nullptr, @@ -339,6 +351,21 @@ class CRU_BASE_API StringView { Range RangeFromCodeUnitToCodePoint(Range code_unit_range) const; Range RangeFromCodePointToCodeUnit(Range code_point_range) const; + template <typename TInteger> + std::enable_if_t<std::is_signed_v<TInteger>, TInteger> ParseToInteger( + Index* processed_characters_count, unsigned flags, int base) const { + auto result = StringToIntegerConverterImpl(flags, base) + .Parse(data(), size(), processed_characters_count); + return result.negate ? -result.value : result.value; + } + + int ParseToInt(Index* processed_characters_count = nullptr, + unsigned flags = StringToNumberFlags::kNoFlags, + int base = 0) const; + long long ParseToLongLong(Index* processed_characters_count = nullptr, + unsigned flags = StringToNumberFlags::kNoFlags, + int base = 0) const; + float ParseToFloat(Index* processed_characters_count = nullptr, unsigned flags = StringToNumberFlags::kNoFlags) const; double ParseToDouble(Index* processed_characters_count = nullptr, @@ -434,6 +461,13 @@ inline Index Utf16NextWord(StringView str, Index position, String CRU_BASE_API ToLower(StringView s); String CRU_BASE_API ToUpper(StringView s); + +template <typename TInteger> +std::enable_if_t<std::is_signed_v<TInteger>, TInteger> String::ParseToInteger( + Index* processed_characters_count, unsigned flags, int base) const { + View().ParseToInteger<TInteger>(processed_characters_count, flags, base); +} + } // namespace cru template <> diff --git a/include/cru/common/StringToNumberConverter.h b/include/cru/common/StringToNumberConverter.h index b5118de6..f1baafee 100644 --- a/include/cru/common/StringToNumberConverter.h +++ b/include/cru/common/StringToNumberConverter.h @@ -4,7 +4,7 @@ #include <ostream> namespace cru { -struct StringToNumberFlags { +struct CRU_BASE_API StringToNumberFlags { constexpr static unsigned kNoFlags = 0; constexpr static unsigned kAllowLeadingSpaces = 1 << 0; constexpr static unsigned kAllowTrailingSpaces = 1 << 1; @@ -13,7 +13,7 @@ struct StringToNumberFlags { constexpr static unsigned kThrowOnError = 1 << 3; }; -struct StringToIntegerConverterImplResult { +struct CRU_BASE_API StringToIntegerConverterImplResult { StringToIntegerConverterImplResult() = default; StringToIntegerConverterImplResult(bool negate, unsigned long long value) : negate(negate), value(value) {} @@ -22,13 +22,15 @@ struct StringToIntegerConverterImplResult { unsigned long long value; }; -inline bool operator==(const StringToIntegerConverterImplResult& left, - const StringToIntegerConverterImplResult& right) { +inline bool CRU_BASE_API +operator==(const StringToIntegerConverterImplResult& left, + const StringToIntegerConverterImplResult& right) { return left.negate == right.negate && left.value == right.value; } -inline bool operator!=(const StringToIntegerConverterImplResult& left, - const StringToIntegerConverterImplResult& right) { +inline bool CRU_BASE_API +operator!=(const StringToIntegerConverterImplResult& left, + const StringToIntegerConverterImplResult& right) { return !(left == right); } @@ -41,7 +43,7 @@ inline std::ostream& operator<<( /** * \brief A converter that convert number into long long. */ -struct StringToIntegerConverterImpl { +struct CRU_BASE_API StringToIntegerConverterImpl { public: explicit StringToIntegerConverterImpl(unsigned flags, int base = 0) : flags(flags), base(base) {} @@ -58,6 +60,9 @@ struct StringToIntegerConverterImpl { StringToIntegerConverterImplResult Parse( const char* str, Index size, Index* processed_characters_count) const; + StringToIntegerConverterImplResult Parse( + const char16_t* str, Index size, Index* processed_characters_count) const; + template <std::size_t Size> StringToIntegerConverterImplResult Parse( const char (&str)[Size], Index* processed_characters_count) const { |