Skip to content

Commit 2785bee

Browse files
authored
Merge pull request #14356 from milseman/what_a_character
[string] Minor cleanup on Character
2 parents e43ff71 + 49a2103 commit 2785bee

File tree

3 files changed

+24
-27
lines changed

3 files changed

+24
-27
lines changed

stdlib/public/core/Character.swift

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,7 @@ extension Character
136136
@_inlineable // FIXME(sil-serialize-all)
137137
@effects(readonly)
138138
public init(_builtinUnicodeScalarLiteral value: Builtin.Int32) {
139-
self = Character(
140-
String._fromWellFormedCodeUnitSequence(
141-
UTF32.self, input: CollectionOfOne(UInt32(value))))
139+
self.init(Unicode.Scalar(_builtinUnicodeScalarLiteral: value))
142140
}
143141

144142
// Inlining ensures that the whole constructor can be folded away to a single
@@ -154,22 +152,22 @@ extension Character
154152
let utf8 = UnsafeBufferPointer(
155153
start: UnsafePointer<Unicode.UTF8.CodeUnit>(start),
156154
count: Int(utf8CodeUnitCount))
157-
155+
158156
if utf8.count == 1 {
159157
_representation = .smallUTF16(
160158
Builtin.zext_Int8_Int63(utf8.first._unsafelyUnwrappedUnchecked._value))
161159
return
162160
}
163161

164-
FastPath:
162+
FastPath:
165163
repeat {
166164
var shift = 0
167165
let maxShift = 64 - 16
168166
var bits: UInt64 = 0
169-
167+
170168
for s8 in Unicode._ParsingIterator(
171169
codeUnits: utf8.makeIterator(), parser: UTF8.ForwardParser()) {
172-
170+
173171
let s16
174172
= UTF16.transcode(s8, from: UTF8.self)._unsafelyUnwrappedUnchecked
175173

@@ -186,7 +184,7 @@ extension Character
186184
return
187185
}
188186
while false
189-
187+
190188
// For anything that doesn't fit in 63 bits, build the large
191189
// representation.
192190
self = Character(_largeRepresentationString:
@@ -227,11 +225,10 @@ extension Character
227225
| UInt64(utf16[3]) &<< 48
228226
_representation = .smallUTF16(Builtin.trunc_Int64_Int63(bits._value))
229227
default:
230-
_representation = Character(
231-
_largeRepresentationString: String(_StringGuts(utf16)))._representation
228+
_representation = .large(_StringGuts(utf16)._extractNativeStorage())
232229
}
233230
}
234-
231+
235232
/// Creates a character with the specified value.
236233
///
237234
/// Do not call this initalizer directly. It is used by the compiler when
@@ -485,7 +482,7 @@ extension Character : Equatable {
485482
if l == r { return true }
486483
}
487484
}
488-
485+
489486
// FIXME(performance): constructing two temporary strings is extremely
490487
// wasteful and inefficient.
491488
return String(lhs) == String(rhs)

stdlib/public/core/String.swift

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ public protocol StringProtocol
2525

2626
associatedtype UTF8View : /*Bidirectional*/Collection
2727
where UTF8View.Element == UInt8 // Unicode.UTF8.CodeUnit
28-
28+
2929
associatedtype UTF16View : BidirectionalCollection
3030
where UTF16View.Element == UInt16 // Unicode.UTF16.CodeUnit
3131

