aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--.editorconfig4
-rw-r--r--include/cru/common/String.h22
-rw-r--r--src/common/String.cpp48
-rw-r--r--src/platform/graphics/Geometry.cpp3
-rw-r--r--test/common/StringTest.cpp12
5 files changed, 62 insertions, 27 deletions
diff --git a/.editorconfig b/.editorconfig
index d5a3daa2..3455562a 100644
--- a/.editorconfig
+++ b/.editorconfig
@@ -8,6 +8,10 @@ insert_final_newline = true
indent_style = space
indent_size = 2
+[*.h]
+indent_style = space
+indent_size = 2
+
[*.hpp]
indent_style = space
indent_size = 2
diff --git a/include/cru/common/String.h b/include/cru/common/String.h
index be886168..60d0912e 100644
--- a/include/cru/common/String.h
+++ b/include/cru/common/String.h
@@ -13,6 +13,13 @@
#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 {
@@ -211,8 +218,10 @@ class CRU_BASE_API String {
Range RangeFromCodeUnitToCodePoint(Range code_unit_range) const;
Range RangeFromCodePointToCodeUnit(Range code_point_range) const;
- float ParseToFloat(Index* processed_characters_count = nullptr) const;
- double ParseToDouble(Index* processed_characters_count = nullptr) const;
+ float ParseToFloat(Index* processed_characters_count = nullptr,
+ int flags = StringToFloatFlags::kNoFlags) const;
+ double ParseToDouble(Index* processed_characters_count = nullptr,
+ int flags = StringToFloatFlags::kNoFlags) const;
std::vector<float> ParseToFloatList(value_type separator = u' ') const;
std::vector<double> ParseToDoubleList(value_type separator = u' ') const;
@@ -244,9 +253,6 @@ class CRU_BASE_API String {
class CRU_BASE_API StringView {
public:
- static double_conversion::StringToDoubleConverter
- kDefaultStringToDoubleConverter;
-
using value_type = char16_t;
using size_type = Index;
using difference_type = Index;
@@ -339,8 +345,10 @@ class CRU_BASE_API StringView {
Range RangeFromCodeUnitToCodePoint(Range code_unit_range) const;
Range RangeFromCodePointToCodeUnit(Range code_point_range) const;
- float ParseToFloat(Index* processed_characters_count = nullptr) const;
- double ParseToDouble(Index* processed_characters_count = nullptr) const;
+ float ParseToFloat(Index* processed_characters_count = nullptr,
+ int flags = StringToFloatFlags::kNoFlags) const;
+ double ParseToDouble(Index* processed_characters_count = nullptr,
+ int flags = StringToFloatFlags::kNoFlags) const;
std::vector<float> ParseToFloatList(value_type separator = u' ') const;
std::vector<double> ParseToDoubleList(value_type separator = u' ') const;
diff --git a/src/common/String.cpp b/src/common/String.cpp
index 1abee273..ed66b914 100644
--- a/src/common/String.cpp
+++ b/src/common/String.cpp
@@ -1,5 +1,6 @@
#include "cru/common/String.h"
#include <double-conversion/double-conversion.h>
+#include <double-conversion/string-to-double.h>
#include "cru/common/Exception.h"
#include "cru/common/StringUtil.h"
@@ -312,12 +313,13 @@ Range String::RangeFromCodePointToCodeUnit(Range code_point_range) const {
return View().RangeFromCodePointToCodeUnit(code_point_range);
}
-float String::ParseToFloat(Index* processed_characters_count) const {
- return View().ParseToFloat(processed_characters_count);
+float String::ParseToFloat(Index* processed_characters_count, int flags) const {
+ return View().ParseToFloat(processed_characters_count, flags);
}
-double String::ParseToDouble(Index* processed_characters_count) const {
- return View().ParseToDouble(processed_characters_count);
+double String::ParseToDouble(Index* processed_characters_count,
+ int flags) const {
+ return View().ParseToDouble(processed_characters_count, flags);
}
std::vector<float> String::ParseToFloatList(value_type separator) const {
@@ -345,15 +347,6 @@ int String::CaseInsensitiveCompare(const String& other) const {
return View().CaseInsensitiveCompare(other);
}
-double_conversion::StringToDoubleConverter
- StringView::kDefaultStringToDoubleConverter(
- double_conversion::StringToDoubleConverter::ALLOW_LEADING_SPACES |
- double_conversion::StringToDoubleConverter::ALLOW_TRAILING_SPACES |
- double_conversion::StringToDoubleConverter::
- ALLOW_CASE_INSENSIBILITY |
- double_conversion::StringToDoubleConverter::ALLOW_TRAILING_JUNK,
- 0.0, NAN, "infinity", "nan");
-
int StringView::Compare(const StringView& other) const {
const_iterator i1 = cbegin();
const_iterator i2 = other.cbegin();
@@ -523,9 +516,29 @@ std::string StringView::ToUtf8() const {
return result;
}
-float StringView::ParseToFloat(Index* processed_characters_count) const {
+static int MapStringToFloatFlags(int flags) {
+ int f = double_conversion::StringToDoubleConverter::ALLOW_CASE_INSENSIBILITY;
+ if (flags & StringToFloatFlags::kAllowLeadingSpaces) {
+ f |= double_conversion::StringToDoubleConverter::ALLOW_LEADING_SPACES;
+ }
+ if (flags & StringToFloatFlags::kAllowTrailingSpaces) {
+ f |= double_conversion::StringToDoubleConverter::ALLOW_TRAILING_SPACES;
+ }
+ if (flags & StringToFloatFlags::kAllowTrailingJunk) {
+ f |= double_conversion::StringToDoubleConverter::ALLOW_TRAILING_JUNK;
+ }
+ return f;
+}
+
+static double_conversion::StringToDoubleConverter CreateStringToDoubleConverter(
+ int flags) {
+ return {MapStringToFloatFlags(flags), 0.0, NAN, "inf", "nan"};
+}
+
+float StringView::ParseToFloat(Index* processed_characters_count,
+ int flags) const {
int pcc;
- auto result = kDefaultStringToDoubleConverter.StringToFloat(
+ auto result = CreateStringToDoubleConverter(flags).StringToFloat(
reinterpret_cast<const uc16*>(ptr_), static_cast<int>(size_), &pcc);
if (processed_characters_count != nullptr) {
*processed_characters_count = pcc;
@@ -533,9 +546,10 @@ float StringView::ParseToFloat(Index* processed_characters_count) const {
return result;
}
-double StringView::ParseToDouble(Index* processed_characters_count) const {
+double StringView::ParseToDouble(Index* processed_characters_count,
+ int flags) const {
int pcc;
- auto result = kDefaultStringToDoubleConverter.StringToDouble(
+ auto result = CreateStringToDoubleConverter(flags).StringToDouble(
reinterpret_cast<const uc16*>(ptr_), static_cast<int>(size_), &pcc);
if (processed_characters_count != nullptr) {
*processed_characters_count = pcc;
diff --git a/src/platform/graphics/Geometry.cpp b/src/platform/graphics/Geometry.cpp
index 389b97f5..32ba0d6d 100644
--- a/src/platform/graphics/Geometry.cpp
+++ b/src/platform/graphics/Geometry.cpp
@@ -192,7 +192,8 @@ void IGeometryBuilder::ParseAndApplySvgPathData(StringView path_d) {
Index processed_count = 0;
- auto result = path_d.substr(position).ParseToFloat(&processed_count);
+ auto result = path_d.substr(position).ParseToFloat(
+ &processed_count, StringToFloatFlags::kAllowTrailingJunk);
if (std::isnan(result)) throw Exception(u"Invalid svg path data number.");
diff --git a/test/common/StringTest.cpp b/test/common/StringTest.cpp
index c6e190c4..e053400e 100644
--- a/test/common/StringTest.cpp
+++ b/test/common/StringTest.cpp
@@ -86,10 +86,18 @@ TEST(String, FromUtf8) {
}
TEST(StringView, ParseToDouble) {
+ using cru::StringToFloatFlags;
using cru::StringView;
ASSERT_EQ(StringView(u"3.14159").ParseToDouble(), 3.14159);
- ASSERT_EQ(StringView(u" 3.14159").ParseToDouble(), 3.14159);
- ASSERT_EQ(StringView(u" 3.14159 ").ParseToDouble(), 3.14159);
+ ASSERT_EQ(
+ StringView(u" 3.14159")
+ .ParseToDouble(nullptr, StringToFloatFlags::kAllowLeadingSpaces),
+ 3.14159);
+ ASSERT_EQ(
+ StringView(u" 3.14159 ")
+ .ParseToDouble(nullptr, StringToFloatFlags::kAllowLeadingSpaces |
+ StringToFloatFlags::kAllowTrailingSpaces),
+ 3.14159);
}
TEST(String, ParseToDoubleList) {