diff options
author | crupest <crupest@outlook.com> | 2022-01-19 22:55:22 +0800 |
---|---|---|
committer | crupest <crupest@outlook.com> | 2022-01-19 22:55:22 +0800 |
commit | 2028bc4914638360fb756079dbddbdbe52676821 (patch) | |
tree | bcbcf8d2d7eead39689324a06767e83faef72dfe /src/common/String.cpp | |
parent | a42609c6cc6d89501d0421f371e4a2dc10dd1685 (diff) | |
download | cru-2028bc4914638360fb756079dbddbdbe52676821.tar.gz cru-2028bc4914638360fb756079dbddbdbe52676821.tar.bz2 cru-2028bc4914638360fb756079dbddbdbe52676821.zip |
...
Diffstat (limited to 'src/common/String.cpp')
-rw-r--r-- | src/common/String.cpp | 39 |
1 files changed, 37 insertions, 2 deletions
diff --git a/src/common/String.cpp b/src/common/String.cpp index 9366abeb..48705d3b 100644 --- a/src/common/String.cpp +++ b/src/common/String.cpp @@ -2,6 +2,7 @@ #include "cru/common/Exception.hpp" #include "cru/common/StringUtil.hpp" +#include <cmath> #include <gsl/gsl> #include <algorithm> @@ -10,6 +11,15 @@ #include <string_view> namespace cru { +double_conversion::StringToDoubleConverter + String::kDefaultStringToDoubleConverter( + double_conversion::StringToDoubleConverter::ALLOW_TRAILING_JUNK | + double_conversion::StringToDoubleConverter::ALLOW_LEADING_SPACES | + double_conversion::StringToDoubleConverter::ALLOW_TRAILING_SPACES | + double_conversion::StringToDoubleConverter:: + ALLOW_CASE_INSENSIBILITY, + 0.0, NAN, "infinity", "nan"); + template <typename C> Index GetStrSize(const C* str) { Index i = 0; @@ -261,7 +271,8 @@ String& String::Trim() { return *this; } -std::vector<String> String::SplitToLines(bool remove_space_line) const { +std::vector<String> String::Split(value_type separator, + bool remove_space_line) const { std::vector<String> result; if (size_ == 0) return result; @@ -269,7 +280,7 @@ std::vector<String> String::SplitToLines(bool remove_space_line) const { Index line_start = 0; Index line_end = 0; while (line_end < size_) { - if (buffer_[line_end] == '\n') { + if (buffer_[line_end] == separator) { if (remove_space_line) { bool add = false; for (Index i = line_start; i < line_end; i++) { @@ -305,6 +316,10 @@ std::vector<String> String::SplitToLines(bool remove_space_line) const { return result; } +std::vector<String> String::SplitToLines(bool remove_space_line) const { + return Split(u'\n', remove_space_line); +} + bool String::StartWith(StringView str) const { if (str.size() > size_) return false; return std::memcmp(str.data(), buffer_, str.size()) == 0; @@ -419,6 +434,26 @@ int StringView::Compare(const StringView& other) const { } } +float String::ParseToFloat(Index* processed_characters_count) const { + int pcc; + auto result = kDefaultStringToDoubleConverter.StringToFloat( + reinterpret_cast<const uc16*>(buffer_), static_cast<int>(size_), &pcc); + if (processed_characters_count != nullptr) { + *processed_characters_count = pcc; + } + return result; +} + +double String::ParseToDouble(Index* processed_characters_count) const { + int pcc; + auto result = kDefaultStringToDoubleConverter.StringToDouble( + reinterpret_cast<const uc16*>(buffer_), static_cast<int>(size_), &pcc); + if (processed_characters_count != nullptr) { + *processed_characters_count = pcc; + } + return result; +} + StringView StringView::substr(Index pos) { Expects(pos >= 0 && pos < size_); return StringView(ptr_ + pos, size_ - pos); |