Skip to content

Commit b0fdb80

Browse files
authored
Merge pull request #3816 from xwu/se-0134
[SE-0134][stdlib] Rename/remove two properties on String
2 parents 58fb4ef + b04830f commit b0fdb80

File tree

8 files changed

+45
-49
lines changed

8 files changed

+45
-49
lines changed

stdlib/private/SwiftPrivate/IO.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -100,14 +100,14 @@ public struct _FDOutputStream : TextOutputStream {
100100
}
101101

102102
public mutating func write(_ string: String) {
103-
let utf8 = string.nulTerminatedUTF8
104-
utf8.withUnsafeBufferPointer {
105-
(utf8) -> Void in
103+
let utf8CStr = string.utf8CString
104+
utf8CStr.withUnsafeBufferPointer {
105+
(utf8CStr) -> Void in
106106
var writtenBytes = 0
107-
let bufferSize = utf8.count - 1
107+
let bufferSize = utf8CStr.count - 1
108108
while writtenBytes != bufferSize {
109109
let result = _swift_stdlib_write(
110-
self.fd, UnsafePointer(utf8.baseAddress! + Int(writtenBytes)),
110+
self.fd, UnsafeRawPointer(utf8CStr.baseAddress! + Int(writtenBytes)),
111111
bufferSize - writtenBytes)
112112
if result < 0 {
113113
fatalError("write() returned an error")

stdlib/private/SwiftPrivateLibcExtras/SwiftPrivateLibcExtras.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ public func _stdlib_mkstemps(_ template: inout String, _ suffixlen: CInt) -> CIn
2222
#if os(Android)
2323
preconditionFailure("mkstemps doesn't work on Android")
2424
#else
25-
var utf8 = template.nulTerminatedUTF8
26-
let (fd, fileName) = utf8.withUnsafeMutableBufferPointer {
27-
(utf8) -> (CInt, String) in
28-
let fd = mkstemps(UnsafeMutablePointer(utf8.baseAddress!), suffixlen)
29-
let fileName = String(cString: utf8.baseAddress!)
25+
var utf8CStr = template.utf8CString
26+
let (fd, fileName) = utf8CStr.withUnsafeMutableBufferPointer {
27+
(utf8CStr) -> (CInt, String) in
28+
let fd = mkstemps(utf8CStr.baseAddress!, suffixlen)
29+
let fileName = String(cString: utf8CStr.baseAddress!)
3030
return (fd, fileName)
3131
}
3232
template = fileName

stdlib/public/core/LifetimeManager.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ extension String {
4343
public func withCString<Result>(
4444
_ body: @noescape (UnsafePointer<Int8>) throws -> Result
4545
) rethrows -> Result {
46-
return try self.nulTerminatedUTF8.withUnsafeBufferPointer {
47-
try body(UnsafePointer($0.baseAddress!))
46+
return try self.utf8CString.withUnsafeBufferPointer {
47+
try body($0.baseAddress!)
4848
}
4949
}
5050
}

stdlib/public/core/Pointer.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,6 @@ public // COMPILER_INTRINSIC
8787
func _convertConstStringToUTF8PointerArgument<
8888
ToPointer : _Pointer
8989
>(_ str: String) -> (AnyObject?, ToPointer) {
90-
let utf8 = Array(str.nulTerminatedUTF8)
90+
let utf8 = Array(str.utf8CString)
9191
return _convertConstArrayToPointerArgument(utf8)
9292
}

stdlib/public/core/StringUTF8.swift

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -383,35 +383,21 @@ extension String {
383383
return _core.elementWidth == 1 ? _core.startASCII : nil
384384
}
385385

386-
/// A contiguously stored null-terminated UTF-8 representation of
387-
/// the string.
386+
/// A contiguously stored null-terminated UTF-8 representation of the string.
388387
///
389-
/// To access the underlying memory, invoke
390-
/// `withUnsafeBufferPointer` on the array.
388+
/// To access the underlying memory, invoke `withUnsafeBufferPointer` on the
389+
/// array.
391390
///
392391
/// let s = "Hello!"
393-
/// let bytes = s.nulTerminatedUTF8
392+
/// let bytes = s.utf8CString
394393
/// print(bytes)
395394
/// // Prints "[72, 101, 108, 108, 111, 33, 0]"
396-
///
395+
///
397396
/// bytes.withUnsafeBufferPointer { ptr in
398-
/// print(strlen(UnsafePointer(ptr.baseAddress!)))
397+
/// print(strlen(ptr.baseAddress!))
399398
/// }
400399
/// // Prints "6"
401-
public var nulTerminatedUTF8: ContiguousArray<UTF8.CodeUnit> {
402-
var result = ContiguousArray<UTF8.CodeUnit>()
403-
result.reserveCapacity(utf8.count + 1)
404-
result += utf8
405-
result.append(0)
406-
return result
407-
}
408-
409-
/// A contiguously stored null-terminated UTF-8 representation of
410-
/// the string.
411-
///
412-
/// This is a variation on nulTerminatedUTF8 that creates an array
413-
/// of element type CChar for use with CString API's.
414-
public var nulTerminatedUTF8CString: ContiguousArray<CChar> {
400+
public var utf8CString: ContiguousArray<CChar> {
415401
var result = ContiguousArray<CChar>()
416402
result.reserveCapacity(utf8.count + 1)
417403
for c in utf8 {
@@ -428,7 +414,11 @@ extension String {
428414
if ptr != nil {
429415
return try body(UnsafeBufferPointer(start: ptr, count: _core.count))
430416
}
431-
return try nulTerminatedUTF8.withUnsafeBufferPointer(body)
417+
var nullTerminatedUTF8 = ContiguousArray<UTF8.CodeUnit>()
418+
nullTerminatedUTF8.reserveCapacity(utf8.count + 1)
419+
nullTerminatedUTF8 += utf8
420+
nullTerminatedUTF8.append(0)
421+
return try nullTerminatedUTF8.withUnsafeBufferPointer(body)
432422
}
433423

434424
/// Creates a string corresponding to the given sequence of UTF-8 code units.
@@ -725,3 +715,9 @@ extension String.UTF8View : CustomPlaygroundQuickLookable {
725715
}
726716
}
727717

718+
extension String {
719+
@available(*, unavailable, message: "Please use String.utf8CString instead.")
720+
public var nulTerminatedUTF8: ContiguousArray<UTF8.CodeUnit> {
721+
Builtin.unreachable()
722+
}
723+
}

test/1_stdlib/Runtime.swift.gyb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,20 @@ import SwiftShims
1313
@_silgen_name("swift_demangle")
1414
public
1515
func _stdlib_demangleImpl(
16-
mangledName: UnsafePointer<UInt8>?,
16+
mangledName: UnsafePointer<CChar>?,
1717
mangledNameLength: UInt,
18-
outputBuffer: UnsafeMutablePointer<UInt8>?,
18+
outputBuffer: UnsafeMutablePointer<CChar>?,
1919
outputBufferSize: UnsafeMutablePointer<UInt>?,
2020
flags: UInt32
2121
) -> UnsafeMutablePointer<CChar>?
2222

2323
func _stdlib_demangleName(_ mangledName: String) -> String {
24-
return mangledName.nulTerminatedUTF8.withUnsafeBufferPointer {
25-
(mangledNameUTF8) in
24+
return mangledName.utf8CString.withUnsafeBufferPointer {
25+
(mangledNameUTF8CStr) in
2626

2727
let demangledNamePtr = _stdlib_demangleImpl(
28-
mangledName: mangledNameUTF8.baseAddress,
29-
mangledNameLength: UInt(mangledNameUTF8.count - 1),
28+
mangledName: mangledNameUTF8CStr.baseAddress,
29+
mangledNameLength: UInt(mangledNameUTF8CStr.count - 1),
3030
outputBuffer: nil,
3131
outputBufferSize: nil,
3232
flags: 0)

test/1_stdlib/StringAPI.swift

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -483,19 +483,17 @@ CStringTests.test("String.decodeCString") {
483483
}
484484
}
485485

486-
CStringTests.test("String.nulTerminatedUTF8") {
486+
CStringTests.test("String.utf8CString") {
487487
do {
488488
let (cstr, dealloc) = getASCIICString()
489489
let str = String(cString: cstr)
490-
expectEqualCString(cstr, str.nulTerminatedUTF8)
491-
expectEqualCString(cstr, str.nulTerminatedUTF8CString)
490+
expectEqualCString(cstr, str.utf8CString)
492491
dealloc()
493492
}
494493
do {
495494
let (cstr, dealloc) = getNonASCIICString()
496495
let str = String(cString: cstr)
497-
expectEqualCString(cstr, str.nulTerminatedUTF8)
498-
expectEqualCString(cstr, str.nulTerminatedUTF8CString)
496+
expectEqualCString(cstr, str.utf8CString)
499497
dealloc()
500498
}
501499
}

test/1_stdlib/TestData.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,11 @@ class TestData : TestDataSuper {
127127
// String of course has its own way to get data, but this way tests our own data struct
128128
func dataFrom(_ string : String) -> Data {
129129
// Create a Data out of those bytes
130-
return string.nulTerminatedUTF8.withUnsafeBufferPointer { (ptr) in
131-
// Subtract 1 so we don't get the null terminator byte. This matches NSString behavior.
132-
return Data(bytes: ptr.baseAddress!, count: ptr.count - 1)
130+
return string.utf8CString.withUnsafeBufferPointer { (ptr) in
131+
ptr.baseAddress!.withMemoryRebound(to: UInt8.self, capacity: ptr.count) {
132+
// Subtract 1 so we don't get the null terminator byte. This matches NSString behavior.
133+
return Data(bytes: $0, count: ptr.count - 1)
134+
}
133135
}
134136
}
135137

0 commit comments

Comments
 (0)