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.
Files changed (164) hide show
  1. package/CHANGELOG.md +38 -0
  2. package/README.md +43 -1
  3. package/android/CMakeLists.txt +4 -0
  4. package/android/gradle.properties +1 -1
  5. package/android/src/main/cpp/JRollingEngine.cpp +3 -2
  6. package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroInputView.kt +20 -0
  7. package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroNumberFormatPlatform.kt +209 -0
  8. package/android/src/main/java/com/margelo/nitro/nitroinput/HybridNitroNumberView.kt +25 -0
  9. package/android/src/main/java/com/margelo/nitro/nitroinput/NitroInputView.kt +69 -10
  10. package/android/src/main/java/com/margelo/nitro/nitroinput/NitroNumberView.kt +164 -24
  11. package/android/src/main/java/com/margelo/nitro/nitroinput/RollingEngine.kt +2 -1
  12. package/cpp/RollingEngine.cpp +12 -2
  13. package/cpp/RollingEngine.hpp +8 -1
  14. package/cpp/numberformat/HybridNitroNumberFormat.cpp +498 -0
  15. package/cpp/numberformat/HybridNitroNumberFormat.hpp +74 -0
  16. package/cpp/numberformat/HybridNitroNumberFormatFactory.hpp +11 -0
  17. package/cpp/numberformat/NumberFormatCore.cpp +629 -0
  18. package/cpp/numberformat/NumberFormatCore.hpp +154 -0
  19. package/cpp/numberformat/NumberFormatProbe.cpp +354 -0
  20. package/cpp/numberformat/NumberFormatProbe.hpp +63 -0
  21. package/ios/HybridNitroInputView.swift +15 -0
  22. package/ios/HybridNitroNumberFormatPlatform.swift +348 -0
  23. package/ios/HybridNitroNumberView.swift +19 -0
  24. package/ios/NitroInputView.swift +79 -19
  25. package/ios/NitroNumberView.swift +171 -31
  26. package/lib/commonjs/nitrogen/generated/shared/json/NitroInputViewConfig.json +5 -0
  27. package/lib/commonjs/nitrogen/generated/shared/json/NitroNumberViewConfig.json +6 -0
  28. package/lib/commonjs/src/NitroInput.js +8 -6
  29. package/lib/commonjs/src/NitroNumber.js +4 -2
  30. package/lib/commonjs/src/NumberFormat.js +525 -0
  31. package/lib/commonjs/src/formatProps.js +48 -0
  32. package/lib/commonjs/src/index.js +3 -1
  33. package/lib/commonjs/src/specs/NumberFormat.nitro.js +2 -0
  34. package/lib/module/nitrogen/generated/shared/json/NitroInputViewConfig.json +5 -0
  35. package/lib/module/nitrogen/generated/shared/json/NitroNumberViewConfig.json +6 -0
  36. package/lib/module/src/NitroInput.js +8 -6
  37. package/lib/module/src/NitroNumber.js +4 -2
  38. package/lib/module/src/NumberFormat.js +521 -0
  39. package/lib/module/src/formatProps.js +45 -0
  40. package/lib/module/src/index.js +1 -0
  41. package/lib/module/src/specs/NumberFormat.nitro.js +1 -0
  42. package/lib/typescript/src/NitroInput.d.ts +22 -0
  43. package/lib/typescript/src/NitroNumber.d.ts +36 -1
  44. package/lib/typescript/src/NumberFormat.d.ts +47 -0
  45. package/lib/typescript/src/formatProps.d.ts +21 -0
  46. package/lib/typescript/src/index.d.ts +2 -0
  47. package/lib/typescript/src/specs/NitroInput.nitro.d.ts +10 -0
  48. package/lib/typescript/src/specs/NitroNumber.nitro.d.ts +13 -1
  49. package/lib/typescript/src/specs/NumberFormat.nitro.d.ts +139 -0
  50. package/nitro.json +16 -0
  51. package/nitrogen/generated/android/NitroInput+autolinking.cmake +6 -0
  52. package/nitrogen/generated/android/NitroInputOnLoad.cpp +28 -0
  53. package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.cpp +45 -0
  54. package/nitrogen/generated/android/c++/JHybridNitroInputViewSpec.hpp +10 -0
  55. package/nitrogen/generated/android/c++/JHybridNitroNumberFormatPlatformSpec.cpp +118 -0
  56. package/nitrogen/generated/android/c++/JHybridNitroNumberFormatPlatformSpec.hpp +64 -0
  57. package/nitrogen/generated/android/c++/JHybridNitroNumberViewSpec.cpp +54 -0
  58. package/nitrogen/generated/android/c++/JHybridNitroNumberViewSpec.hpp +12 -0
  59. package/nitrogen/generated/android/c++/JHybridNitroPlatformNumberFormatterSpec.cpp +65 -0
  60. package/nitrogen/generated/android/c++/JHybridNitroPlatformNumberFormatterSpec.hpp +64 -0
  61. package/nitrogen/generated/android/c++/JNitroNumberDirection.hpp +3 -0
  62. package/nitrogen/generated/android/c++/JNumberFormatCompactDisplay.hpp +58 -0
  63. package/nitrogen/generated/android/c++/JNumberFormatCurrencyDisplay.hpp +64 -0
  64. package/nitrogen/generated/android/c++/JNumberFormatCurrencySign.hpp +58 -0
  65. package/nitrogen/generated/android/c++/JNumberFormatNotation.hpp +64 -0
  66. package/nitrogen/generated/android/c++/JNumberFormatPlatformOptions.hpp +161 -0
  67. package/nitrogen/generated/android/c++/JNumberFormatPlatformSymbols.hpp +89 -0
  68. package/nitrogen/generated/android/c++/JNumberFormatRoundingMode.hpp +79 -0
  69. package/nitrogen/generated/android/c++/JNumberFormatSignDisplay.hpp +67 -0
  70. package/nitrogen/generated/android/c++/JNumberFormatStyle.hpp +64 -0
  71. package/nitrogen/generated/android/c++/JNumberFormatUnitDisplay.hpp +61 -0
  72. package/nitrogen/generated/android/c++/views/JHybridNitroInputViewStateUpdater.cpp +25 -0
  73. package/nitrogen/generated/android/c++/views/JHybridNitroNumberViewStateUpdater.cpp +30 -0
  74. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroInputViewSpec.kt +30 -0
  75. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroNumberFormatPlatformSpec.kt +60 -0
  76. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroNumberViewSpec.kt +36 -0
  77. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/HybridNitroPlatformNumberFormatterSpec.kt +62 -0
  78. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NitroNumberDirection.kt +2 -1
  79. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatCompactDisplay.kt +23 -0
  80. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatCurrencyDisplay.kt +25 -0
  81. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatCurrencySign.kt +23 -0
  82. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatNotation.kt +25 -0
  83. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatPlatformOptions.kt +136 -0
  84. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatPlatformSymbols.kt +91 -0
  85. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatRoundingMode.kt +30 -0
  86. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatSignDisplay.kt +26 -0
  87. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatStyle.kt +25 -0
  88. package/nitrogen/generated/android/kotlin/com/margelo/nitro/nitroinput/NumberFormatUnitDisplay.kt +24 -0
  89. package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.cpp +34 -0
  90. package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Bridge.hpp +63 -0
  91. package/nitrogen/generated/ios/NitroInput-Swift-Cxx-Umbrella.hpp +40 -0
  92. package/nitrogen/generated/ios/NitroInputAutolinking.mm +18 -0
  93. package/nitrogen/generated/ios/NitroInputAutolinking.swift +12 -0
  94. package/nitrogen/generated/ios/c++/HybridNitroInputViewSpecSwift.hpp +30 -0
  95. package/nitrogen/generated/ios/c++/HybridNitroNumberFormatPlatformSpecSwift.cpp +11 -0
  96. package/nitrogen/generated/ios/c++/HybridNitroNumberFormatPlatformSpecSwift.hpp +122 -0
  97. package/nitrogen/generated/ios/c++/HybridNitroNumberViewSpecSwift.hpp +42 -0
  98. package/nitrogen/generated/ios/c++/HybridNitroPlatformNumberFormatterSpecSwift.cpp +11 -0
  99. package/nitrogen/generated/ios/c++/HybridNitroPlatformNumberFormatterSpecSwift.hpp +94 -0
  100. package/nitrogen/generated/ios/c++/views/HybridNitroInputViewComponent.mm +30 -0
  101. package/nitrogen/generated/ios/c++/views/HybridNitroNumberViewComponent.mm +36 -0
  102. package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec.swift +5 -0
  103. package/nitrogen/generated/ios/swift/HybridNitroInputViewSpec_cxx.swift +55 -0
  104. package/nitrogen/generated/ios/swift/HybridNitroNumberFormatPlatformSpec.swift +56 -0
  105. package/nitrogen/generated/ios/swift/HybridNitroNumberFormatPlatformSpec_cxx.swift +159 -0
  106. package/nitrogen/generated/ios/swift/HybridNitroNumberViewSpec.swift +6 -0
  107. package/nitrogen/generated/ios/swift/HybridNitroNumberViewSpec_cxx.swift +144 -0
  108. package/nitrogen/generated/ios/swift/HybridNitroPlatformNumberFormatterSpec.swift +56 -0
  109. package/nitrogen/generated/ios/swift/HybridNitroPlatformNumberFormatterSpec_cxx.swift +155 -0
  110. package/nitrogen/generated/ios/swift/NitroNumberDirection.swift +4 -0
  111. package/nitrogen/generated/ios/swift/NumberFormatCompactDisplay.swift +40 -0
  112. package/nitrogen/generated/ios/swift/NumberFormatCurrencyDisplay.swift +48 -0
  113. package/nitrogen/generated/ios/swift/NumberFormatCurrencySign.swift +40 -0
  114. package/nitrogen/generated/ios/swift/NumberFormatNotation.swift +48 -0
  115. package/nitrogen/generated/ios/swift/NumberFormatPlatformOptions.swift +159 -0
  116. package/nitrogen/generated/ios/swift/NumberFormatPlatformSymbols.swift +69 -0
  117. package/nitrogen/generated/ios/swift/NumberFormatRoundingMode.swift +68 -0
  118. package/nitrogen/generated/ios/swift/NumberFormatSignDisplay.swift +52 -0
  119. package/nitrogen/generated/ios/swift/NumberFormatStyle.swift +48 -0
  120. package/nitrogen/generated/ios/swift/NumberFormatUnitDisplay.swift +44 -0
  121. package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.cpp +10 -0
  122. package/nitrogen/generated/shared/c++/HybridNitroInputViewSpec.hpp +10 -0
  123. package/nitrogen/generated/shared/c++/HybridNitroNumberFormatFactorySpec.cpp +22 -0
  124. package/nitrogen/generated/shared/c++/HybridNitroNumberFormatFactorySpec.hpp +70 -0
  125. package/nitrogen/generated/shared/c++/HybridNitroNumberFormatPlatformSpec.cpp +22 -0
  126. package/nitrogen/generated/shared/c++/HybridNitroNumberFormatPlatformSpec.hpp +70 -0
  127. package/nitrogen/generated/shared/c++/HybridNitroNumberFormatSpec.cpp +23 -0
  128. package/nitrogen/generated/shared/c++/HybridNitroNumberFormatSpec.hpp +71 -0
  129. package/nitrogen/generated/shared/c++/HybridNitroNumberViewSpec.cpp +12 -0
  130. package/nitrogen/generated/shared/c++/HybridNitroNumberViewSpec.hpp +12 -0
  131. package/nitrogen/generated/shared/c++/HybridNitroPlatformNumberFormatterSpec.cpp +23 -0
  132. package/nitrogen/generated/shared/c++/HybridNitroPlatformNumberFormatterSpec.hpp +65 -0
  133. package/nitrogen/generated/shared/c++/NitroNumberDirection.hpp +4 -0
  134. package/nitrogen/generated/shared/c++/NumberFormatCompactDisplay.hpp +76 -0
  135. package/nitrogen/generated/shared/c++/NumberFormatCurrencyDisplay.hpp +84 -0
  136. package/nitrogen/generated/shared/c++/NumberFormatCurrencySign.hpp +76 -0
  137. package/nitrogen/generated/shared/c++/NumberFormatGrouping.hpp +80 -0
  138. package/nitrogen/generated/shared/c++/NumberFormatNotation.hpp +84 -0
  139. package/nitrogen/generated/shared/c++/NumberFormatOptions.hpp +193 -0
  140. package/nitrogen/generated/shared/c++/NumberFormatPart.hpp +87 -0
  141. package/nitrogen/generated/shared/c++/NumberFormatPlatformOptions.hpp +176 -0
  142. package/nitrogen/generated/shared/c++/NumberFormatPlatformSymbols.hpp +115 -0
  143. package/nitrogen/generated/shared/c++/NumberFormatRoundingMode.hpp +104 -0
  144. package/nitrogen/generated/shared/c++/NumberFormatRoundingPriority.hpp +80 -0
  145. package/nitrogen/generated/shared/c++/NumberFormatSignDisplay.hpp +88 -0
  146. package/nitrogen/generated/shared/c++/NumberFormatStyle.hpp +84 -0
  147. package/nitrogen/generated/shared/c++/NumberFormatTrailingZeroDisplay.hpp +76 -0
  148. package/nitrogen/generated/shared/c++/NumberFormatUnitDisplay.hpp +80 -0
  149. package/nitrogen/generated/shared/c++/ResolvedNumberFormatOptions.hpp +197 -0
  150. package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.cpp +10 -0
  151. package/nitrogen/generated/shared/c++/views/HybridNitroInputViewComponent.hpp +15 -0
  152. package/nitrogen/generated/shared/c++/views/HybridNitroNumberViewComponent.cpp +12 -0
  153. package/nitrogen/generated/shared/c++/views/HybridNitroNumberViewComponent.hpp +18 -0
  154. package/nitrogen/generated/shared/json/NitroInputViewConfig.json +5 -0
  155. package/nitrogen/generated/shared/json/NitroNumberViewConfig.json +6 -0
  156. package/package.json +2 -2
  157. package/src/NitroInput.tsx +42 -7
  158. package/src/NitroNumber.tsx +57 -7
  159. package/src/NumberFormat.ts +563 -0
  160. package/src/formatProps.ts +59 -0
  161. package/src/index.ts +18 -0
  162. package/src/specs/NitroInput.nitro.ts +10 -0
  163. package/src/specs/NitroNumber.nitro.ts +13 -1
  164. package/src/specs/NumberFormat.nitro.ts +145 -0
