Skip to content

[DO NOT MERGE] Rename nulTerminatedUTF8 and nulTerminatedUTF8CString #3742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion stdlib/private/SwiftPrivate/IO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public struct _FDOutputStream : TextOutputStream {
}

public mutating func write(_ string: String) {
let utf8 = string.nulTerminatedUTF8
let utf8 = string.nullTerminatedUTF8
utf8.withUnsafeBufferPointer {
(utf8) -> Void in
var writtenBytes = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public func _stdlib_mkstemps(_ template: inout String, _ suffixlen: CInt) -> CIn
#if os(Android)
preconditionFailure("mkstemps doesn't work on Android")
#else
var utf8 = template.nulTerminatedUTF8
var utf8 = template.nullTerminatedUTF8
let (fd, fileName) = utf8.withUnsafeMutableBufferPointer {
(utf8) -> (CInt, String) in
let fd = mkstemps(UnsafeMutablePointer(utf8.baseAddress!), suffixlen)
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/LifetimeManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ extension String {
public func withCString<Result>(
_ body: @noescape (UnsafePointer<Int8>) throws -> Result
) rethrows -> Result {
return try self.nulTerminatedUTF8.withUnsafeBufferPointer {
return try self.nullTerminatedUTF8.withUnsafeBufferPointer {
try body(UnsafePointer($0.baseAddress!))
}
}
Expand Down
2 changes: 1 addition & 1 deletion stdlib/public/core/Pointer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,6 @@ public // COMPILER_INTRINSIC
func _convertConstStringToUTF8PointerArgument<
ToPointer : _Pointer
>(_ str: String) -> (AnyObject?, ToPointer) {
let utf8 = Array(str.nulTerminatedUTF8)
let utf8 = Array(str.nullTerminatedUTF8)
return _convertConstArrayToPointerArgument(utf8)
}
23 changes: 17 additions & 6 deletions stdlib/public/core/StringUTF8.swift
Original file line number Diff line number Diff line change
Expand Up @@ -390,15 +390,15 @@ extension String {
/// `withUnsafeBufferPointer` on the array.
///
/// let s = "Hello!"
/// let bytes = s.nulTerminatedUTF8
/// let bytes = s.nullTerminatedUTF8
/// print(bytes)
/// // Prints "[72, 101, 108, 108, 111, 33, 0]"
///
/// bytes.withUnsafeBufferPointer { ptr in
/// print(strlen(UnsafePointer(ptr.baseAddress!)))
/// }
/// // Prints "6"
public var nulTerminatedUTF8: ContiguousArray<UTF8.CodeUnit> {
public var nullTerminatedUTF8: ContiguousArray<UTF8.CodeUnit> {
var result = ContiguousArray<UTF8.CodeUnit>()
result.reserveCapacity(utf8.count + 1)
result += utf8
Expand All @@ -409,9 +409,9 @@ extension String {
/// A contiguously stored null-terminated UTF-8 representation of
/// the string.
///
/// This is a variation on nulTerminatedUTF8 that creates an array
/// of element type CChar for use with CString API's.
public var nulTerminatedUTF8CString: ContiguousArray<CChar> {
/// This is a variation on `nullTerminatedUTF8` that creates an array
/// of element type CChar for use with CString APIs.
public var utf8CString: ContiguousArray<CChar> {
var result = ContiguousArray<CChar>()
result.reserveCapacity(utf8.count + 1)
for c in utf8 {
Expand All @@ -428,7 +428,7 @@ extension String {
if ptr != nil {
return try body(UnsafeBufferPointer(start: ptr, count: _core.count))
}
return try nulTerminatedUTF8.withUnsafeBufferPointer(body)
return try nullTerminatedUTF8.withUnsafeBufferPointer(body)
}

/// Creates a string corresponding to the given sequence of UTF-8 code units.
Expand Down Expand Up @@ -725,3 +725,14 @@ extension String.UTF8View : CustomPlaygroundQuickLookable {
}
}

extension String {
@available(*, unavailable, renamed: "nullTerminatedUTF8")
public var nulTerminatedUTF8: ContiguousArray<UTF8.CodeUnit> {
Builtin.unreachable()
}

@available(*, unavailable, renamed: "utf8CString")
public var nulTerminatedUTF8CString: ContiguousArray<CChar> {
Builtin.unreachable()
}
}
2 changes: 1 addition & 1 deletion test/1_stdlib/Runtime.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func _stdlib_demangleImpl(
) -> UnsafeMutablePointer<CChar>?

func _stdlib_demangleName(_ mangledName: String) -> String {
return mangledName.nulTerminatedUTF8.withUnsafeBufferPointer {
return mangledName.nullTerminatedUTF8.withUnsafeBufferPointer {
(mangledNameUTF8) in

let demangledNamePtr = _stdlib_demangleImpl(
Expand Down
10 changes: 5 additions & 5 deletions test/1_stdlib/StringAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -483,19 +483,19 @@ CStringTests.test("String.decodeCString") {
}
}

CStringTests.test("String.nulTerminatedUTF8") {
CStringTests.test("String.nullTerminatedUTF8") {
do {
let (cstr, dealloc) = getASCIICString()
let str = String(cString: cstr)
expectEqualCString(cstr, str.nulTerminatedUTF8)
expectEqualCString(cstr, str.nulTerminatedUTF8CString)
expectEqualCString(cstr, str.nullTerminatedUTF8)
expectEqualCString(cstr, str.utf8CString)
dealloc()
}
do {
let (cstr, dealloc) = getNonASCIICString()
let str = String(cString: cstr)
expectEqualCString(cstr, str.nulTerminatedUTF8)
expectEqualCString(cstr, str.nulTerminatedUTF8CString)
expectEqualCString(cstr, str.nullTerminatedUTF8)
expectEqualCString(cstr, str.utf8CString)
dealloc()
}
}
Expand Down
2 changes: 1 addition & 1 deletion test/1_stdlib/TestData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ class TestData : TestDataSuper {
// String of course has its own way to get data, but this way tests our own data struct
func dataFrom(_ string : String) -> Data {
// Create a Data out of those bytes
return string.nulTerminatedUTF8.withUnsafeBufferPointer { (ptr) in
return string.nullTerminatedUTF8.withUnsafeBufferPointer { (ptr) in
// Subtract 1 so we don't get the null terminator byte. This matches NSString behavior.
return Data(bytes: ptr.baseAddress!, count: ptr.count - 1)
}
Expand Down