Skip to content

Commit 26740f8

Browse files
gribozavrMax Moiseev
authored andcommitted
stdlib: replace error-prone pairs of isASCII/startASCII calls with an API that returns Optional
1 parent 89fdd77 commit 26740f8

File tree

5 files changed

+49
-37
lines changed

5 files changed

+49
-37
lines changed

stdlib/public/core/OutputStream.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -497,11 +497,13 @@ internal struct _Stdout : TextOutputStream {
497497
mutating func write(_ string: String) {
498498
if string.isEmpty { return }
499499

500-
if string._core.isASCII {
500+
if let asciiBuffer = string._core.asciiBuffer {
501501
defer { _fixLifetime(string) }
502502

503-
_swift_stdlib_fwrite_stdout(UnsafePointer(string._core.startASCII),
504-
string._core.count, 1)
503+
_swift_stdlib_fwrite_stdout(
504+
UnsafePointer(asciiBuffer.baseAddress!),
505+
asciiBuffer.count,
506+
1)
505507
return
506508
}
507509

stdlib/public/core/String.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -726,9 +726,9 @@ extension String {
726726
///
727727
/// - Complexity: O(*n*)
728728
public func lowercased() -> String {
729-
if self._core.isASCII {
730-
let count = self._core.count
731-
let source = self._core.startASCII
729+
if let asciiBuffer = self._core.asciiBuffer {
730+
let count = asciiBuffer.count
731+
let source = asciiBuffer.baseAddress!
732732
let buffer = _StringBuffer(
733733
capacity: count, initialSize: count, elementWidth: 1)
734734
let dest = buffer.start
@@ -776,9 +776,9 @@ extension String {
776776
///
777777
/// - Complexity: O(*n*)
778778
public func uppercased() -> String {
779-
if self._core.isASCII {
780-
let count = self._core.count
781-
let source = self._core.startASCII
779+
if let asciiBuffer = self._core.asciiBuffer {
780+
let count = asciiBuffer.count
781+
let source = asciiBuffer.baseAddress!
782782
let buffer = _StringBuffer(
783783
capacity: count, initialSize: count, elementWidth: 1)
784784
let dest = buffer.start

stdlib/public/core/StringCore.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,6 +240,13 @@ public struct _StringCore {
240240
return _baseAddress!.assumingMemoryBound(to: UTF16.CodeUnit.self)
241241
}
242242

243+
public var asciiBuffer: UnsafeMutableBufferPointer<UTF8.CodeUnit>? {
244+
if elementWidth != 1 {
245+
return nil
246+
}
247+
return UnsafeMutableBufferPointer(start: startASCII, count: count)
248+
}
249+
243250
/// the native _StringBuffer, if any, or `nil`.
244251
public var nativeBuffer: _StringBuffer? {
245252
if !hasCocoaBuffer {

stdlib/public/core/StringLegacy.swift

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,18 @@ extension String {
147147
if prefixCount == 0 {
148148
return true
149149
}
150-
if selfCore.hasContiguousStorage && prefixCore.hasContiguousStorage {
151-
if selfCore.isASCII && prefixCore.isASCII {
152-
// Prefix longer than self.
153-
if prefixCount > selfCore.count {
154-
return false
155-
}
156-
return Int(_swift_stdlib_memcmp(
157-
selfCore.startASCII, prefixCore.startASCII, prefixCount)) == 0
150+
if let selfASCIIBuffer = selfCore.asciiBuffer,
151+
let prefixASCIIBuffer = prefixCore.asciiBuffer {
152+
if prefixASCIIBuffer.count > selfASCIIBuffer.count {
153+
// Prefix is longer than self.
154+
return false
158155
}
156+
return Int(_swift_stdlib_memcmp(
157+
selfASCIIBuffer.baseAddress!,
158+
prefixASCIIBuffer.baseAddress!,
159+
prefixASCIIBuffer.count)) == 0
160+
}
161+
if selfCore.hasContiguousStorage && prefixCore.hasContiguousStorage {
159162
let lhsStr = _NSContiguousString(selfCore)
160163
let rhsStr = _NSContiguousString(prefixCore)
161164
return lhsStr._unsafeWithNotEscapedSelfPointerPair(rhsStr) {
@@ -202,17 +205,19 @@ extension String {
202205
if suffixCount == 0 {
203206
return true
204207
}
205-
if selfCore.hasContiguousStorage && suffixCore.hasContiguousStorage {
206-
if selfCore.isASCII && suffixCore.isASCII {
207-
// Suffix longer than self.
208-
let selfCount = selfCore.count
209-
if suffixCount > selfCount {
210-
return false
211-
}
212-
return Int(_swift_stdlib_memcmp(
213-
selfCore.startASCII + (selfCount - suffixCount),
214-
suffixCore.startASCII, suffixCount)) == 0
208+
if let selfASCIIBuffer = selfCore.asciiBuffer,
209+
let suffixASCIIBuffer = suffixCore.asciiBuffer {
210+
if suffixASCIIBuffer.count > selfASCIIBuffer.count {
211+
// Suffix is longer than self.
212+
return false
215213
}
214+
return Int(_swift_stdlib_memcmp(
215+
selfASCIIBuffer.baseAddress!
216+
+ (selfASCIIBuffer.count - suffixASCIIBuffer.count),
217+
suffixASCIIBuffer.baseAddress!,
218+
suffixASCIIBuffer.count)) == 0
219+
}
220+
if selfCore.hasContiguousStorage && suffixCore.hasContiguousStorage {
216221
let lhsStr = _NSContiguousString(selfCore)
217222
let rhsStr = _NSContiguousString(suffixCore)
218223
return lhsStr._unsafeWithNotEscapedSelfPointerPair(rhsStr) {

stdlib/public/core/StringUTF8.swift

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,16 +35,17 @@ extension _StringCore {
3535
func _encodeSomeUTF8(from i: Int) -> (Int, _UTF8Chunk) {
3636
_sanityCheck(i <= count)
3737

38-
if _fastPath(elementWidth == 1) {
38+
if let asciiBuffer = self.asciiBuffer {
3939
// How many UTF-16 code units might we use before we've filled up
4040
// our _UTF8Chunk with UTF-8 code units?
41-
let utf16Count = Swift.min(MemoryLayout<_UTF8Chunk>.size, count - i)
41+
let utf16Count =
42+
Swift.min(MemoryLayout<_UTF8Chunk>.size, asciiBuffer.count - i)
4243

4344
var result: _UTF8Chunk = ~0 // Start with all bits set
4445

4546
_memcpy(
4647
dest: UnsafeMutableRawPointer(Builtin.addressof(&result)),
47-
src: startASCII + i,
48+
src: asciiBuffer.baseAddress! + i,
4849
size: numericCast(utf16Count))
4950

5051
// Convert the _UTF8Chunk into host endianness.
@@ -379,10 +380,6 @@ extension String {
379380
}
380381
}
381382

382-
public var _contiguousUTF8: UnsafeMutablePointer<UTF8.CodeUnit>? {
383-
return _core.elementWidth == 1 ? _core.startASCII : nil
384-
}
385-
386383
/// A contiguously stored null-terminated UTF-8 representation of the string.
387384
///
388385
/// To access the underlying memory, invoke `withUnsafeBufferPointer` on the
@@ -410,9 +407,10 @@ extension String {
410407
internal func _withUnsafeBufferPointerToUTF8<R>(
411408
_ body: (UnsafeBufferPointer<UTF8.CodeUnit>) throws -> R
412409
) rethrows -> R {
413-
let ptr = _contiguousUTF8
414-
if ptr != nil {
415-
return try body(UnsafeBufferPointer(start: ptr, count: _core.count))
410+
if let asciiBuffer = self._core.asciiBuffer {
411+
return try body(UnsafeBufferPointer(
412+
start: asciiBuffer.baseAddress,
413+
count: asciiBuffer.count))
416414
}
417415
var nullTerminatedUTF8 = ContiguousArray<UTF8.CodeUnit>()
418416
nullTerminatedUTF8.reserveCapacity(utf8.count + 1)

0 commit comments

Comments
 (0)