Skip to content

Commit

Permalink
change return type of String.fromCStringRepairingIllFormedUTF8
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxim Moiseev committed Dec 18, 2015
1 parent 156d29a commit d2be97d
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 17 deletions.
3 changes: 2 additions & 1 deletion stdlib/public/SDK/ObjectiveC/ObjectiveC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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>"
Expand Down
7 changes: 3 additions & 4 deletions stdlib/public/core/CString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,17 @@ 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
/// ill-formed UTF-8 code unit sequences, replaces them with replacement
/// 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,
Expand Down
3 changes: 2 additions & 1 deletion stdlib/public/core/Process.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 } ?? ""
}
}()

Expand Down
30 changes: 19 additions & 11 deletions test/1_stdlib/NSStringAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
}
Expand Down

0 comments on commit d2be97d

Please sign in to comment.