From 2028bc4914638360fb756079dbddbdbe52676821 Mon Sep 17 00:00:00 2001 From: crupest Date: Wed, 19 Jan 2022 22:55:22 +0800 Subject: ... --- src/common/String.cpp | 39 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) (limited to 'src/common/String.cpp') 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 #include #include @@ -10,6 +11,15 @@ #include 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 Index GetStrSize(const C* str) { Index i = 0; @@ -261,7 +271,8 @@ String& String::Trim() { return *this; } -std::vector String::SplitToLines(bool remove_space_line) const { +std::vector String::Split(value_type separator, + bool remove_space_line) const { std::vector result; if (size_ == 0) return result; @@ -269,7 +280,7 @@ std::vector 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::SplitToLines(bool remove_space_line) const { return result; } +std::vector 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(buffer_), static_cast(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(buffer_), static_cast(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); -- cgit v1.2.3