File tree Expand file tree Collapse file tree 1 file changed +16
-0
lines changed Expand file tree Collapse file tree 1 file changed +16
-0
lines changed Original file line number Diff line number Diff line change @@ -439,6 +439,22 @@ extension String.CharacterView : BidirectionalCollection {
439
439
/// - Parameter position: A valid index of the character view. `position`
440
440
/// must be less than the view's end index.
441
441
public subscript( i: Index ) -> Character {
442
+ if i. _countUTF16 == 1 {
443
+ // For single-code-unit graphemes, we can construct a Character directly
444
+ // from a single unicode scalar (if sub-surrogate).
445
+ let relativeOffset = i. _base. _position - _coreOffset
446
+ if _core. isASCII {
447
+ let asciiBuffer = _core. asciiBuffer. _unsafelyUnwrappedUnchecked
448
+ return Character ( UnicodeScalar ( asciiBuffer [ relativeOffset] ) )
449
+ } else if _core. _baseAddress != nil {
450
+ let cu = _core. _nthContiguous ( relativeOffset)
451
+ // Only constructible if sub-surrogate
452
+ if ( cu < 0xd800 ) {
453
+ return Character ( UnicodeScalar ( cu) . _unsafelyUnwrappedUnchecked)
454
+ }
455
+ }
456
+ }
457
+
442
458
return Character ( String ( unicodeScalars [ i. _base..< i. _endBase] ) )
443
459
}
444
460
}
You can’t perform that action at this time.
0 commit comments