3232
associatedtype UnicodeScalarView : BidirectionalCollection
3333
where UnicodeScalarView.Element == Unicode.Scalar
34-
34+
3535
var utf8: UTF8View { get }
3636
var utf16: UTF16View { get }
3737
var unicodeScalars: UnicodeScalarView { get }
38-
38+
3939
#if _runtime(_ObjC)
4040
func hasPrefix(_ prefix: String) -> Bool
4141
func hasSuffix(_ prefix: String) -> Bool
@@ -63,7 +63,7 @@ public protocol StringProtocol
6363
/// - Parameter nullTerminatedUTF8: A pointer to a sequence of contiguous,
6464
/// UTF-8 encoded bytes ending just before the first zero byte.
6565
init(cString nullTerminatedUTF8: UnsafePointer<CChar>)
66-
66+
6767
/// Creates a string from the null-terminated sequence of bytes at the given
6868
/// pointer.
6969
///
@@ -76,7 +76,7 @@ public protocol StringProtocol
7676
init<Encoding: Unicode.Encoding>(
7777
decodingCString nullTerminatedCodeUnits: UnsafePointer<Encoding.CodeUnit>,
7878
as sourceEncoding: Encoding.Type)
79-
79+
8080
/// Calls the given closure with a pointer to the contents of the string,
8181
/// represented as a null-terminated sequence of UTF-8 code units.
8282
///
@@ -151,7 +151,7 @@ internal protocol _SwiftStringView {
151151
/// A `String`, having the same contents as `self`, that may be unsuitable for
152152
/// long-term storage.
153153
var _ephemeralContent : String { get }
154-
154+
155155
/// A `String`, having the same contents as `self`, that is suitable for
156156
/// long-term storage.
157157
//
@@ -206,8 +206,8 @@ extension String : _SwiftStringView {
206206
@_versioned // FIXME(sil-serialize-all)
207207
internal func _withCString<
208208
Source : Collection,
209-
SourceEncoding : Unicode.Encoding,
210-
TargetEncoding : Unicode.Encoding,
209+
SourceEncoding : Unicode.Encoding,
210+
TargetEncoding : Unicode.Encoding,
211211
Result
212212
>(
213213
encodedAs targetEncoding: TargetEncoding.Type,
@@ -227,8 +227,8 @@ where Source.Iterator.Element == SourceEncoding.CodeUnit {
227227
@_semantics("optimize.sil.specialize.generic.partial.never")
228228
internal func _withCStringAndLength<
229229
Source : Collection,
230-
SourceEncoding : Unicode.Encoding,
231-
TargetEncoding : Unicode.Encoding,
230+
SourceEncoding : Unicode.Encoding,
231+
TargetEncoding : Unicode.Encoding,
232232
Result
233233
>(
234234
encodedAs targetEncoding: TargetEncoding.Type,
@@ -383,7 +383,7 @@ extension String {
383383
repairIllFormedSequences: true)
384384
self = result!
385385
}
386-
386+
387387
/// Creates a string from the null-terminated sequence of bytes at the given
388388
/// pointer.
389389
///
@@ -404,7 +404,7 @@ extension String {
404404
)
405405
self.init(decoding: codeUnits, as: sourceEncoding)
406406
}
407-
407+
408408
/// Calls the given closure with a pointer to the contents of the string,
409409
/// represented as a null-terminated sequence of code units.
410410
///
@@ -1063,7 +1063,7 @@ extension String {
10631063
start: UnsafeMutablePointer<UTF8.CodeUnit>,
10641064
utf8CodeUnitCount: Int
10651065
) {
1066-
resultStorage.initialize(to:
1066+
resultStorage.initialize(to:
10671067
String._fromWellFormedCodeUnitSequence(
10681068
UTF8.self,
10691069
input: UnsafeBufferPointer(start: start, count: utf8CodeUnitCount)))
@@ -1356,7 +1356,7 @@ extension String {
13561356
return _nativeUnicodeUppercaseString(self)
13571357
#endif
13581358
}
1359-
1359+
13601360
/// Creates an instance from the description of a given
13611361
/// `LosslessStringConvertible` instance.
13621362
@_inlineable // FIXME(sil-serialize-all)

stdlib/public/core/StringBridge.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ internal func _cocoaStringSlice(
8181
_ target: _CocoaString, _ bounds: Range<Int>
8282
) -> _CocoaString {
8383
let cfSelf: _swift_shims_CFStringRef = target
84-
84+
8585
_sanityCheck(
8686
_swift_stdlib_CFStringGetCharactersPtr(cfSelf) == nil,
8787
"Known contiguously stored strings should already be converted to Swift")

0 commit comments

Comments
 (0)