mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
change return type of String.fromCStringRepairingIllFormedUTF8
This commit is contained in:
committed by
Max Moiseev
parent
156d29a2af
commit
d2be97d856
@@ -149,7 +149,8 @@ extension Selector : Equatable, Hashable {
|
||||
extension Selector : CustomStringConvertible {
|
||||
/// A textual representation of `self`.
|
||||
public var description: String {
|
||||
if let s = String.fromCStringRepairingIllFormedUTF8(sel_getName(self)).0 {
|
||||
if let (s, _) = String.fromCStringRepairingIllFormedUTF8(
|
||||
sel_getName(self)) {
|
||||
return s
|
||||
}
|
||||
return "<NULL>"
|
||||
|
||||
@@ -30,7 +30,7 @@ extension String {
|
||||
input: UnsafeBufferPointer(start: UnsafeMutablePointer(cs), length: len))
|
||||
}
|
||||
|
||||
/// Creates a new `String` by copying the nul-terminated UTF-8 data
|
||||
/// Create a new `String` by copying the nul-terminated UTF-8 data
|
||||
/// referenced by a `CString`.
|
||||
///
|
||||
/// Returns `nil` if the `CString` is `NULL`. If `CString` contains
|
||||
@@ -38,10 +38,9 @@ extension String {
|
||||
/// characters (U+FFFD).
|
||||
@warn_unused_result
|
||||
public static func fromCStringRepairingIllFormedUTF8(
|
||||
cs: UnsafePointer<CChar>)
|
||||
-> (String?, hadError: Bool) {
|
||||
cs: UnsafePointer<CChar>) -> (String, hadError: Bool)? {
|
||||
if cs._isNull {
|
||||
return (nil, hadError: false)
|
||||
return nil
|
||||
}
|
||||
let len = Int(_swift_stdlib_strlen(cs))
|
||||
let (result, hadError) = String._fromCodeUnitSequenceWithRepair(UTF8.self,
|
||||
|
||||
@@ -17,7 +17,8 @@ public enum Process {
|
||||
// Use lazy initialization of static properties to safely initialize the
|
||||
// public 'arguments' property on first use.
|
||||
(0..<Int(argc)).map { i in
|
||||
String.fromCStringRepairingIllFormedUTF8(unsafeArgv[i]).0 ?? ""
|
||||
String.fromCStringRepairingIllFormedUTF8(unsafeArgv[i])
|
||||
.map { $0.0 } ?? ""
|
||||
}
|
||||
}()
|
||||
|
||||
|
||||
@@ -2215,29 +2215,37 @@ CStringTests.test("String.fromCString") {
|
||||
CStringTests.test("String.fromCStringRepairingIllFormedUTF8") {
|
||||
do {
|
||||
let s = getNullCString()
|
||||
let (result, hadError) = String.fromCStringRepairingIllFormedUTF8(s)
|
||||
let result = String.fromCStringRepairingIllFormedUTF8(s)
|
||||
expectEmpty(result)
|
||||
expectFalse(hadError)
|
||||
}
|
||||
do {
|
||||
let (s, dealloc) = getASCIICString()
|
||||
let (result, hadError) = String.fromCStringRepairingIllFormedUTF8(s)
|
||||
expectOptionalEqual("ab", result)
|
||||
expectFalse(hadError)
|
||||
if let (result, hadError) = String.fromCStringRepairingIllFormedUTF8(s) {
|
||||
expectOptionalEqual("ab", result)
|
||||
expectFalse(hadError)
|
||||
} else {
|
||||
expectTrue(false, "Expected .Some()")
|
||||
}
|
||||
dealloc()
|
||||
}
|
||||
do {
|
||||
let (s, dealloc) = getNonASCIICString()
|
||||
let (result, hadError) = String.fromCStringRepairingIllFormedUTF8(s)
|
||||
expectOptionalEqual("аб", result)
|
||||
expectFalse(hadError)
|
||||
if let (result, hadError) = String.fromCStringRepairingIllFormedUTF8(s) {
|
||||
expectOptionalEqual("аб", result)
|
||||
expectFalse(hadError)
|
||||
} else {
|
||||
expectTrue(false, "Expected .Some()")
|
||||
}
|
||||
dealloc()
|
||||
}
|
||||
do {
|
||||
let (s, dealloc) = getIllFormedUTF8String1()
|
||||
let (result, hadError) = String.fromCStringRepairingIllFormedUTF8(s)
|
||||
expectOptionalEqual("\u{41}\u{fffd}\u{fffd}\u{fffd}\u{41}", result)
|
||||
expectTrue(hadError)
|
||||
if let (result, hadError) = String.fromCStringRepairingIllFormedUTF8(s) {
|
||||
expectOptionalEqual("\u{41}\u{fffd}\u{fffd}\u{fffd}\u{41}", result)
|
||||
expectTrue(hadError)
|
||||
} else {
|
||||
expectTrue(false, "Expected .Some()")
|
||||
}
|
||||
dealloc()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user