diff options
Diffstat (limited to 'include/cru/common')
-rw-r--r-- | include/cru/common/String.h | 16 | ||||
-rw-r--r-- | include/cru/common/StringToNumberConverter.h | 50 |
2 files changed, 55 insertions, 11 deletions
diff --git a/include/cru/common/String.h b/include/cru/common/String.h index 60d0912e..9ec494c6 100644 --- a/include/cru/common/String.h +++ b/include/cru/common/String.h @@ -2,6 +2,7 @@ #include "Base.h" #include "Range.h" +#include "StringToNumberConverter.h" #include "StringUtil.h" #include <double-conversion/double-conversion.h> @@ -13,13 +14,6 @@ #include <vector> namespace cru { -struct StringToFloatFlags { - constexpr static int kNoFlags = 0; - constexpr static int kAllowLeadingSpaces = 1 << 0; - constexpr static int kAllowTrailingSpaces = 1 << 1; - constexpr static int kAllowTrailingJunk = 1 << 2; -}; - class StringView; class CRU_BASE_API String { @@ -219,9 +213,9 @@ class CRU_BASE_API String { Range RangeFromCodePointToCodeUnit(Range code_point_range) const; float ParseToFloat(Index* processed_characters_count = nullptr, - int flags = StringToFloatFlags::kNoFlags) const; + unsigned flags = StringToNumberFlags::kNoFlags) const; double ParseToDouble(Index* processed_characters_count = nullptr, - int flags = StringToFloatFlags::kNoFlags) const; + unsigned flags = StringToNumberFlags::kNoFlags) const; std::vector<float> ParseToFloatList(value_type separator = u' ') const; std::vector<double> ParseToDoubleList(value_type separator = u' ') const; @@ -346,9 +340,9 @@ class CRU_BASE_API StringView { Range RangeFromCodePointToCodeUnit(Range code_point_range) const; float ParseToFloat(Index* processed_characters_count = nullptr, - int flags = StringToFloatFlags::kNoFlags) const; + unsigned flags = StringToNumberFlags::kNoFlags) const; double ParseToDouble(Index* processed_characters_count = nullptr, - int flags = StringToFloatFlags::kNoFlags) const; + unsigned flags = StringToNumberFlags::kNoFlags) const; std::vector<float> ParseToFloatList(value_type separator = u' ') const; std::vector<double> ParseToDoubleList(value_type separator = u' ') const; diff --git a/include/cru/common/StringToNumberConverter.h b/include/cru/common/StringToNumberConverter.h new file mode 100644 index 00000000..e68d12a6 --- /dev/null +++ b/include/cru/common/StringToNumberConverter.h @@ -0,0 +1,50 @@ +#pragma once +#include "Base.h" + +namespace cru { +struct StringToNumberFlags { + constexpr static unsigned kNoFlags = 0; + constexpr static unsigned kAllowLeadingSpaces = 1 << 0; + constexpr static unsigned kAllowTrailingSpaces = 1 << 1; + constexpr static unsigned kAllowTrailingJunk = 1 << 2; + constexpr static unsigned kAllowLeadingZeroForInteger = 1 << 2; + constexpr static unsigned kThrowOnError = 1 << 3; +}; + +struct StringToIntegerConverterImplResult { + bool negate; + unsigned long long value; +}; + +/** + * \brief A converter that convert number into long long. + */ +struct StringToIntegerConverterImpl { + public: + explicit StringToIntegerConverterImpl(unsigned flags, int base = 0) + : flags(flags), base(base) {} + + bool CheckParams() const; + + /** + * \brief Convert string to long long. + * \param str The string to convert. + * \param size The size of the string. + * \param processed_characters_count The number of characters that were + * processed. Or nullptr to not retrieve. + */ + StringToIntegerConverterImplResult Parse( + const char* str, Index size, Index* processed_characters_count) const; + + unsigned flags; + /** + * \brief The base of the number used for parse or 0 for auto detect. + * \remarks Base can only be of range [2, 36] or 0. If base is 0, decimal is + * assumed by default ,or if str is started with "0x" or "0X" hexadecimal is + * assumed, or if str is started with a single "0" octoral is assumed, or if + * str is started with "0b" or "0B" binary is assumed. Otherwise it is an + * error. + */ + int base; +}; +} // namespace cru |