@@ -0,0 +1,154 @@
1
+ //
2
+ // NumberFormatCore.hpp
3
+ // NitroInput
4
+ //
5
+ // The formatting half of `NumberFormat`: turns a number into text and parts
6
+ // the way ECMA-402's `Intl.NumberFormat` does, from a locale's format that
7
+ // was learned once from the platform formatter (see `NumberFormatProbe`).
8
+ // Everything here is plain C++ with no platform or JSI dependency, so it is
9
+ // exact, thread-safe once built, and unit-tested on the host.
10
+ //
11
+ // The number is handled as decimal digits, never as a binary double after
12
+ // parsing: a double is read as its shortest round-trip decimal (what ICU and
13
+ // V8 do), so 1.005 rounds to 1.01 as it does in `Intl`, not 1.00.
14
+ //
15
+
16
+ #pragma once
17
+
18
+ #include <array>
19
+ #include <cstdint>
20
+ #include <string>
21
+ #include <string_view>
22
+ #include <vector>
23
+
24
+ namespace margelo::nitro::nitroinput::numberformat {
25
+
26
+ enum class PartType {
27
+ Literal,
28
+ Integer,
29
+ Group,
30
+ Decimal,
31
+ Fraction,
32
+ MinusSign,
33
+ PlusSign,
34
+ PercentSign,
35
+ Currency,
36
+ Nan,
37
+ Infinity,
38
+ Compact,
39
+ Unit,
40
+ ExponentSeparator,
41
+ ExponentMinusSign,
42
+ ExponentInteger,
43
+ };
44
+
45
+ /// The `type` string `formatToParts` reports for a part.
46
+ const char* partTypeName(PartType type);
47
+
48
+ struct Part {
49
+ PartType type;
50
+ std::string value;
51
+ };
52
+
53
+ enum class RoundingMode { Ceil, Floor, Expand, Trunc, HalfCeil, HalfFloor, HalfExpand, HalfTrunc, HalfEven };
54
+ enum class RoundingPriority { Auto, MorePrecision, LessPrecision };
55
+ enum class SignDisplay { Auto, Never, Always, ExceptZero, Negative };
56
+ enum class Grouping { Off, Min2, Auto, Always };
57
+ enum class TrailingZeroDisplay { Auto, StripIfInteger };
58
+ enum class Notation { Standard, Scientific, Engineering };
59
+
60
+ /// How a number is rounded and padded: the digit options of `Intl.NumberFormat`, resolved.
61
+ struct Rounding {
62
+ int minimumIntegerDigits = 1;
63
+ /// Fraction-digit rounding applies (it is off when only significant digits were given).
64
+ bool useFraction = true;
65
+ int minimumFractionDigits = 0;
66
+ int maximumFractionDigits = 3;
67
+ /// Significant-digit rounding applies.
68
+ bool useSignificant = false;
69
+ int minimumSignificantDigits = 1;
70
+ int maximumSignificantDigits = 21;
71
+ /// With both kinds on: which result wins (`roundingPriority`).
72
+ RoundingPriority priority = RoundingPriority::Auto;
73
+ /// 1, 2, 5, 10, 20, 25, 50, 100, … 5000, in units of the last fraction digit.
74
+ int increment = 1;
75
+ RoundingMode mode = RoundingMode::HalfExpand;
76
+ TrailingZeroDisplay trailingZeroDisplay = TrailingZeroDisplay::Auto;
77
+ };
78
+
79
+ /// A locale's format for one style and currency, as learned from the platform.
80
+ struct LocaleFormat {
81
+ /// The locale's digits 0…9 (Latin unless the numbering system says otherwise).
82
+ std::array<std::string, 10> digits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
83
+ std::string decimalSeparator = ".";
84
+ std::string groupingSeparator = ",";
85
+ int primaryGroupingSize = 3;
86
+ int secondaryGroupingSize = 3;
87
+ /// Digits needed before the first separator for `useGrouping: 'auto'` to group (2 in es, pl, …).
88
+ int minimumGroupingDigits = 1;
89
+ /// The text around the number, already split into parts.
90
+ std::vector<Part> positivePrefix, positiveSuffix;
91
+ std::vector<Part> negativePrefix, negativeSuffix;
92
+ std::vector<Part> plusPrefix, plusSuffix;
93
+ std::string nan = "NaN";
94
+ std::string infinity = "∞";
95
+ /// Powers of ten the value is scaled by before formatting (2 for percent).
96
+ int scale = 0;
97
+ /// Scientific notation: the "E" and the minus sign of a negative exponent.
98
+ std::string exponentSeparator = "E";
99
+ std::string exponentMinusSign = "-";
100
+ };
101
+
102
+ /// A number as decimal digits: `digits` with the decimal point after `point`
103
+ /// of them (`point` may be negative or past the end); no leading or trailing
104
+ /// zeros, and empty for zero.
105
+ struct Decimal {
106
+ enum class Kind { Finite, NaN, Infinity };
107
+ Kind kind = Kind::Finite;
108
+ bool negative = false;
109
+ std::string digits;
110
+ int point = 0;
111
+
112
+ bool isZero() const { return kind == Kind::Finite && digits.empty(); }
113
+
114
+ static Decimal fromDouble(double value);
115
+ /// A numeric string as `Intl` reads one: decimal with an optional exponent,
116
+ /// `Infinity`, or a 0x / 0o / 0b integer; anything else is NaN.
117
+ static Decimal fromString(std::string_view text);
118
+ static Decimal fromInt64(int64_t value);
119
+ };
120
+
121
+ class NumberFormatCore final {
122
+ public:
123
+ NumberFormatCore() = default;
124
+ NumberFormatCore(LocaleFormat format, Rounding rounding, Grouping grouping, SignDisplay signDisplay, Notation notation = Notation::Standard);
125
+
126
+ std::vector<Part> formatToParts(const Decimal& value) const;
127
+ std::string format(const Decimal& value) const;
128
+
129
+ const LocaleFormat& localeFormat() const { return format_; }
130
+ const Rounding& rounding() const { return rounding_; }
131
+
132
+ /// Splits `text` (a prefix or suffix) into currency, sign, percent and literal parts.
133
+ static std::vector<Part> splitAffix(std::string_view text, std::string_view currency, std::string_view minus, std::string_view plus,
134
+ std::string_view percent);
135
+
136
+ private:
137
+ LocaleFormat format_;
138
+ Rounding rounding_;
139
+ Grouping grouping_ = Grouping::Auto;
140
+ SignDisplay signDisplay_ = SignDisplay::Auto;
141
+ Notation notation_ = Notation::Standard;
142
+ };
143
+
144
+ /// Rounds `value` as `rounding` asks; the result's `point` and `digits` give the kept digits.
145
+ /// Exposed for tests. `fractionDigits` receives how many fraction digits to show (with padding).
146
+ Decimal roundDecimal(const Decimal& value, const Rounding& rounding, int& fractionDigits);
147
+
148
+ /// The power of ten of a decimal's leading digit (0 for zero).
149
+ int decimalMagnitude(const Decimal& value);
150
+
151
+ /// ISO 4217 minor-unit digits, as ECMA-402's CurrencyDigits: 2 unless listed.
152
+ int currencyDigits(std::string_view currencyCode);
153
+
154
+ } // namespace margelo::nitro::nitroinput::numberformat
@@ -0,0 +1,354 @@
1
+ //
2
+ // NumberFormatProbe.cpp
3
+ // NitroInput
4
+ //
5
+
6
+ #include "NumberFormatProbe.hpp"
7
+
8
+ #include <algorithm>
9
+ #include <cmath>
10
+ #include <limits>
11
+
12
+ namespace margelo::nitro::nitroinput::numberformat {
13
+
14
+ std::vector<std::string> codePoints(const std::string& text) {
15
+ std::vector<std::string> out;
16
+ out.reserve(text.size());
17
+ size_t i = 0;
18
+ while (i < text.size()) {
19
+ const auto c = static_cast<unsigned char>(text[i]);
20
+ size_t length = c < 0x80 ? 1 : (c >> 5) == 0x6 ? 2 : (c >> 4) == 0xE ? 3 : (c >> 3) == 0x1E ? 4 : 1;
21
+ if (i + length > text.size()) length = 1;
22
+ out.push_back(text.substr(i, length));
23
+ i += length;
24
+ }
25
+ return out;
26
+ }
27
+
28
+ namespace {
29
+
30
+ /// Direction marks some locales put around numbers and signs.
31
+ bool isBidiControl(const std::string& cp) {
32
+ return cp == "‎" || cp == "‏" || cp == "؜" || cp == "‪" || cp == "‫" || cp == "‬" || cp == "‭" ||
33
+ cp == "‮" || cp == "⁦" || cp == "⁧" || cp == "⁨" || cp == "⁩";
34
+ }
35
+
36
+ int digitValue(const std::string& cp, const std::array<std::string, 10>& digits) {
37
+ for (int d = 0; d < 10; d++) {
38
+ if (digits[static_cast<size_t>(d)] == cp) return d;
39
+ }
40
+ if (cp.size() == 1 && cp[0] >= '0' && cp[0] <= '9') return cp[0] - '0';
41
+ return -1;
42
+ }
43
+
44
+ struct Around {
45
+ std::string prefix;
46
+ std::vector<std::string> core; // from the first digit to the last, code points
47
+ std::string suffix;
48
+ bool hasDigits = false;
49
+ };
50
+
51
+ Around splitAround(const std::string& text, const std::array<std::string, 10>& digits) {
52
+ Around out;
53
+ const auto cps = codePoints(text);
54
+ int first = -1;
55
+ int last = -1;
56
+ for (int i = 0; i < static_cast<int>(cps.size()); i++) {
57
+ if (digitValue(cps[static_cast<size_t>(i)], digits) >= 0) {
58
+ if (first < 0) first = i;
59
+ last = i;
60
+ }
61
+ }
62
+ if (first < 0) {
63
+ out.prefix = text;
64
+ return out;
65
+ }
66
+ out.hasDigits = true;
67
+ for (int i = 0; i < first; i++) out.prefix += cps[static_cast<size_t>(i)];
68
+ for (int i = first; i <= last; i++) out.core.push_back(cps[static_cast<size_t>(i)]);
69
+ for (int i = last + 1; i < static_cast<int>(cps.size()); i++) out.suffix += cps[static_cast<size_t>(i)];
70
+ return out;
71
+ }
72
+
73
+ /// The core as alternating runs of digits and of separators.
74
+ struct Run {
75
+ bool digits;
76
+ std::string text;
77
+ int count; // digits in a digit run
78
+ };
79
+
80
+ std::vector<Run> runsOf(const std::vector<std::string>& core, const std::array<std::string, 10>& digits) {
81
+ std::vector<Run> runs;
82
+ for (const auto& cp : core) {
83
+ const bool isDigit = digitValue(cp, digits) >= 0;
84
+ if (runs.empty() || runs.back().digits != isDigit) runs.push_back({isDigit, "", 0});
85
+ runs.back().text += cp;
86
+ if (isDigit) runs.back().count++;
87
+ }
88
+ return runs;
89
+ }
90
+
91
+ } // namespace
92
+
93
+ std::array<std::string, 10> learnDigits(const std::string& formatted) {
94
+ std::vector<std::string> cps;
95
+ for (auto& cp : codePoints(formatted)) {
96
+ if (!isBidiControl(cp)) cps.push_back(cp);
97
+ }
98
+ std::array<std::string, 10> digits = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
99
+ if (cps.size() != 10) return digits;
100
+ // "1234567890": positions 0…8 are 1…9, position 9 is 0.
101
+ for (size_t i = 0; i < 9; i++) digits[i + 1] = cps[i];
102
+ digits[0] = cps[9];
103
+ return digits;
104
+ }
105
+
106
+ LocaleFormat learnLocaleFormat(const ProbeFormat& format, const std::array<std::string, 10>& digits, const ProbeSymbols& symbols, int scale) {
107
+ LocaleFormat out;
108
+ out.digits = digits;
109
+ out.scale = scale;
110
+ const double unit = std::pow(10.0, scale);
111
+
112
+ // 1,234,567.89: the separators, the grouping sizes and the positive affixes.
113
+ const Around positive = splitAround(format(1234567.89 / unit), digits);
114
+ const auto runs = runsOf(positive.core, digits);
115
+ // The last separator before the two fraction digits is the decimal separator.
116
+ size_t integerEnd = runs.size();
117
+ if (runs.size() >= 3 && runs.back().digits && runs.back().count == 2 && !runs[runs.size() - 2].digits) {
118
+ out.decimalSeparator = runs[runs.size() - 2].text;
119
+ integerEnd = runs.size() - 2;
120
+ }
121
+ std::vector<int> groups;
122
+ out.groupingSeparator.clear();
123
+ for (size_t i = 0; i < integerEnd; i++) {
124
+ if (runs[i].digits) groups.push_back(runs[i].count);
125
+ else if (out.groupingSeparator.empty()) out.groupingSeparator = runs[i].text;
126
+ }
127
+ if (groups.size() >= 2) {
128
+ out.primaryGroupingSize = groups.back();
129
+ out.secondaryGroupingSize = groups.size() >= 3 ? groups[groups.size() - 2] : groups.back();
130
+ } else {
131
+ out.primaryGroupingSize = 0;
132
+ out.secondaryGroupingSize = 0;
133
+ }
134
+
135
+ // 1234.5: grouped only when a single digit may stand before a separator.
136
+ if (!out.groupingSeparator.empty()) {
137
+ const Around small = splitAround(format(1234.5 / unit), digits);
138
+ const auto smallRuns = runsOf(small.core, digits);
139
+ const bool grouped = smallRuns.size() >= 2 && smallRuns[0].digits && smallRuns[0].count == 1 && smallRuns[1].text == out.groupingSeparator;
140
+ out.minimumGroupingDigits = grouped ? 1 : 2;
141
+ }
142
+
143
+ const Around negative = splitAround(format(-1234567.89 / unit), digits);
144
+
145
+ auto split = [&](const std::string& text) {
146
+ return NumberFormatCore::splitAffix(text, symbols.currency, symbols.minusSign, symbols.plusSign, symbols.percentSign);
147
+ };
148
+ out.positivePrefix = split(positive.prefix);
149
+ out.positiveSuffix = split(positive.suffix);
150
+ out.negativePrefix = split(negative.prefix);
151
+ out.negativeSuffix = split(negative.suffix);
152
+
153
+ // The plus pattern is the minus pattern with the sign swapped (ICU's rule);
154
+ // a minus pattern without a sign (accounting) gets the sign in front.
155
+ std::string plusPrefix = negative.prefix;
156
+ std::string plusSuffix = negative.suffix;
157
+ const auto swap = [&](std::string& text) {
158
+ const size_t at = symbols.minusSign.empty() ? std::string::npos : text.find(symbols.minusSign);
159
+ if (at == std::string::npos) return false;
160
+ text.replace(at, symbols.minusSign.size(), symbols.plusSign);
161
+ return true;
162
+ };
163
+ if (!swap(plusPrefix) && !swap(plusSuffix)) {
164
+ plusPrefix = symbols.plusSign + positive.prefix;
165
+ plusSuffix = positive.suffix;
166
+ }
167
+ out.plusPrefix = split(plusPrefix);
168
+ out.plusSuffix = split(plusSuffix);
169
+
170
+ // The platform's own output for NaN and infinities is no guide (Foundation
171
+ // prints "+∞" without the currency), so the symbols come from its data.
172
+ if (!symbols.nan.empty()) out.nan = symbols.nan;
173
+ std::string infinity = symbols.infinity;
174
+ for (const auto& sign : {symbols.plusSign, symbols.minusSign, std::string("+"), std::string("-")}) {
175
+ if (!sign.empty() && infinity.size() > sign.size() && infinity.compare(0, sign.size(), sign) == 0) infinity.erase(0, sign.size());
176
+ }
177
+ if (!infinity.empty()) out.infinity = infinity;
178
+ if (!symbols.exponentSeparator.empty()) out.exponentSeparator = symbols.exponentSeparator;
179
+ // The exponent's minus sign is the bare sign, without the direction marks around it.
180
+ std::string exponentMinus = symbols.minusSign;
181
+ for (const auto& mark : {std::string("\u200E"), std::string("\u200F"), std::string("\u061C")}) {
182
+ for (size_t at; (at = exponentMinus.find(mark)) != std::string::npos;) exponentMinus.erase(at, mark.size());
183
+ }
184
+ if (!exponentMinus.empty()) out.exponentMinusSign = exponentMinus;
185
+ return out;
186
+ }
187
+
188
+ std::array<int, 16> learnCompactExponents(const ProbeFormat& format, const LocaleFormat& locale) {
189
+ std::array<int, 16> exponents{};
190
+ for (int k = 0; k < 16; k++) {
191
+ // Digits before the decimal separator in the platform's "1K", "10K", "100K"…
192
+ int integerDigits = 0;
193
+ const std::string decimal = locale.decimalSeparator;
194
+ std::string text = format(std::pow(10.0, k));
195
+ const size_t at = decimal.empty() ? std::string::npos : text.find(decimal);
196
+ if (at != std::string::npos) text.erase(at);
197
+ for (const auto& cp : codePoints(text)) {
198
+ if (digitValue(cp, locale.digits) >= 0) integerDigits++;
199
+ }
200
+ exponents[static_cast<size_t>(k)] = integerDigits > 0 ? std::max(0, k - (integerDigits - 1)) : (k > 0 ? exponents[static_cast<size_t>(k - 1)] : 0);
201
+ }
202
+ return exponents;
203
+ }
204
+
205
+ Decimal roundCompact(const Decimal& value, const Rounding& rounding, const std::array<int, 16>& exponents) {
206
+ if (value.kind != Decimal::Kind::Finite || value.digits.empty()) return value;
207
+ auto exponentFor = [&](int magnitude) { return exponents[static_cast<size_t>(std::clamp(magnitude, 0, 15))]; };
208
+ const int magnitude = decimalMagnitude(value);
209
+ int exponent = exponentFor(magnitude);
210
+ int fractionDigits = 0;
211
+ Decimal scaled = value;
212
+ scaled.point -= exponent;
213
+ Decimal rounded = roundDecimal(scaled, rounding, fractionDigits);
214
+ // Rounding up can reach the next compact magnitude (999.9K → 1M).
215
+ if (!rounded.digits.empty() && decimalMagnitude(rounded) + exponent > magnitude) {
216
+ const int carried = exponentFor(decimalMagnitude(rounded) + exponent);
217
+ if (carried != exponent) {
218
+ exponent = carried;
219
+ scaled = value;
220
+ scaled.point -= exponent;
221
+ rounded = roundDecimal(scaled, rounding, fractionDigits);
222
+ }
223
+ }
224
+ if (!rounded.digits.empty()) rounded.point += exponent;
225
+ return rounded;
226
+ }
227
+
228
+ std::vector<Part> partsOfFormatted(const std::string& text, const LocaleFormat& format, const ProbeSymbols& symbols, TextKind words,
229
+ bool scientific) {
230
+ std::vector<Part> parts;
231
+ const auto cps = codePoints(text);
232
+ std::string pending; // text that is not part of the number
233
+ bool afterDecimal = false;
234
+ bool inExponent = false;
235
+
236
+ auto flushText = [&] {
237
+ if (pending.empty()) return;
238
+ // Spaces and marks around a word are literals of their own ("12", " ", "km/h").
239
+ const auto cps = codePoints(pending);
240
+ auto isBlank = [](const std::string& cp) { return cp == " " || cp == " " || cp == " " || isBidiControl(cp); };
241
+ size_t begin = 0;
242
+ size_t end = cps.size();
243
+ while (begin < end && isBlank(cps[begin])) begin++;
244
+ while (end > begin && isBlank(cps[end - 1])) end--;
245
+ PartType type = PartType::Literal;
246
+ switch (words) {
247
+ case TextKind::Compact:
248
+ type = PartType::Compact;
249
+ break;
250
+ case TextKind::Unit:
251
+ type = PartType::Unit;
252
+ break;
253
+ case TextKind::CurrencyName:
254
+ type = PartType::Currency;
255
+ break;
256
+ case TextKind::Literal:
257
+ break;
258
+ }
259
+ if (begin == end || type == PartType::Literal) {
260
+ parts.push_back({PartType::Literal, pending});
261
+ } else {
262
+ std::string lead, word, trail;
263
+ for (size_t i = 0; i < begin; i++) lead += cps[i];
264
+ for (size_t i = begin; i < end; i++) word += cps[i];
265
+ for (size_t i = end; i < cps.size(); i++) trail += cps[i];
266
+ if (!lead.empty()) parts.push_back({PartType::Literal, lead});
267
+ parts.push_back({type, word});
268
+ if (!trail.empty()) parts.push_back({PartType::Literal, trail});
269
+ }
270
+ pending.clear();
271
+ };
272
+ auto push = [&](PartType type, const std::string& value) {
273
+ if (!parts.empty() && parts.back().type == type && (type == PartType::Integer || type == PartType::Fraction || type == PartType::ExponentInteger)) {
274
+ parts.back().value += value;
275
+ } else {
276
+ parts.push_back({type, value});
277
+ }
278
+ };
279
+ auto startsAt = [&](size_t i, const std::string& symbol) {
280
+ if (symbol.empty()) return false;
281
+ std::string joined;
282
+ for (size_t j = i; j < cps.size() && joined.size() < symbol.size(); j++) joined += cps[j];
283
+ return joined == symbol;
284
+ };
285
+ auto isDigitAt = [&](size_t i) { return i < cps.size() && digitValue(cps[i], format.digits) >= 0; };
286
+ auto advance = [&](size_t& i, const std::string& symbol) {
287
+ size_t consumed = 0;
288
+ while (i < cps.size() && consumed < symbol.size()) consumed += cps[i++].size();
289
+ };
290
+
291
+ bool seenDigit = false;
292
+ for (size_t i = 0; i < cps.size();) {
293
+ if (isDigitAt(i)) {
294
+ flushText();
295
+ push(inExponent ? PartType::ExponentInteger : afterDecimal ? PartType::Fraction : PartType::Integer, cps[i]);
296
+ seenDigit = true;
297
+ i++;
298
+ continue;
299
+ }
300
+ if (seenDigit && !inExponent && !afterDecimal && startsAt(i, format.groupingSeparator)) {
301
+ size_t j = i;
302
+ advance(j, format.groupingSeparator);
303
+ if (isDigitAt(j)) {
304
+ flushText();
305
+ parts.push_back({PartType::Group, format.groupingSeparator});
306
+ i = j;
307
+ continue;
308
+ }
309
+ }
310
+ if (seenDigit && !inExponent && !afterDecimal && startsAt(i, format.decimalSeparator)) {
311
+ size_t j = i;
312
+ advance(j, format.decimalSeparator);
313
+ if (isDigitAt(j)) {
314
+ flushText();
315
+ parts.push_back({PartType::Decimal, format.decimalSeparator});
316
+ afterDecimal = true;
317
+ i = j;
318
+ continue;
319
+ }
320
+ }
321
+ if (scientific && seenDigit && !inExponent && (cps[i] == "E" || cps[i] == "e")) {
322
+ flushText();
323
+ parts.push_back({PartType::ExponentSeparator, cps[i]});
324
+ inExponent = true;
325
+ i++;
326
+ continue;
327
+ }
328
+ struct Symbol {
329
+ const std::string* text;
330
+ PartType type;
331
+ };
332
+ const Symbol candidates[] = {{&symbols.currency, PartType::Currency},
333
+ {&symbols.minusSign, inExponent ? PartType::ExponentMinusSign : PartType::MinusSign},
334
+ {&symbols.plusSign, PartType::PlusSign},
335
+ {&symbols.percentSign, PartType::PercentSign},
336
+ {&format.nan, PartType::Nan},
337
+ {&format.infinity, PartType::Infinity}};
338
+ const Symbol* best = nullptr;
339
+ for (const auto& c : candidates) {
340
+ if (startsAt(i, *c.text) && (!best || c.text->size() > best->text->size())) best = &c;
341
+ }
342
+ if (best) {
343
+ flushText();
344
+ parts.push_back({best->type, *best->text});
345
+ advance(i, *best->text);
346
+ continue;
347
+ }
348
+ pending += cps[i++];
349
+ }
350
+ flushText();
351
+ return parts;
352
+ }
353
+
354
+ } // namespace margelo::nitro::nitroinput::numberformat
@@ -0,0 +1,63 @@
1
+ //
2
+ // NumberFormatProbe.hpp
3
+ // NitroInput
4
+ //
5
+ // Learns a locale's number format from the platform's own formatter, by
6
+ // formatting a few known numbers with it and reading the output: the digits,
7
+ // the separators, the grouping sizes, the text before and after a positive
8
+ // and a negative number. What the platform prints for those numbers is by
9
+ // construction what `NumberFormatCore` prints afterwards, so the C++ output
10
+ // matches the platform's (and so Hermes' `Intl`, which uses the same
11
+ // formatter) without asking it again per number.
12
+ //
13
+
14
+ #pragma once
15
+
16
+ #include "NumberFormatCore.hpp"
17
+
18
+ #include <functional>
19
+ #include <string>
20
+
21
+ namespace margelo::nitro::nitroinput::numberformat {
22
+
23
+ struct ProbeSymbols {
24
+ std::string minusSign = "-";
25
+ std::string plusSign = "+";
26
+ std::string percentSign = "%";
27
+ /// The currency as the formatter shows it; empty without one.
28
+ std::string currency;
29
+ std::string nan = "NaN";
30
+ std::string infinity = "\u221E";
31
+ std::string exponentSeparator = "E";
32
+ };
33
+
34
+ /// Formats a number with the platform formatter being probed.
35
+ using ProbeFormat = std::function<std::string(double)>;
36
+
37
+ /// The locale's digits 0…9 from its formatting of 1234567890 without grouping,
38
+ /// or Latin digits when that output is not ten digits.
39
+ std::array<std::string, 10> learnDigits(const std::string& formatted1234567890);
40
+
41
+ /// The format of a formatter set to two fraction digits with grouping on.
42
+ /// `scale` is 2 for percent (the formatter multiplies by 100).
43
+ LocaleFormat learnLocaleFormat(const ProbeFormat& format, const std::array<std::string, 10>& digits, const ProbeSymbols& symbols, int scale);
44
+
45
+ /// What the output of a platform-formatted number consists of, for the
46
+ /// notations and styles C++ does not format itself.
47
+ enum class TextKind { Literal, Compact, Unit, CurrencyName };
48
+ std::vector<Part> partsOfFormatted(const std::string& text, const LocaleFormat& format, const ProbeSymbols& symbols, TextKind words,
49
+ bool scientific);
50
+
51
+ /// Compact notation: for each magnitude 0…15, the power of ten the platform
52
+ /// divides by ("1K" for 10^3 → 3, "1万" for 10^4 → 4, "1000" → 0), read off its
53
+ /// formatting of 10^magnitude.
54
+ std::array<int, 16> learnCompactExponents(const ProbeFormat& format, const LocaleFormat& locale);
55
+
56
+ /// A value rounded the way ECMA-402 rounds a compact number (the digit options
57
+ /// applied to the value divided by its compact power of ten), still undivided.
58
+ Decimal roundCompact(const Decimal& value, const Rounding& rounding, const std::array<int, 16>& exponents);
59
+
60
+ /// Splits UTF-8 into code points (as strings); invalid bytes stand alone.
61
+ std::vector<std::string> codePoints(const std::string& text);
62
+
63
+ } // namespace margelo::nitro::nitroinput::numberformat
@@ -106,6 +106,11 @@ final class HybridNitroInputView: HybridNitroInputViewSpec, RecyclableView {
106
106
  var signPlacement: NitroInputSignPlacement = .beforeaffix { didSet { markConfigDirty() } }
107
107
  var prefixAlign: NitroInputAffixAlign = .baseline { didSet { markConfigDirty() } }
108
108
  var suffixAlign: NitroInputAffixAlign = .baseline { didSet { markConfigDirty() } }
109
+ var letterSpacing: Double = 0 { didSet { markConfigDirty() } }
110
+ var prefixSpacing: Double = .infinity { didSet { markConfigDirty() } }
111
+ var suffixSpacing: Double = .infinity { didSet { markConfigDirty() } }
112
+ var prefixOffset: Double = 0 { didSet { markConfigDirty() } }
113
+ var suffixOffset: Double = 0 { didSet { markConfigDirty() } }
109
114
  var placeholder: String = "" { didSet { markConfigDirty() } }
110
115
  var placeholderColor: Double = .nan { didSet { markConfigDirty() } }
111
116
  var duration: Double = 400 { didSet { markConfigDirty() } }
@@ -302,6 +307,11 @@ final class HybridNitroInputView: HybridNitroInputViewSpec, RecyclableView {
302
307
  signPlacement = .beforeaffix
303
308
  prefixAlign = .baseline
304
309
  suffixAlign = .baseline
310
+ letterSpacing = 0
311
+ prefixSpacing = .infinity
312
+ suffixSpacing = .infinity
313
+ prefixOffset = 0
314
+ suffixOffset = 0
305
315
  placeholder = ""
306
316
  placeholderColor = .nan
307
317
  duration = 400
@@ -509,6 +519,11 @@ final class HybridNitroInputView: HybridNitroInputViewSpec, RecyclableView {
509
519
  typography.placeholderColor = Self.color(fromARGB: placeholderColor) ?? .placeholderText
510
520
  typography.prefixAlign = Self.mapAffixAlign(prefixAlign)
511
521
  typography.suffixAlign = Self.mapAffixAlign(suffixAlign)
522
+ typography.letterSpacing = CGFloat(letterSpacing.isFinite ? letterSpacing : 0)
523
+ typography.prefixSpacing = prefixSpacing.isFinite ? CGFloat(prefixSpacing) : nil
524
+ typography.suffixSpacing = suffixSpacing.isFinite ? CGFloat(suffixSpacing) : nil
525
+ typography.prefixOffset = CGFloat(prefixOffset.isFinite ? prefixOffset : 0)
526
+ typography.suffixOffset = CGFloat(suffixOffset.isFinite ? suffixOffset : 0)
512
527
  typography.adjustsFontSizeToFit = adjustsFontSizeToFit
513
528
  typography.minimumFontScale = CGFloat(min(1, max(0.05, minimumFontScale.isFinite ? minimumFontScale : 0.5)))
514
529
  typography.allowFontScaling = allowFontScaling