change return type of String.fromCStringRepairingIllFormedUTF8

This commit is contained in:
Maxim Moiseev
2015-11-10 14:58:47 -08:00
committed by Max Moiseev
parent 156d29a2af
commit d2be97d856
4 changed files with 26 additions and 17 deletions

View File

@@ -149,7 +149,8 @@ extension Selector : Equatable, Hashable {
extension Selector : CustomStringConvertible { extension Selector : CustomStringConvertible {
/// A textual representation of `self`. /// A textual representation of `self`.
public var description: String { public var description: String {
if let s = String.fromCStringRepairingIllFormedUTF8(sel_getName(self)).0 { if let (s, _) = String.fromCStringRepairingIllFormedUTF8(
sel_getName(self)) {
return s return s
} }
return "<NULL>" return "<NULL>"

View File

@@ -30,7 +30,7 @@ extension String {
input: UnsafeBufferPointer(start: UnsafeMutablePointer(cs), length: len)) 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`. /// referenced by a `CString`.
/// ///
/// Returns `nil` if the `CString` is `NULL`. If `CString` contains /// Returns `nil` if the `CString` is `NULL`. If `CString` contains
@@ -38,10 +38,9 @@ extension String {
/// characters (U+FFFD). /// characters (U+FFFD).
@warn_unused_result @warn_unused_result
public static func fromCStringRepairingIllFormedUTF8( public static func fromCStringRepairingIllFormedUTF8(
cs: UnsafePointer<CChar>) cs: UnsafePointer<CChar>) -> (String, hadError: Bool)? {
-> (String?, hadError: Bool) {
if cs._isNull { if cs._isNull {
return (nil, hadError: false) return nil
} }
let len = Int(_swift_stdlib_strlen(cs)) let len = Int(_swift_stdlib_strlen(cs))
let (result, hadError) = String._fromCodeUnitSequenceWithRepair(UTF8.self, let (result, hadError) = String._fromCodeUnitSequenceWithRepair(UTF8.self,

View File

@@ -17,7 +17,8 @@ public enum Process {
// Use lazy initialization of static properties to safely initialize the // Use lazy initialization of static properties to safely initialize the
// public 'arguments' property on first use. // public 'arguments' property on first use.
(0..<Int(argc)).map { i in (0..<Int(argc)).map { i in
String.fromCStringRepairingIllFormedUTF8(unsafeArgv[i]).0 ?? "" String.fromCStringRepairingIllFormedUTF8(unsafeArgv[i])
.map { $0.0 } ?? ""
} }
}() }()

View File

@@ -2215,29 +2215,37 @@ CStringTests.test("String.fromCString") {
CStringTests.test("String.fromCStringRepairingIllFormedUTF8") { CStringTests.test("String.fromCStringRepairingIllFormedUTF8") {
do { do {
let s = getNullCString() let s = getNullCString()
let (result, hadError) = String.fromCStringRepairingIllFormedUTF8(s) let result = String.fromCStringRepairingIllFormedUTF8(s)
expectEmpty(result) expectEmpty(result)
expectFalse(hadError)
} }
do { do {
let (s, dealloc) = getASCIICString() let (s, dealloc) = getASCIICString()
let (result, hadError) = String.fromCStringRepairingIllFormedUTF8(s) if let (result, hadError) = String.fromCStringRepairingIllFormedUTF8(s) {
expectOptionalEqual("ab", result) expectOptionalEqual("ab", result)
expectFalse(hadError) expectFalse(hadError)
} else {
expectTrue(false, "Expected .Some()")
}
dealloc() dealloc()
} }
do { do {
let (s, dealloc) = getNonASCIICString() let (s, dealloc) = getNonASCIICString()
let (result, hadError) = String.fromCStringRepairingIllFormedUTF8(s) if let (result, hadError) = String.fromCStringRepairingIllFormedUTF8(s) {
expectOptionalEqual("аб", result) expectOptionalEqual("аб", result)
expectFalse(hadError) expectFalse(hadError)
} else {
expectTrue(false, "Expected .Some()")
}
dealloc() dealloc()
} }
do { do {
let (s, dealloc) = getIllFormedUTF8String1() let (s, dealloc) = getIllFormedUTF8String1()
let (result, hadError) = String.fromCStringRepairingIllFormedUTF8(s) if let (result, hadError) = String.fromCStringRepairingIllFormedUTF8(s) {
expectOptionalEqual("\u{41}\u{fffd}\u{fffd}\u{fffd}\u{41}", result) expectOptionalEqual("\u{41}\u{fffd}\u{fffd}\u{fffd}\u{41}", result)
expectTrue(hadError) expectTrue(hadError)
} else {
expectTrue(false, "Expected .Some()")
}
dealloc() dealloc()
} }
} }