Skip to content

Commit aa51936

Browse files
committed
[String] Add Character.UTF16View and Character.UTF8View
Adds these collections, which are just String's views. Tests added.
1 parent b19c2cf commit aa51936

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

stdlib/public/core/Character.swift

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -88,25 +88,26 @@ extension Character {
8888
}
8989

9090
extension Character {
91-
@usableFromInline
92-
typealias UTF8View = String.UTF8View
91+
/// A view of a character's contents as a collection of UTF-8 code units. See
92+
/// String.UTF8View for more information
93+
public typealias UTF8View = String.UTF8View
9394

95+
/// A UTF-8 encoding of `self`.
9496
@inlinable
95-
internal var utf8: UTF8View {
96-
return _str.utf8
97-
}
98-
@usableFromInline
99-
typealias UTF16View = String.UTF16View
97+
public var utf8: UTF8View { return _str.utf8 }
98+
99+
/// A view of a character's contents as a collection of UTF-16 code units. See
100+
/// String.UTF16View for more information
101+
public typealias UTF16View = String.UTF16View
100102

103+
/// A UTF-16 encoding of `self`.
101104
@inlinable
102-
internal var utf16: UTF16View {
103-
return _str.utf16
104-
}
105+
public var utf16: UTF16View { return _str.utf16 }
106+
105107
public typealias UnicodeScalarView = String.UnicodeScalarView
108+
106109
@inlinable
107-
public var unicodeScalars: UnicodeScalarView {
108-
return _str.unicodeScalars
109-
}
110+
public var unicodeScalars: UnicodeScalarView { return _str.unicodeScalars }
110111
}
111112

112113
extension Character :

test/stdlib/Character.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,19 @@ CharacterTests.test("String.append(_: Character)") {
347347
}
348348
}
349349

350+
CharacterTests.test("utf6/16/unicodescalar views") {
351+
for c in testCharacters {
352+
expectEqualSequence(String(c).unicodeScalars, c.unicodeScalars)
353+
expectEqualSequence(String(c).utf8, c.utf8)
354+
expectEqualSequence(String(c).utf16, c.utf16)
355+
356+
expectEqualSequence(
357+
String(c).unicodeScalars.reversed(), c.unicodeScalars.reversed())
358+
expectEqualSequence(String(c).utf8.reversed(), c.utf8.reversed())
359+
expectEqualSequence(String(c).utf16.reversed(), c.utf16.reversed())
360+
}
361+
}
362+
350363
var UnicodeScalarTests = TestSuite("UnicodeScalar")
351364

352365
UnicodeScalarTests.test("UInt8(ascii: UnicodeScalar)") {

0 commit comments

Comments
 (0)