react-native-nitro-input 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +38 -0
- package/README.md +43 -1
- package/android/CMakeLists.txt +4 -0
- package/android/gradle.properties +1 -1
- package/android/src/main/cpp/JRollingEngine.cpp +3 -2
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroInputView.kt +20 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroNumberFormatPlatform.kt +209 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroNumberView.kt +25 -0
- package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputView.kt +69 -10
- package/android/src/main/java/com/margelo/nitro/nitroinput/NitroNumberView.kt +164 -24
- package/android/src/main/java/com/margelo/nitro/nitroinput/RollingEngine.kt +2 -1
- package/cpp/RollingEngine.cpp +12 -2
- package/cpp/RollingEngine.hpp +8 -1
- package/cpp/numberformat/HybridNitroNumberFormat.cpp +498 -0
- package/cpp/numberformat/HybridNitroNumberFormat.hpp +74 -0
- package/cpp/numberformat/HybridNitroNumberFormatFactory.hpp +11 -0
- package/cpp/numberformat/NumberFormatCore.cpp +629 -0
- package/cpp/numberformat/NumberFormatCore.hpp +154 -0
- package/cpp/numberformat/NumberFormatProbe.cpp +354 -0
- package/cpp/numberformat/NumberFormatProbe.hpp +63 -0
- package/ios/HybridNitroInputView.swift +15 -0
- package/ios/HybridNitroNumberFormatPlatform.swift +348 -0
- package/ios/HybridNitroNumberView.swift +19 -0
- package/ios/NitroInputView.swift +79 -19
- package/ios/NitroNumberView.swift +171 -31
- package/lib/commonjs/nitrogen/generated/shared/json/NitroInputViewConfig.json +5 -0
- package/lib/commonjs/nitrogen/generated/shared/json/NitroNumberViewConfig.json +6 -0
- package/lib/commonjs/src/NitroInput.js +8 -6
- package/lib/commonjs/src/NitroNumber.js +4 -2
- package/lib/commonjs/src/NumberFormat.js +525 -0
- package/lib/commonjs/src/formatProps.js +48 -0
- package/lib/commonjs/src/index.js +3 -1
- package/lib/commonjs/src/specs/NumberFormat.nitro.js +2 -0
- package/lib/module/nitrogen/generated/shared/json/NitroInputViewConfig.json +5 -0
- package/lib/module/nitrogen/generated/shared/json/NitroNumberViewConfig.json +6 -0
- package/lib/module/src/NitroInput.js +8 -6
- package/lib/module/src/NitroNumber.js +4 -2
- package/lib/module/src/NumberFormat.js +521 -0
- package/lib/module/src/formatProps.js +45 -0
- package/lib/module/src/index.js +1 -0
- package/lib/module/src/specs/NumberFormat.nitro.js +1 -0
- package/lib/typescript/src/NitroInput.d.ts +22 -0
- package/lib/typescript/src/NitroNumber.d.ts +36 -1
- package/lib/typescript/src/NumberFormat.d.ts +47 -0
- package/lib/typescript/src/formatProps.d.ts +21 -0
- package/lib/typescript/src/index.d.ts +2 -0
- package/lib/typescript/src/specs/NitroInput.nitro.d.ts +10 -0
- package/lib/typescript/src/specs/NitroNumber.nitro.d.ts +13 -1
- package/lib/typescript/src/specs/NumberFormat.nitro.d.ts +139 -0
- package/nitro.json +16 -0
- package/nitrogen/generated/android/NitroInput+autolinking.cmake +6 -0
- package/nitrogen/generated/android/NitroInputOnLoad.cpp +28 -0
- package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.cpp +45 -0
- package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.hpp +10 -0
- package/nitrogen/generated/android/c++/JHybridNitroNumberFormatPlatformSpec.cpp +118 -0
- package/nitrogen/generated/android/c++/JHybridNitroNumberFormatPlatformSpec.hpp +64 -0
- package/nitrogen/generated/android/c++/JHybridNitroNumberViewSpec.cpp +54 -0
- package/nitrogen/generated/android/c++/JHybridNitroNumberViewSpec.hpp +12 -0
- package/nitrogen/generated/android/c++/JHybridNitroPlatformNumberFormatterSpec.cpp +65 -0
- package/nitrogen/generated/android/c++/JHybridNitroPlatformNumberFormatterSpec.hpp +64 -0
- package/nitrogen/generated/android/c++/JNitroNumberDirection.hpp +3 -0
- package/nitrogen/generated/android/c++/JNumberFormatCompactDisplay.hpp +58 -0
- package/nitrogen/generated/android/c++/JNumberFormatCurrencyDisplay.hpp +64 -0
- package/nitrogen/generated/android/c++/JNumberFormatCurrencySign.hpp +58 -0
- package/nitrogen/generated/android/c++/JNumberFormatNotation.hpp +64 -0
- package/nitrogen/generated/android/c++/JNumberFormatPlatformOptions.hpp +161 -0
- package/nitrogen/generated/android/c++/JNumberFormatPlatformSymbols.hpp +89 -0
- package/nitrogen/generated/android/c++/JNumberFormatRoundingMode.hpp +79 -0
- package/nitrogen/generated/android/c++/JNumberFormatSignDisplay.hpp +67 -0
- package/nitrogen/generated/android/c++/JNumberFormatStyle.hpp +64 -0
- package/nitrogen/generated/android/c++/JNumberFormatUnitDisplay.hpp +61 -0
- package/nitrogen/generated/android/c++/views/JHybridNitroInputViewStateUpdater.cpp +25 -0
- package/nitrogen/generated/android/c++/views/JHybridNitroNumberViewStateUpdater.cpp +30 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroInputViewSpec.kt +30 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroNumberFormatPlatformSpec.kt +60 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroNumberViewSpec.kt +36 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroPlatformNumberFormatterSpec.kt +62 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroNumberDirection.kt +2 -1
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatCompactDisplay.kt +23 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatCurrencyDisplay.kt +25 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatCurrencySign.kt +23 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatNotation.kt +25 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatPlatformOptions.kt +136 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatPlatformSymbols.kt +91 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatRoundingMode.kt +30 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatSignDisplay.kt +26 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatStyle.kt +25 -0
- package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatUnitDisplay.kt +24 -0
- package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.cpp +34 -0
- package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.hpp +63 -0
- package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Umbrella.hpp +40 -0
- package/nitrogen/generated/ios/NitroInputAutolinking.mm +18 -0
- package/nitrogen/generated/ios/NitroInputAutolinking.swift +12 -0
- package/nitrogen/generated/ios/c++/HybridNitroInputViewSpecSwift.hpp +30 -0
- package/nitrogen/generated/ios/c++/HybridNitroNumberFormatPlatformSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridNitroNumberFormatPlatformSpecSwift.hpp +122 -0
- package/nitrogen/generated/ios/c++/HybridNitroNumberViewSpecSwift.hpp +42 -0
- package/nitrogen/generated/ios/c++/HybridNitroPlatformNumberFormatterSpecSwift.cpp +11 -0
- package/nitrogen/generated/ios/c++/HybridNitroPlatformNumberFormatterSpecSwift.hpp +94 -0
- package/nitrogen/generated/ios/c++/views/HybridNitroInputViewComponent.mm +30 -0
- package/nitrogen/generated/ios/c++/views/HybridNitroNumberViewComponent.mm +36 -0
- package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec.swift +5 -0
- package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec_cxx.swift +55 -0
- package/nitrogen/generated/ios/swift/HybridNitroNumberFormatPlatformSpec.swift +56 -0
- package/nitrogen/generated/ios/swift/HybridNitroNumberFormatPlatformSpec_cxx.swift +159 -0
- package/nitrogen/generated/ios/swift/HybridNitroNumberViewSpec.swift +6 -0
- package/nitrogen/generated/ios/swift/HybridNitroNumberViewSpec_cxx.swift +144 -0
- package/nitrogen/generated/ios/swift/HybridNitroPlatformNumberFormatterSpec.swift +56 -0
- package/nitrogen/generated/ios/swift/HybridNitroPlatformNumberFormatterSpec_cxx.swift +155 -0
- package/nitrogen/generated/ios/swift/NitroNumberDirection.swift +4 -0
- package/nitrogen/generated/ios/swift/NumberFormatCompactDisplay.swift +40 -0
- package/nitrogen/generated/ios/swift/NumberFormatCurrencyDisplay.swift +48 -0
- package/nitrogen/generated/ios/swift/NumberFormatCurrencySign.swift +40 -0
- package/nitrogen/generated/ios/swift/NumberFormatNotation.swift +48 -0
- package/nitrogen/generated/ios/swift/NumberFormatPlatformOptions.swift +159 -0
- package/nitrogen/generated/ios/swift/NumberFormatPlatformSymbols.swift +69 -0
- package/nitrogen/generated/ios/swift/NumberFormatRoundingMode.swift +68 -0
- package/nitrogen/generated/ios/swift/NumberFormatSignDisplay.swift +52 -0
- package/nitrogen/generated/ios/swift/NumberFormatStyle.swift +48 -0
- package/nitrogen/generated/ios/swift/NumberFormatUnitDisplay.swift +44 -0
- package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.cpp +10 -0
- package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.hpp +10 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberFormatFactorySpec.cpp +22 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberFormatFactorySpec.hpp +70 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberFormatPlatformSpec.cpp +22 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberFormatPlatformSpec.hpp +70 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberFormatSpec.cpp +23 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberFormatSpec.hpp +71 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberViewSpec.cpp +12 -0
- package/nitrogen/generated/shared/c++/HybridNitroNumberViewSpec.hpp +12 -0
- package/nitrogen/generated/shared/c++/HybridNitroPlatformNumberFormatterSpec.cpp +23 -0
- package/nitrogen/generated/shared/c++/HybridNitroPlatformNumberFormatterSpec.hpp +65 -0
- package/nitrogen/generated/shared/c++/NitroNumberDirection.hpp +4 -0
- package/nitrogen/generated/shared/c++/NumberFormatCompactDisplay.hpp +76 -0
- package/nitrogen/generated/shared/c++/NumberFormatCurrencyDisplay.hpp +84 -0
- package/nitrogen/generated/shared/c++/NumberFormatCurrencySign.hpp +76 -0
- package/nitrogen/generated/shared/c++/NumberFormatGrouping.hpp +80 -0
- package/nitrogen/generated/shared/c++/NumberFormatNotation.hpp +84 -0
- package/nitrogen/generated/shared/c++/NumberFormatOptions.hpp +193 -0
- package/nitrogen/generated/shared/c++/NumberFormatPart.hpp +87 -0
- package/nitrogen/generated/shared/c++/NumberFormatPlatformOptions.hpp +176 -0
- package/nitrogen/generated/shared/c++/NumberFormatPlatformSymbols.hpp +115 -0
- package/nitrogen/generated/shared/c++/NumberFormatRoundingMode.hpp +104 -0
- package/nitrogen/generated/shared/c++/NumberFormatRoundingPriority.hpp +80 -0
- package/nitrogen/generated/shared/c++/NumberFormatSignDisplay.hpp +88 -0
- package/nitrogen/generated/shared/c++/NumberFormatStyle.hpp +84 -0
- package/nitrogen/generated/shared/c++/NumberFormatTrailingZeroDisplay.hpp +76 -0
- package/nitrogen/generated/shared/c++/NumberFormatUnitDisplay.hpp +80 -0
- package/nitrogen/generated/shared/c++/ResolvedNumberFormatOptions.hpp +197 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.cpp +10 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.hpp +15 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroNumberViewComponent.cpp +12 -0
- package/nitrogen/generated/shared/c++/views/HybridNitroNumberViewComponent.hpp +18 -0
- package/nitrogen/generated/shared/json/NitroInputViewConfig.json +5 -0
- package/nitrogen/generated/shared/json/NitroNumberViewConfig.json +6 -0
- package/package.json +2 -2
- package/src/NitroInput.tsx +42 -7
- package/src/NitroNumber.tsx +57 -7
- package/src/NumberFormat.ts +563 -0
- package/src/formatProps.ts +59 -0
- package/src/index.ts +18 -0
- package/src/specs/NitroInput.nitro.ts +10 -0
- package/src/specs/NitroNumber.nitro.ts +13 -1
- package/src/specs/NumberFormat.nitro.ts +145 -0
|
@@ -0,0 +1,629 @@
|
|
|
1
|
+
//
|
|
2
|
+
// NumberFormatCore.cpp
|
|
3
|
+
// NitroInput
|
|
4
|
+
//
|
|
5
|
+
|
|
6
|
+
#include "NumberFormatCore.hpp"
|
|
7
|
+
|
|
8
|
+
#include <algorithm>
|
|
9
|
+
#include <cerrno>
|
|
10
|
+
#include <charconv>
|
|
11
|
+
#include <cmath>
|
|
12
|
+
#include <cstdio>
|
|
13
|
+
#include <cstdlib>
|
|
14
|
+
#include <utility>
|
|
15
|
+
|
|
16
|
+
namespace margelo::nitro::nitroinput::numberformat {
|
|
17
|
+
|
|
18
|
+
const char* partTypeName(PartType type) {
|
|
19
|
+
switch (type) {
|
|
20
|
+
case PartType::Literal:
|
|
21
|
+
return "literal";
|
|
22
|
+
case PartType::Integer:
|
|
23
|
+
return "integer";
|
|
24
|
+
case PartType::Group:
|
|
25
|
+
return "group";
|
|
26
|
+
case PartType::Decimal:
|
|
27
|
+
return "decimal";
|
|
28
|
+
case PartType::Fraction:
|
|
29
|
+
return "fraction";
|
|
30
|
+
case PartType::MinusSign:
|
|
31
|
+
return "minusSign";
|
|
32
|
+
case PartType::PlusSign:
|
|
33
|
+
return "plusSign";
|
|
34
|
+
case PartType::PercentSign:
|
|
35
|
+
return "percentSign";
|
|
36
|
+
case PartType::Currency:
|
|
37
|
+
return "currency";
|
|
38
|
+
case PartType::Nan:
|
|
39
|
+
return "nan";
|
|
40
|
+
case PartType::Infinity:
|
|
41
|
+
return "infinity";
|
|
42
|
+
case PartType::Compact:
|
|
43
|
+
return "compact";
|
|
44
|
+
case PartType::Unit:
|
|
45
|
+
return "unit";
|
|
46
|
+
case PartType::ExponentSeparator:
|
|
47
|
+
return "exponentSeparator";
|
|
48
|
+
case PartType::ExponentMinusSign:
|
|
49
|
+
return "exponentMinusSign";
|
|
50
|
+
case PartType::ExponentInteger:
|
|
51
|
+
return "exponentInteger";
|
|
52
|
+
}
|
|
53
|
+
return "literal";
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// MARK: - Decimal
|
|
57
|
+
|
|
58
|
+
namespace {
|
|
59
|
+
|
|
60
|
+
/// Drops leading and trailing zeros; `point` follows the leading ones.
|
|
61
|
+
void normalize(Decimal& d) {
|
|
62
|
+
size_t lead = 0;
|
|
63
|
+
while (lead < d.digits.size() && d.digits[lead] == '0') lead++;
|
|
64
|
+
if (lead > 0) {
|
|
65
|
+
d.digits.erase(0, lead);
|
|
66
|
+
d.point -= static_cast<int>(lead);
|
|
67
|
+
}
|
|
68
|
+
while (!d.digits.empty() && d.digits.back() == '0') d.digits.pop_back();
|
|
69
|
+
if (d.digits.empty()) d.point = 0;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/// Reads "d.ddde±x" (the decimal point may be ',' under a C locale that says so).
|
|
73
|
+
Decimal fromScientific(const char* begin, const char* end, bool negative) {
|
|
74
|
+
Decimal d;
|
|
75
|
+
d.negative = negative;
|
|
76
|
+
int exponent = 0;
|
|
77
|
+
const char* p = begin;
|
|
78
|
+
int integerDigits = 0;
|
|
79
|
+
bool afterPoint = false;
|
|
80
|
+
for (; p < end && *p != 'e' && *p != 'E'; p++) {
|
|
81
|
+
if (*p >= '0' && *p <= '9') {
|
|
82
|
+
d.digits.push_back(*p);
|
|
83
|
+
if (!afterPoint) integerDigits++;
|
|
84
|
+
} else if (*p == '.' || *p == ',') {
|
|
85
|
+
afterPoint = true;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (p < end) exponent = std::atoi(std::string(p + 1, end).c_str());
|
|
89
|
+
d.point = integerDigits + exponent;
|
|
90
|
+
normalize(d);
|
|
91
|
+
return d;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
} // namespace
|
|
95
|
+
|
|
96
|
+
Decimal Decimal::fromDouble(double value) {
|
|
97
|
+
Decimal d;
|
|
98
|
+
if (std::isnan(value)) {
|
|
99
|
+
d.kind = Kind::NaN;
|
|
100
|
+
return d;
|
|
101
|
+
}
|
|
102
|
+
d.negative = std::signbit(value);
|
|
103
|
+
if (std::isinf(value)) {
|
|
104
|
+
d.kind = Kind::Infinity;
|
|
105
|
+
return d;
|
|
106
|
+
}
|
|
107
|
+
const double magnitude = std::fabs(value);
|
|
108
|
+
if (magnitude == 0) return d;
|
|
109
|
+
|
|
110
|
+
// The common case, amounts: the fewest fraction digits k for which the
|
|
111
|
+
// nearest integer r to value·10^k gives back the same double as r / 10^k.
|
|
112
|
+
// r and 10^k are exact below 2^53, so the division is the correctly rounded
|
|
113
|
+
// value of the decimal r·10^-k: the test is exact, and the first k that
|
|
114
|
+
// passes is the shortest round-trip form, as to_chars would print it.
|
|
115
|
+
static constexpr double kPow10[] = {1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10, 1e11, 1e12, 1e13, 1e14, 1e15};
|
|
116
|
+
constexpr double kExact = 9007199254740992.0; // 2^53
|
|
117
|
+
for (int k = 0; k < 16; k++) {
|
|
118
|
+
const double scaled = magnitude * kPow10[k];
|
|
119
|
+
if (scaled >= kExact) break;
|
|
120
|
+
const double r = std::nearbyint(scaled);
|
|
121
|
+
if (r / kPow10[k] == magnitude) {
|
|
122
|
+
d.digits = std::to_string(static_cast<uint64_t>(r));
|
|
123
|
+
d.point = static_cast<int>(d.digits.size()) - k;
|
|
124
|
+
normalize(d);
|
|
125
|
+
return d;
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
char buffer[64];
|
|
130
|
+
#if !defined(__APPLE__)
|
|
131
|
+
{
|
|
132
|
+
const auto result = std::to_chars(buffer, buffer + sizeof(buffer), magnitude, std::chars_format::scientific);
|
|
133
|
+
if (result.ec == std::errc()) return fromScientific(buffer, result.ptr, d.negative);
|
|
134
|
+
}
|
|
135
|
+
#endif
|
|
136
|
+
// The shortest of 15, 16 or 17 significant digits that reads back as the same double.
|
|
137
|
+
for (int precision = 14; precision <= 16; precision++) {
|
|
138
|
+
const int length = std::snprintf(buffer, sizeof(buffer), "%.*e", precision, magnitude);
|
|
139
|
+
if (length <= 0) continue;
|
|
140
|
+
if (precision == 16 || std::strtod(buffer, nullptr) == magnitude) return fromScientific(buffer, buffer + length, d.negative);
|
|
141
|
+
}
|
|
142
|
+
return d;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
Decimal Decimal::fromInt64(int64_t value) {
|
|
146
|
+
Decimal d;
|
|
147
|
+
d.negative = value < 0;
|
|
148
|
+
// Through unsigned so INT64_MIN has a magnitude.
|
|
149
|
+
uint64_t magnitude = value < 0 ? 0 - static_cast<uint64_t>(value) : static_cast<uint64_t>(value);
|
|
150
|
+
d.digits = std::to_string(magnitude);
|
|
151
|
+
d.point = static_cast<int>(d.digits.size());
|
|
152
|
+
normalize(d);
|
|
153
|
+
return d;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
Decimal Decimal::fromString(std::string_view text) {
|
|
157
|
+
Decimal nan;
|
|
158
|
+
nan.kind = Kind::NaN;
|
|
159
|
+
// StringToNumber trims white space and line terminators.
|
|
160
|
+
auto isSpace = [](char c) { return c == ' ' || c == '\t' || c == '\n' || c == '\r' || c == '\v' || c == '\f'; };
|
|
161
|
+
while (!text.empty() && isSpace(text.front())) text.remove_prefix(1);
|
|
162
|
+
while (!text.empty() && isSpace(text.back())) text.remove_suffix(1);
|
|
163
|
+
Decimal d;
|
|
164
|
+
if (text.empty()) return d; // "" is 0
|
|
165
|
+
// 0x / 0o / 0b integers take no sign.
|
|
166
|
+
if (text.size() > 2 && text[0] == '0') {
|
|
167
|
+
int base = 0;
|
|
168
|
+
if (text[1] == 'x' || text[1] == 'X') base = 16;
|
|
169
|
+
else if (text[1] == 'o' || text[1] == 'O') base = 8;
|
|
170
|
+
else if (text[1] == 'b' || text[1] == 'B') base = 2;
|
|
171
|
+
if (base != 0) {
|
|
172
|
+
// Accumulate in decimal digits so any length works.
|
|
173
|
+
std::string decimal = "0";
|
|
174
|
+
for (char c : text.substr(2)) {
|
|
175
|
+
int v = -1;
|
|
176
|
+
if (c >= '0' && c <= '9') v = c - '0';
|
|
177
|
+
else if (c >= 'a' && c <= 'f') v = c - 'a' + 10;
|
|
178
|
+
else if (c >= 'A' && c <= 'F') v = c - 'A' + 10;
|
|
179
|
+
if (v < 0 || v >= base) return nan;
|
|
180
|
+
int carry = v;
|
|
181
|
+
for (auto it = decimal.rbegin(); it != decimal.rend(); ++it) {
|
|
182
|
+
int x = (*it - '0') * base + carry;
|
|
183
|
+
*it = static_cast<char>('0' + x % 10);
|
|
184
|
+
carry = x / 10;
|
|
185
|
+
}
|
|
186
|
+
while (carry > 0) {
|
|
187
|
+
decimal.insert(decimal.begin(), static_cast<char>('0' + carry % 10));
|
|
188
|
+
carry /= 10;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
d.digits = decimal;
|
|
192
|
+
d.point = static_cast<int>(decimal.size());
|
|
193
|
+
normalize(d);
|
|
194
|
+
return d;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
size_t i = 0;
|
|
198
|
+
if (text[i] == '+' || text[i] == '-') {
|
|
199
|
+
d.negative = text[i] == '-';
|
|
200
|
+
i++;
|
|
201
|
+
}
|
|
202
|
+
if (text.substr(i) == "Infinity") {
|
|
203
|
+
d.kind = Kind::Infinity;
|
|
204
|
+
return d;
|
|
205
|
+
}
|
|
206
|
+
bool anyDigit = false;
|
|
207
|
+
bool afterPoint = false;
|
|
208
|
+
int integerDigits = 0;
|
|
209
|
+
for (; i < text.size(); i++) {
|
|
210
|
+
const char c = text[i];
|
|
211
|
+
if (c >= '0' && c <= '9') {
|
|
212
|
+
d.digits.push_back(c);
|
|
213
|
+
if (!afterPoint) integerDigits++;
|
|
214
|
+
anyDigit = true;
|
|
215
|
+
} else if (c == '.' && !afterPoint) {
|
|
216
|
+
afterPoint = true;
|
|
217
|
+
} else {
|
|
218
|
+
break;
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
if (!anyDigit) return nan;
|
|
222
|
+
long exponent = 0;
|
|
223
|
+
if (i < text.size()) {
|
|
224
|
+
if (text[i] != 'e' && text[i] != 'E') return nan;
|
|
225
|
+
i++;
|
|
226
|
+
bool negativeExponent = false;
|
|
227
|
+
if (i < text.size() && (text[i] == '+' || text[i] == '-')) {
|
|
228
|
+
negativeExponent = text[i] == '-';
|
|
229
|
+
i++;
|
|
230
|
+
}
|
|
231
|
+
if (i >= text.size()) return nan;
|
|
232
|
+
for (; i < text.size(); i++) {
|
|
233
|
+
if (text[i] < '0' || text[i] > '9') return nan;
|
|
234
|
+
if (exponent < 100000) exponent = exponent * 10 + (text[i] - '0');
|
|
235
|
+
}
|
|
236
|
+
if (negativeExponent) exponent = -exponent;
|
|
237
|
+
}
|
|
238
|
+
d.point = integerDigits + static_cast<int>(exponent);
|
|
239
|
+
normalize(d);
|
|
240
|
+
return d;
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
// MARK: - Rounding
|
|
244
|
+
|
|
245
|
+
namespace {
|
|
246
|
+
|
|
247
|
+
/// Whether the digits dropped past `keep` are below, exactly at, or above half a unit.
|
|
248
|
+
enum class Remainder { Zero, BelowHalf, Half, AboveHalf };
|
|
249
|
+
|
|
250
|
+
Remainder remainderAfter(const std::string& digits, int keep) {
|
|
251
|
+
if (keep >= static_cast<int>(digits.size())) return Remainder::Zero;
|
|
252
|
+
if (keep < 0) return Remainder::BelowHalf; // the first dropped digit is a leading zero
|
|
253
|
+
const char first = digits[static_cast<size_t>(keep)];
|
|
254
|
+
// Trailing zeros are stripped, so anything after the first dropped digit is non-zero.
|
|
255
|
+
const bool more = keep + 1 < static_cast<int>(digits.size());
|
|
256
|
+
if (first > '5') return Remainder::AboveHalf;
|
|
257
|
+
if (first == '5') return more ? Remainder::AboveHalf : Remainder::Half;
|
|
258
|
+
return first == '0' && !more ? Remainder::Zero : Remainder::BelowHalf;
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
bool roundsAway(RoundingMode mode, bool negative, Remainder remainder, bool lastOdd) {
|
|
262
|
+
if (remainder == Remainder::Zero) return false;
|
|
263
|
+
switch (mode) {
|
|
264
|
+
case RoundingMode::Ceil:
|
|
265
|
+
return !negative;
|
|
266
|
+
case RoundingMode::Floor:
|
|
267
|
+
return negative;
|
|
268
|
+
case RoundingMode::Expand:
|
|
269
|
+
return true;
|
|
270
|
+
case RoundingMode::Trunc:
|
|
271
|
+
return false;
|
|
272
|
+
default:
|
|
273
|
+
break;
|
|
274
|
+
}
|
|
275
|
+
if (remainder == Remainder::AboveHalf) return true;
|
|
276
|
+
if (remainder == Remainder::BelowHalf) return false;
|
|
277
|
+
switch (mode) {
|
|
278
|
+
case RoundingMode::HalfCeil:
|
|
279
|
+
return !negative;
|
|
280
|
+
case RoundingMode::HalfFloor:
|
|
281
|
+
return negative;
|
|
282
|
+
case RoundingMode::HalfExpand:
|
|
283
|
+
return true;
|
|
284
|
+
case RoundingMode::HalfTrunc:
|
|
285
|
+
return false;
|
|
286
|
+
case RoundingMode::HalfEven:
|
|
287
|
+
return lastOdd;
|
|
288
|
+
default:
|
|
289
|
+
return true;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/// Keeps the digits worth at least 10^magnitude and rounds the rest away,
|
|
294
|
+
/// in steps of `increment` units of 10^magnitude.
|
|
295
|
+
Decimal roundAt(const Decimal& value, int magnitude, RoundingMode mode, int increment) {
|
|
296
|
+
Decimal out = value;
|
|
297
|
+
const int keep = value.point - magnitude; // digits kept, counted from the first
|
|
298
|
+
if (keep >= static_cast<int>(value.digits.size()) && increment == 1) return out;
|
|
299
|
+
|
|
300
|
+
// The kept part as an integer K (units of 10^magnitude) and the dropped part.
|
|
301
|
+
std::string kept = keep > 0 ? value.digits.substr(0, static_cast<size_t>(std::min<int>(keep, static_cast<int>(value.digits.size())))) : "";
|
|
302
|
+
if (keep > static_cast<int>(value.digits.size())) kept.append(static_cast<size_t>(keep) - value.digits.size(), '0');
|
|
303
|
+
const Remainder dropped = remainderAfter(value.digits, keep);
|
|
304
|
+
|
|
305
|
+
// The last five digits of K decide everything an increment (a divisor of 10000) needs.
|
|
306
|
+
const size_t tailLength = std::min<size_t>(kept.size(), 5);
|
|
307
|
+
int64_t tail = 0;
|
|
308
|
+
for (size_t i = kept.size() - tailLength; i < kept.size(); i++) tail = tail * 10 + (kept[i] - '0');
|
|
309
|
+
|
|
310
|
+
const int64_t rem = tail % increment; // K mod increment
|
|
311
|
+
int64_t lower = tail - rem; // the multiple of increment at or below K (tail part)
|
|
312
|
+
bool up;
|
|
313
|
+
if (increment == 1) {
|
|
314
|
+
up = roundsAway(mode, value.negative, dropped, (tail % 2) != 0);
|
|
315
|
+
} else {
|
|
316
|
+
// Where K + dropped sits between lower and lower + increment.
|
|
317
|
+
Remainder position;
|
|
318
|
+
if (rem == 0 && dropped == Remainder::Zero) {
|
|
319
|
+
position = Remainder::Zero;
|
|
320
|
+
} else {
|
|
321
|
+
// Compare 2 × (rem + dropped) with increment.
|
|
322
|
+
const int64_t t = 2 * rem - increment;
|
|
323
|
+
if (t > 0) position = Remainder::AboveHalf;
|
|
324
|
+
else if (t == 0) position = dropped == Remainder::Zero ? Remainder::Half : Remainder::AboveHalf;
|
|
325
|
+
else if (t == -1) position = dropped == Remainder::Zero ? Remainder::BelowHalf : dropped;
|
|
326
|
+
else position = Remainder::BelowHalf;
|
|
327
|
+
}
|
|
328
|
+
up = roundsAway(mode, value.negative, position, ((lower / increment) % 2) != 0);
|
|
329
|
+
}
|
|
330
|
+
int64_t newTail = up ? lower + increment : lower;
|
|
331
|
+
|
|
332
|
+
// Write the new tail back, carrying into the digits before it.
|
|
333
|
+
std::string head = kept.substr(0, kept.size() - tailLength);
|
|
334
|
+
if (head.empty()) {
|
|
335
|
+
out.digits = std::to_string(newTail);
|
|
336
|
+
out.point = magnitude + static_cast<int>(out.digits.size());
|
|
337
|
+
normalize(out);
|
|
338
|
+
return out;
|
|
339
|
+
}
|
|
340
|
+
std::string tailText = std::to_string(newTail);
|
|
341
|
+
if (tailText.size() < tailLength) tailText.insert(0, tailLength - tailText.size(), '0');
|
|
342
|
+
if (tailText.size() > tailLength) {
|
|
343
|
+
// A carry out of the tail: add 1 to the head.
|
|
344
|
+
tailText.erase(0, 1);
|
|
345
|
+
int i = static_cast<int>(head.size()) - 1;
|
|
346
|
+
while (i >= 0 && head[static_cast<size_t>(i)] == '9') head[static_cast<size_t>(i--)] = '0';
|
|
347
|
+
if (i >= 0) head[static_cast<size_t>(i)]++;
|
|
348
|
+
else head.insert(head.begin(), '1');
|
|
349
|
+
}
|
|
350
|
+
out.digits = head + tailText;
|
|
351
|
+
out.point = magnitude + static_cast<int>(out.digits.size());
|
|
352
|
+
normalize(out);
|
|
353
|
+
return out;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/// The power of ten of the leading digit (0 for zero).
|
|
357
|
+
int magnitudeOf(const Decimal& d) { return d.digits.empty() ? 0 : d.point - 1; }
|
|
358
|
+
|
|
359
|
+
struct Rounded {
|
|
360
|
+
Decimal value;
|
|
361
|
+
int minimumFraction;
|
|
362
|
+
int roundingMagnitude;
|
|
363
|
+
};
|
|
364
|
+
|
|
365
|
+
Rounded roundFraction(const Decimal& value, const Rounding& r) {
|
|
366
|
+
Decimal out = roundAt(value, -r.maximumFractionDigits, r.mode, r.increment);
|
|
367
|
+
return {out, r.minimumFractionDigits, -r.maximumFractionDigits};
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
Rounded roundSignificant(const Decimal& value, const Rounding& r) {
|
|
371
|
+
const int mag = magnitudeOf(value);
|
|
372
|
+
Decimal out = roundAt(value, mag - r.maximumSignificantDigits + 1, r.mode, 1);
|
|
373
|
+
// Rounding up can add a digit in front (9.99 → 10.0): keep the same number of significant digits.
|
|
374
|
+
const int outMag = magnitudeOf(out);
|
|
375
|
+
const int minimumFraction = std::max(0, r.minimumSignificantDigits - outMag - 1);
|
|
376
|
+
return {out, minimumFraction, mag - r.maximumSignificantDigits + 1};
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
} // namespace
|
|
380
|
+
|
|
381
|
+
Decimal roundDecimal(const Decimal& value, const Rounding& r, int& fractionDigits) {
|
|
382
|
+
Rounded result;
|
|
383
|
+
if (r.useSignificant && (!r.useFraction || r.priority == RoundingPriority::Auto)) {
|
|
384
|
+
result = roundSignificant(value, r);
|
|
385
|
+
} else if (!r.useSignificant) {
|
|
386
|
+
result = roundFraction(value, r);
|
|
387
|
+
} else {
|
|
388
|
+
const Rounded s = roundSignificant(value, r);
|
|
389
|
+
const Rounded f = roundFraction(value, r);
|
|
390
|
+
const bool pickSignificant = r.priority == RoundingPriority::MorePrecision ? s.roundingMagnitude <= f.roundingMagnitude
|
|
391
|
+
: s.roundingMagnitude > f.roundingMagnitude;
|
|
392
|
+
result = pickSignificant ? s : f;
|
|
393
|
+
}
|
|
394
|
+
const int shown = std::max(0, static_cast<int>(result.value.digits.size()) - result.value.point);
|
|
395
|
+
fractionDigits = std::max(shown, result.minimumFraction);
|
|
396
|
+
if (r.trailingZeroDisplay == TrailingZeroDisplay::StripIfInteger && shown == 0) fractionDigits = 0;
|
|
397
|
+
return result.value;
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
int decimalMagnitude(const Decimal& value) { return magnitudeOf(value); }
|
|
401
|
+
|
|
402
|
+
int currencyDigits(std::string_view code) {
|
|
403
|
+
struct Entry {
|
|
404
|
+
const char* code;
|
|
405
|
+
int digits;
|
|
406
|
+
};
|
|
407
|
+
// ISO 4217 currencies whose minor unit is not 2.
|
|
408
|
+
static constexpr Entry kDigits[] = {
|
|
409
|
+
{"BHD", 3}, {"BIF", 0}, {"CLF", 4}, {"CLP", 0}, {"DJF", 0}, {"GNF", 0}, {"IQD", 3}, {"ISK", 0}, {"JOD", 3},
|
|
410
|
+
{"JPY", 0}, {"KMF", 0}, {"KRW", 0}, {"KWD", 3}, {"LYD", 3}, {"OMR", 3}, {"PYG", 0}, {"RWF", 0}, {"TND", 3},
|
|
411
|
+
{"UGX", 0}, {"UYI", 0}, {"UYW", 4}, {"VND", 0}, {"VUV", 0}, {"XAF", 0}, {"XOF", 0}, {"XPF", 0},
|
|
412
|
+
};
|
|
413
|
+
if (code.size() != 3) return 2;
|
|
414
|
+
char upper[3];
|
|
415
|
+
for (size_t i = 0; i < 3; i++) upper[i] = static_cast<char>(code[i] >= 'a' && code[i] <= 'z' ? code[i] - 32 : code[i]);
|
|
416
|
+
for (const auto& e : kDigits) {
|
|
417
|
+
if (std::string_view(e.code, 3) == std::string_view(upper, 3)) return e.digits;
|
|
418
|
+
}
|
|
419
|
+
return 2;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
// MARK: - Formatting
|
|
423
|
+
|
|
424
|
+
NumberFormatCore::NumberFormatCore(LocaleFormat format, Rounding rounding, Grouping grouping, SignDisplay signDisplay, Notation notation)
|
|
425
|
+
: format_(std::move(format)), rounding_(rounding), grouping_(grouping), signDisplay_(signDisplay), notation_(notation) {}
|
|
426
|
+
|
|
427
|
+
namespace {
|
|
428
|
+
|
|
429
|
+
/// A symbol without the direction marks around it: ICU's minus sign in ar is
|
|
430
|
+
/// ALM + "-", and `formatToParts` reports the mark as a literal of its own.
|
|
431
|
+
std::string_view trimBidi(std::string_view symbol) {
|
|
432
|
+
static constexpr std::string_view kMarks[] = {"", "", "", "", "", "",
|
|
433
|
+
"", "", "", "", "", ""};
|
|
434
|
+
bool trimmed = true;
|
|
435
|
+
while (trimmed && !symbol.empty()) {
|
|
436
|
+
trimmed = false;
|
|
437
|
+
for (auto mark : kMarks) {
|
|
438
|
+
if (symbol.size() > mark.size() && symbol.substr(0, mark.size()) == mark) {
|
|
439
|
+
symbol.remove_prefix(mark.size());
|
|
440
|
+
trimmed = true;
|
|
441
|
+
}
|
|
442
|
+
if (symbol.size() > mark.size() && symbol.substr(symbol.size() - mark.size()) == mark) {
|
|
443
|
+
symbol.remove_suffix(mark.size());
|
|
444
|
+
trimmed = true;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
return symbol;
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
} // namespace
|
|
452
|
+
|
|
453
|
+
std::vector<Part> NumberFormatCore::splitAffix(std::string_view text, std::string_view currencySymbol, std::string_view minusSign,
|
|
454
|
+
std::string_view plusSign, std::string_view percentSign) {
|
|
455
|
+
const std::string_view currency = trimBidi(currencySymbol);
|
|
456
|
+
const std::string_view minus = trimBidi(minusSign);
|
|
457
|
+
const std::string_view plus = trimBidi(plusSign);
|
|
458
|
+
const std::string_view percent = trimBidi(percentSign);
|
|
459
|
+
std::vector<Part> parts;
|
|
460
|
+
std::string literal;
|
|
461
|
+
auto flush = [&] {
|
|
462
|
+
if (!literal.empty()) parts.push_back({PartType::Literal, std::move(literal)});
|
|
463
|
+
literal.clear();
|
|
464
|
+
};
|
|
465
|
+
size_t i = 0;
|
|
466
|
+
while (i < text.size()) {
|
|
467
|
+
// The longest symbol first, so "US$" is not split around a "$" minus look-alike.
|
|
468
|
+
struct Candidate {
|
|
469
|
+
std::string_view symbol;
|
|
470
|
+
PartType type;
|
|
471
|
+
};
|
|
472
|
+
const Candidate candidates[] = {{currency, PartType::Currency}, {minus, PartType::MinusSign}, {plus, PartType::PlusSign}, {percent, PartType::PercentSign}};
|
|
473
|
+
const Candidate* best = nullptr;
|
|
474
|
+
for (const auto& c : candidates) {
|
|
475
|
+
if (!c.symbol.empty() && text.substr(i, c.symbol.size()) == c.symbol && (!best || c.symbol.size() > best->symbol.size())) best = &c;
|
|
476
|
+
}
|
|
477
|
+
if (best) {
|
|
478
|
+
flush();
|
|
479
|
+
parts.push_back({best->type, std::string(best->symbol)});
|
|
480
|
+
i += best->symbol.size();
|
|
481
|
+
} else {
|
|
482
|
+
literal.push_back(text[i++]);
|
|
483
|
+
}
|
|
484
|
+
}
|
|
485
|
+
flush();
|
|
486
|
+
return parts;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
std::vector<Part> NumberFormatCore::formatToParts(const Decimal& input) const {
|
|
490
|
+
Decimal value = input;
|
|
491
|
+
if (value.kind == Decimal::Kind::Finite && !value.digits.empty()) value.point += format_.scale;
|
|
492
|
+
|
|
493
|
+
int fractionDigits = 0;
|
|
494
|
+
Decimal rounded = value;
|
|
495
|
+
int exponent = 0;
|
|
496
|
+
const bool scientific = notation_ != Notation::Standard;
|
|
497
|
+
if (value.kind == Decimal::Kind::Finite) {
|
|
498
|
+
if (scientific && !value.digits.empty()) {
|
|
499
|
+
// ECMA-402 ComputeExponent: the exponent of the magnitude (a multiple of
|
|
500
|
+
// three for engineering), again if rounding the mantissa carried (9.99 → 10).
|
|
501
|
+
auto exponentFor = [&](int magnitude) {
|
|
502
|
+
if (notation_ == Notation::Scientific) return magnitude;
|
|
503
|
+
return magnitude >= 0 ? magnitude / 3 * 3 : -((-magnitude + 2) / 3) * 3;
|
|
504
|
+
};
|
|
505
|
+
const int magnitude = magnitudeOf(value);
|
|
506
|
+
exponent = exponentFor(magnitude);
|
|
507
|
+
Decimal mantissa = value;
|
|
508
|
+
mantissa.point -= exponent;
|
|
509
|
+
rounded = roundDecimal(mantissa, rounding_, fractionDigits);
|
|
510
|
+
if (!rounded.digits.empty() && magnitudeOf(rounded) + exponent > magnitude) {
|
|
511
|
+
const int carried = exponentFor(magnitudeOf(rounded) + exponent);
|
|
512
|
+
if (carried != exponent) {
|
|
513
|
+
exponent = carried;
|
|
514
|
+
mantissa = value;
|
|
515
|
+
mantissa.point -= exponent;
|
|
516
|
+
rounded = roundDecimal(mantissa, rounding_, fractionDigits);
|
|
517
|
+
}
|
|
518
|
+
}
|
|
519
|
+
if (rounded.digits.empty()) exponent = 0;
|
|
520
|
+
} else {
|
|
521
|
+
rounded = roundDecimal(value, rounding_, fractionDigits);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
const bool isZero = rounded.kind == Decimal::Kind::Finite && rounded.digits.empty();
|
|
525
|
+
const bool negative = value.kind != Decimal::Kind::NaN && value.negative;
|
|
526
|
+
|
|
527
|
+
// Which sign the number wears (ECMA-402 GetNumberFormatPattern).
|
|
528
|
+
enum class Sign { None, Minus, Plus } sign = Sign::None;
|
|
529
|
+
switch (signDisplay_) {
|
|
530
|
+
case SignDisplay::Auto:
|
|
531
|
+
sign = negative ? Sign::Minus : Sign::None;
|
|
532
|
+
break;
|
|
533
|
+
case SignDisplay::Never:
|
|
534
|
+
sign = Sign::None;
|
|
535
|
+
break;
|
|
536
|
+
case SignDisplay::Always:
|
|
537
|
+
sign = negative ? Sign::Minus : Sign::Plus;
|
|
538
|
+
break;
|
|
539
|
+
case SignDisplay::ExceptZero:
|
|
540
|
+
if (value.kind == Decimal::Kind::NaN || isZero) sign = Sign::None;
|
|
541
|
+
else sign = negative ? Sign::Minus : Sign::Plus;
|
|
542
|
+
break;
|
|
543
|
+
case SignDisplay::Negative:
|
|
544
|
+
sign = negative && !isZero ? Sign::Minus : Sign::None;
|
|
545
|
+
break;
|
|
546
|
+
}
|
|
547
|
+
const auto& prefix = sign == Sign::Minus ? format_.negativePrefix : sign == Sign::Plus ? format_.plusPrefix : format_.positivePrefix;
|
|
548
|
+
const auto& suffix = sign == Sign::Minus ? format_.negativeSuffix : sign == Sign::Plus ? format_.plusSuffix : format_.positiveSuffix;
|
|
549
|
+
|
|
550
|
+
std::vector<Part> parts;
|
|
551
|
+
parts.reserve(prefix.size() + suffix.size() + 8);
|
|
552
|
+
parts.insert(parts.end(), prefix.begin(), prefix.end());
|
|
553
|
+
|
|
554
|
+
if (value.kind == Decimal::Kind::NaN) {
|
|
555
|
+
parts.push_back({PartType::Nan, format_.nan});
|
|
556
|
+
} else if (value.kind == Decimal::Kind::Infinity) {
|
|
557
|
+
parts.push_back({PartType::Infinity, format_.infinity});
|
|
558
|
+
} else {
|
|
559
|
+
// Integer digits, padded to minimumIntegerDigits.
|
|
560
|
+
std::string integer;
|
|
561
|
+
for (int i = 0; i < std::max(rounded.point, 0); i++) integer.push_back(i < static_cast<int>(rounded.digits.size()) ? rounded.digits[static_cast<size_t>(i)] : '0');
|
|
562
|
+
if (integer.empty()) integer = "0";
|
|
563
|
+
if (static_cast<int>(integer.size()) < rounding_.minimumIntegerDigits) integer.insert(0, static_cast<size_t>(rounding_.minimumIntegerDigits) - integer.size(), '0');
|
|
564
|
+
|
|
565
|
+
const int length = static_cast<int>(integer.size());
|
|
566
|
+
const int primary = std::max(1, format_.primaryGroupingSize);
|
|
567
|
+
const int secondary = std::max(1, format_.secondaryGroupingSize);
|
|
568
|
+
int minimumGrouping = 1;
|
|
569
|
+
switch (grouping_) {
|
|
570
|
+
case Grouping::Off:
|
|
571
|
+
minimumGrouping = 1 << 20;
|
|
572
|
+
break;
|
|
573
|
+
case Grouping::Min2:
|
|
574
|
+
minimumGrouping = std::max(2, format_.minimumGroupingDigits);
|
|
575
|
+
break;
|
|
576
|
+
case Grouping::Auto:
|
|
577
|
+
minimumGrouping = format_.minimumGroupingDigits;
|
|
578
|
+
break;
|
|
579
|
+
case Grouping::Always:
|
|
580
|
+
minimumGrouping = 1;
|
|
581
|
+
break;
|
|
582
|
+
}
|
|
583
|
+
const bool group = !scientific && !format_.groupingSeparator.empty() && format_.primaryGroupingSize > 0 && length >= primary + minimumGrouping;
|
|
584
|
+
|
|
585
|
+
std::string run;
|
|
586
|
+
auto flushRun = [&] {
|
|
587
|
+
if (!run.empty()) parts.push_back({PartType::Integer, std::move(run)});
|
|
588
|
+
run.clear();
|
|
589
|
+
};
|
|
590
|
+
for (int i = 0; i < length; i++) {
|
|
591
|
+
run += format_.digits[static_cast<size_t>(integer[static_cast<size_t>(i)] - '0')];
|
|
592
|
+
const int left = length - 1 - i; // digits after this one
|
|
593
|
+
if (group && left > 0 && (left == primary || (left > primary && (left - primary) % secondary == 0))) {
|
|
594
|
+
flushRun();
|
|
595
|
+
parts.push_back({PartType::Group, format_.groupingSeparator});
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
flushRun();
|
|
599
|
+
|
|
600
|
+
if (fractionDigits > 0) {
|
|
601
|
+
parts.push_back({PartType::Decimal, format_.decimalSeparator});
|
|
602
|
+
std::string fraction;
|
|
603
|
+
for (int i = 0; i < fractionDigits; i++) {
|
|
604
|
+
const int index = rounded.point + i;
|
|
605
|
+
const char c = index >= 0 && index < static_cast<int>(rounded.digits.size()) ? rounded.digits[static_cast<size_t>(index)] : '0';
|
|
606
|
+
fraction += format_.digits[static_cast<size_t>(c - '0')];
|
|
607
|
+
}
|
|
608
|
+
parts.push_back({PartType::Fraction, std::move(fraction)});
|
|
609
|
+
}
|
|
610
|
+
if (scientific) {
|
|
611
|
+
parts.push_back({PartType::ExponentSeparator, format_.exponentSeparator});
|
|
612
|
+
if (exponent < 0) parts.push_back({PartType::ExponentMinusSign, format_.exponentMinusSign});
|
|
613
|
+
std::string digits;
|
|
614
|
+
for (char c : std::to_string(exponent < 0 ? -exponent : exponent)) digits += format_.digits[static_cast<size_t>(c - '0')];
|
|
615
|
+
parts.push_back({PartType::ExponentInteger, std::move(digits)});
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
parts.insert(parts.end(), suffix.begin(), suffix.end());
|
|
620
|
+
return parts;
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
std::string NumberFormatCore::format(const Decimal& value) const {
|
|
624
|
+
std::string out;
|
|
625
|
+
for (const auto& part : formatToParts(value)) out += part.value;
|
|
626
|
+
return out;
|
|
627
|
+
}
|
|
628
|
+
|
|
629
|
+
} // namespace margelo::nitro::nitroinput::numberformat
|