Skip to content

Commit adb5d21

Browse files
committed
Add basic Unicode scalar operations & iterators to _StringGuts variants. Use them to de-core String.UnicodeScalarView.
1 parent fc757d3 commit adb5d21

File tree

6 files changed

+337
-145
lines changed

6 files changed

+337
-145
lines changed

stdlib/public/core/StringCharacterView.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -788,8 +788,9 @@ extension String._CharacterView {
788788
/// Objective-C, where *n* is the length of the string; otherwise, O(1).
789789
@_inlineable // FIXME(sil-serialize-all)
790790
public subscript(bounds: Range<Index>) -> String.CharacterView {
791-
return String._CharacterView(
792-
unicodeScalars[bounds]._guts,
793-
coreOffset: bounds.lowerBound.encodedOffset)
791+
let scalarSlice: String.UnicodeScalarView = unicodeScalars[bounds]
792+
return String.CharacterView(
793+
scalarSlice._guts,
794+
coreOffset: scalarSlice._coreOffset)
794795
}
795796
}

stdlib/public/core/StringGuts.swift

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,6 +1362,41 @@ extension _StringGuts {
13621362
// String API
13631363
//
13641364

1365+
// UnicodeScalarView operations
1366+
extension _StringGuts {
1367+
@_inlineable // FIXME(sil-serialize-all)
1368+
@_versioned // FIXME(sil-serialize-all)
1369+
func _unicodeScalarWidth(startingAt offset: Int) -> Int {
1370+
if _slowPath(_isOpaque) {
1371+
return _asOpaque()._unicodeScalarWidth(startingAt: offset)
1372+
}
1373+
if isASCII { return 1 }
1374+
return _unmanagedUTF16View._unicodeScalarWidth(startingAt: offset)
1375+
}
1376+
1377+
@_inlineable // FIXME(sil-serialize-all)
1378+
@_versioned // FIXME(sil-serialize-all)
1379+
func _unicodeScalarWidth(endingAt offset: Int) -> Int {
1380+
if _slowPath(_isOpaque) {
1381+
return _asOpaque()._unicodeScalarWidth(endingAt: offset)
1382+
}
1383+
if isASCII { return 1 }
1384+
return _unmanagedUTF16View._unicodeScalarWidth(endingAt: offset)
1385+
}
1386+
1387+
@_inlineable // FIXME(sil-serialize-all)
1388+
@_versioned // FIXME(sil-serialize-all)
1389+
func _decodeUnicodeScalar(startingAt offset: Int) -> UnicodeDecodingResult {
1390+
if _slowPath(_isOpaque) {
1391+
return _asOpaque()._decodeUnicodeScalar(startingAt: offset)
1392+
}
1393+
if isASCII {
1394+
return _unmanagedASCIIView._decodeUnicodeScalar(startingAt: offset)
1395+
}
1396+
return _unmanagedUTF16View._decodeUnicodeScalar(startingAt: offset)
1397+
}
1398+
}
1399+
13651400
// Some CharacterView operations
13661401
extension String {
13671402
/// Accesses the character at the given position.

0 commit comments

Comments
 (0)