react-native-nitro-input 0.4.3 β†’ 0.4.4

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 CHANGED
@@ -1,5 +1,18 @@
1
1
  # Changelog
2
2
 
3
+ ## 0.4.4
4
+
5
+ - **iOS: no more crash editing next to an emoji.** UIKit can hand
6
+ `textField(_:shouldChangeCharactersIn:)` a range whose offset falls inside a
7
+ surrogate pair (an emoji, or another character outside the Basic
8
+ Multilingual Plane). Converting it to code points stepped back from an
9
+ unaligned index: that trapped (`EXC_BREAKPOINT`, "String index is out of
10
+ bounds") when the pair was the first character, and counted one code point
11
+ too few anywhere else, so number, mask and `transform` edits and the
12
+ reported selection landed one character early. Such an offset now counts as
13
+ the start of its code point, as `String.replaceSubrange` rounds it. Covered
14
+ by `bun run test:swift`.
15
+
3
16
  ## 0.4.3
4
17
 
5
18
  0.4.2 was never published; its change is part of this release, with the
@@ -25,8 +25,8 @@ Pod::Spec.new do |s|
25
25
  # The shared C++ engines and formatter are public headers so the pod's Swift
26
26
  # sources can call them directly through Swift/C++ interop (enabled by Nitro below).
27
27
  s.public_header_files = ["cpp/**/*.hpp"]
28
- # The engines' unit tests build as a host executable (`bun run test:cpp`).
29
- s.exclude_files = ["cpp/__tests__/**"]
28
+ # The engines' unit tests build as host executables (`bun run test:cpp`, `bun run test:swift`).
29
+ s.exclude_files = ["cpp/__tests__/**", "ios/__tests__/**"]
30
30
 
31
31
  # Worklets are optional: with react-native-worklets in the app, `transform`
32
32
  # and worklet callbacks run synchronously on the UI thread.
@@ -0,0 +1,35 @@
1
+ import Foundation
2
+
3
+ /// UITextField and UITextView speak UTF-16 offsets; the props, callbacks and
4
+ /// engines speak code points. Foundation-only, so `bun run test:swift` builds
5
+ /// it as a host executable.
6
+ enum CodePointOffsets {
7
+ /// The code point offset of a UTF-16 offset. One that lands inside a
8
+ /// surrogate pair counts as the start of that code point, as
9
+ /// `String.replaceSubrange` rounds it. UIKit hands those over (the emoji
10
+ /// keyboard, autocorrect and IME replacements next to an emoji, a caret
11
+ /// left inside a pair). Stepping back from such an index with
12
+ /// `unicodeScalars.index(before:)`, as this used to, trapped when the pair
13
+ /// was the first code point (EXC_BREAKPOINT in
14
+ /// `textField(_:shouldChangeCharactersIn:)`) and counted one too few
15
+ /// anywhere else.
16
+ static func codePoint(in string: String, utf16 offset: Int) -> Int {
17
+ guard offset > 0 else { return 0 }
18
+ var units = 0
19
+ var count = 0
20
+ for scalar in string.unicodeScalars {
21
+ units += scalar.utf16.count
22
+ if units > offset { return count }
23
+ count += 1
24
+ }
25
+ return count
26
+ }
27
+
28
+ /// The UTF-16 offset of a code point offset, clamped to the string.
29
+ static func utf16(in string: String, codePoint offset: Int) -> Int {
30
+ let scalars = string.unicodeScalars
31
+ let clamped = min(max(0, offset), scalars.count)
32
+ let index = scalars.index(scalars.startIndex, offsetBy: clamped)
33
+ return string.utf16.distance(from: string.utf16.startIndex, to: index)
34
+ }
35
+ }
@@ -1881,19 +1881,11 @@ final class NitroInputView: UIView {
1881
1881
  static func isSign(_ scalar: Unicode.Scalar) -> Bool { InputText.isSign(scalar) }
1882
1882
 
1883
1883
  static func codePointOffset(in string: String, utf16 offset: Int) -> Int {
1884
- let utf16 = string.utf16
1885
- let clamped = min(max(0, offset), utf16.count)
1886
- let index = utf16.index(utf16.startIndex, offsetBy: clamped)
1887
- let scalars = string.unicodeScalars
1888
- let scalarIndex = index.samePosition(in: scalars) ?? scalars.index(before: min(index, scalars.endIndex))
1889
- return scalars.distance(from: scalars.startIndex, to: scalarIndex)
1884
+ CodePointOffsets.codePoint(in: string, utf16: offset)
1890
1885
  }
1891
1886
 
1892
1887
  static func utf16Offset(in string: String, codePoint offset: Int) -> Int {
1893
- let scalars = string.unicodeScalars
1894
- let clamped = min(max(0, offset), scalars.count)
1895
- let index = scalars.index(scalars.startIndex, offsetBy: clamped)
1896
- return string.utf16.distance(from: string.utf16.startIndex, to: index)
1888
+ CodePointOffsets.utf16(in: string, codePoint: offset)
1897
1889
  }
1898
1890
 
1899
1891
  // MARK: - Display link
@@ -0,0 +1,77 @@
1
+ import Foundation
2
+
3
+ // Host test for ios/CodePointOffsets.swift: `bun run test:swift`.
4
+ // UITextField's text is a bridged NSString, so every case runs on a native
5
+ // Swift string and on a bridged one.
6
+
7
+ @main
8
+ enum CodePointOffsetsTest {
9
+ static var failures = 0
10
+ static var checks = 0
11
+
12
+ static func expect(_ got: Int, _ want: Int, _ what: @autoclosure () -> String) {
13
+ checks += 1
14
+ if got != want {
15
+ failures += 1
16
+ print("FAIL \(what()): got \(got), want \(want)")
17
+ }
18
+ }
19
+
20
+ static func variants(_ s: String) -> [String] { [s, NSMutableString(string: s) as String] }
21
+
22
+ static func main() {
23
+ // The production crash: a UTF-16 offset inside the first code point's
24
+ // surrogate pair. This trapped (EXC_BREAKPOINT) before the fix.
25
+ for s in variants("πŸ˜€ab") {
26
+ expect(CodePointOffsets.codePoint(in: s, utf16: 0), 0, "πŸ˜€ab @0")
27
+ expect(CodePointOffsets.codePoint(in: s, utf16: 1), 0, "πŸ˜€ab @1 (mid-surrogate)")
28
+ expect(CodePointOffsets.codePoint(in: s, utf16: 2), 1, "πŸ˜€ab @2")
29
+ expect(CodePointOffsets.codePoint(in: s, utf16: 4), 3, "πŸ˜€ab @4")
30
+ }
31
+ // Inside a later pair it used to count one too few (0 here).
32
+ for s in variants("aπŸ˜€bπŸ˜€") {
33
+ expect(CodePointOffsets.codePoint(in: s, utf16: 2), 1, "aπŸ˜€bπŸ˜€ @2 (mid-surrogate)")
34
+ expect(CodePointOffsets.codePoint(in: s, utf16: 5), 3, "aπŸ˜€bπŸ˜€ @5 (mid-surrogate)")
35
+ expect(CodePointOffsets.codePoint(in: s, utf16: 6), 4, "aπŸ˜€bπŸ˜€ @6")
36
+ }
37
+ // Out of range clamps.
38
+ for s in variants("ab") {
39
+ expect(CodePointOffsets.codePoint(in: s, utf16: -3), 0, "ab @-3")
40
+ expect(CodePointOffsets.codePoint(in: s, utf16: 9), 2, "ab @9")
41
+ }
42
+ // A lone surrogate (a bridged NSString can hold one mid-edit) is one unit.
43
+ let lone = NSString(characters: [0xD83D, 0x61], length: 2) as String
44
+ expect(CodePointOffsets.codePoint(in: lone, utf16: 1), 1, "lone @1")
45
+ expect(CodePointOffsets.codePoint(in: lone, utf16: 2), 2, "lone @2")
46
+
47
+ // Every offset of strings UIKit edits around: skin tones, ZWJ families,
48
+ // flags, combining marks, CJK. Monotonic, rounds down inside a pair, and
49
+ // agrees with utf16(in:codePoint:) on every scalar boundary.
50
+ let samples = ["", "a", "πŸ˜€", "πŸ‘πŸ½x", "πŸ‘¨β€πŸ‘©β€πŸ‘§β€πŸ‘¦", "πŸ‡ΏπŸ‡¦πŸ‡ΊπŸ‡Έ", "Γ©", "e\u{301}", "ζ—₯本θͺžπŸ˜€", "aπŸ˜€πŸ˜€πŸ˜€z"]
51
+ for sample in samples {
52
+ for s in variants(sample) {
53
+ let n = s.utf16.count
54
+ var boundaries = Set<Int>()
55
+ var units = 0
56
+ boundaries.insert(0)
57
+ for scalar in s.unicodeScalars { units += scalar.utf16.count; boundaries.insert(units) }
58
+ var previous = 0
59
+ for offset in 0...n {
60
+ let got = CodePointOffsets.codePoint(in: s, utf16: offset)
61
+ let back = CodePointOffsets.utf16(in: s, codePoint: got)
62
+ checks += 1
63
+ if got < previous || back > offset || (boundaries.contains(offset) && back != offset) {
64
+ failures += 1
65
+ print("FAIL \(sample.debugDescription) @\(offset): \(got) -> \(back)")
66
+ }
67
+ previous = got
68
+ }
69
+ expect(CodePointOffsets.codePoint(in: s, utf16: n), s.unicodeScalars.count, "\(sample.debugDescription) end")
70
+ expect(CodePointOffsets.utf16(in: s, codePoint: 99), n, "\(sample.debugDescription) utf16 clamp")
71
+ }
72
+ }
73
+
74
+ print("CodePointOffsetsTest: \(checks) checks, \(failures) failures")
75
+ if failures > 0 { exit(1) }
76
+ }
77
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-nitro-input",
3
- "version": "0.4.3",
3
+ "version": "0.4.4",
4
4
  "description": "Native text for React Native, built with Nitro: a text and amount input with native formatting, masks and a Torph-style reflow, and an animated number (odometer, SwiftUI-style numeric text, ticker) whose digits roll or swap natively, with currency layouts and animated currency switches, shrink-to-fit, a loading shimmer and a jackpot reveal.",
5
5
  "main": "lib/commonjs/src/index.js",
6
6
  "module": "lib/module/src/index.js",
@@ -40,6 +40,7 @@
40
40
  "prepack": "bun run build",
41
41
  "test": "jest",
42
42
  "test:cpp": "mkdir -p .cache && clang++ -std=c++20 -O1 -Wall -Wextra -Icpp cpp/ReflowEngine.cpp cpp/AmountFormatter.cpp cpp/__tests__/ReflowEngineTest.cpp -o .cache/reflow-engine-test && .cache/reflow-engine-test && clang++ -std=c++20 -O1 -Wall -Wextra -Icpp cpp/AmountFormatter.cpp cpp/__tests__/AmountFormatterTest.cpp -o .cache/amount-formatter-test && .cache/amount-formatter-test && clang++ -std=c++20 -O1 -Wall -Wextra -Icpp cpp/AmountFormatter.cpp cpp/MaskEngine.cpp cpp/__tests__/MaskEngineTest.cpp -o .cache/mask-engine-test && .cache/mask-engine-test && clang++ -std=c++20 -O1 -Wall -Wextra -Icpp cpp/AmountFormatter.cpp cpp/MaskEngine.cpp cpp/__tests__/MaskEngineCorpusTest.cpp -o .cache/mask-engine-corpus-test && .cache/mask-engine-corpus-test && clang++ -std=c++20 -O1 -Wall -Wextra -Icpp cpp/OutlineGeometry.cpp cpp/__tests__/OutlineGeometryTest.cpp -o .cache/outline-geometry-test && .cache/outline-geometry-test && clang++ -std=c++20 -O1 -Wall -Wextra -Icpp cpp/RollingEngine.cpp cpp/__tests__/RollingEngineTest.cpp -o .cache/rolling-engine-test && .cache/rolling-engine-test && clang++ -std=c++20 -O1 -Wall -Wextra -Icpp cpp/numberformat/NumberFormatCore.cpp cpp/numberformat/FormatFigure.cpp cpp/__tests__/NumberFormatCoreTest.cpp -o .cache/number-format-core-test && .cache/number-format-core-test && clang++ -std=c++20 -O1 -Wall -Wextra -Icpp cpp/numberformat/NumberFormatCore.cpp cpp/numberformat/NumberFormatProbe.cpp cpp/__tests__/NumberFormatProbeTest.cpp -o .cache/number-format-probe-test && .cache/number-format-probe-test",
43
+ "test:swift": "mkdir -p .cache && swiftc -parse-as-library ios/CodePointOffsets.swift ios/__tests__/CodePointOffsetsTest.swift -o .cache/code-point-offsets-test && .cache/code-point-offsets-test",
43
44
  "test:cpp:sanitize": "mkdir -p .cache && clang++ -std=c++20 -O1 -g -fsanitize=address,undefined -fno-omit-frame-pointer -Icpp cpp/RollingEngine.cpp cpp/__tests__/RollingEngineTest.cpp -o .cache/rolling-engine-sanitize && .cache/rolling-engine-sanitize",
44
45
  "release": "release-it"
45
46
  },