mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
stdlib/NSString APIs on String: fix two bugs and add more tests
stringWithBytes(_:length:encoding:) was passing an array to Objective-C incorrectly, and a garbage NSString was being constructed as a result. Unhide the initializer was accidentally hidden. Swift SVN r18674
This commit is contained in:
@@ -724,7 +724,7 @@ extension String {
|
||||
encoding: NSStringEncoding
|
||||
) -> String? {
|
||||
return NSString(
|
||||
bytes: &bytes, length: min(bytes.count, length), encoding: encoding
|
||||
bytes: bytes, length: min(bytes.count, length), encoding: encoding
|
||||
) as NSString? // HACK: FIXME: coerce to optional NSString to handle nil
|
||||
}
|
||||
|
||||
@@ -818,8 +818,8 @@ extension String {
|
||||
/// Returns a `String` object initialized by using a given
|
||||
/// format string as a template into which the remaining argument
|
||||
/// values are substituted.
|
||||
init(format: String, _ _arguments: CVarArg...) {
|
||||
self = String(format: format, arguments: _arguments)
|
||||
init(format: String, _ arguments: CVarArg...) {
|
||||
self = String(format: format, arguments: arguments)
|
||||
}
|
||||
|
||||
// - (instancetype)
|
||||
|
||||
@@ -776,43 +776,65 @@ NSStringAPIs.test("hash") {
|
||||
}
|
||||
|
||||
NSStringAPIs.test("stringWithBytes(_:length:encoding:)") {
|
||||
/*
|
||||
Tests disabled because of:
|
||||
<rdar://problem/17034498> NSString(bytes:length:encoding:) constructs a garbage NSString
|
||||
|
||||
var s: String = "abc あかさた"
|
||||
var bytes: UInt8[] = Array(s.utf8)
|
||||
dump(bytes)
|
||||
expectEqual(s, String.stringWithBytes(bytes, length: bytes.count,
|
||||
encoding: NSUTF8StringEncoding))
|
||||
|
||||
/*
|
||||
FIXME: Test disabled because the NSString documentation is unclear about
|
||||
what should actually happen in this case.
|
||||
|
||||
expectEmpty(String.stringWithBytes(bytes, length: bytes.count,
|
||||
encoding: NSASCIIStringEncoding))
|
||||
*/
|
||||
|
||||
// FIXME: add a test where this function actually returns nil.
|
||||
}
|
||||
|
||||
NSStringAPIs.test("stringWithBytesNoCopy(_:length:encoding:freeWhenDone:)") {
|
||||
// FIXME
|
||||
var s: String = "abc あかさた"
|
||||
var bytes: UInt8[] = Array(s.utf8)
|
||||
expectEqual(s, String.stringWithBytesNoCopy(&bytes, length: bytes.count,
|
||||
encoding: NSUTF8StringEncoding, freeWhenDone: false))
|
||||
|
||||
/*
|
||||
FIXME: Test disabled because the NSString documentation is unclear about
|
||||
what should actually happen in this case.
|
||||
|
||||
expectEmpty(String.stringWithBytesNoCopy(&bytes, length: bytes.count,
|
||||
encoding: NSASCIIStringEncoding, freeWhenDone: false))
|
||||
*/
|
||||
|
||||
// FIXME: add a test where this function actually returns nil.
|
||||
}
|
||||
|
||||
NSStringAPIs.test("init(utf16CodeUnits:count:)") {
|
||||
var chars: unichar[] = [
|
||||
unichar("s".value), unichar("o".value), unichar("x".value) ]
|
||||
let expected = "abc абв \U0001F60A"
|
||||
let chars: unichar[] = Array(expected.utf16)
|
||||
|
||||
var sox = String(utf16CodeUnits: chars, count: chars.count)
|
||||
expectEqual("sox", sox)
|
||||
expectEqual(expected, String(utf16CodeUnits: chars, count: chars.count))
|
||||
}
|
||||
|
||||
NSStringAPIs.test("init(utf16CodeUnitsNoCopy:count:freeWhenDone:)") {
|
||||
// FIXME
|
||||
let expected = "abc абв \U0001F60A"
|
||||
let chars: unichar[] = Array(expected.utf16)
|
||||
|
||||
expectEqual(expected, String(utf16CodeUnitsNoCopy: chars,
|
||||
count: chars.count, freeWhenDone: false))
|
||||
}
|
||||
|
||||
NSStringAPIs.test("init(format:_:...)") {
|
||||
var world: NSString = "world"
|
||||
expectEqual("Hello, world!%42", String(format: "Hello, %@!%%%ld", world, 42))
|
||||
let world: NSString = "world"
|
||||
expectEqual("Hello, world!%42",
|
||||
String(format: "Hello, %@!%%%ld", world, 42))
|
||||
}
|
||||
|
||||
NSStringAPIs.test("init(format:arguments:)") {
|
||||
// FIXME
|
||||
let world: NSString = "world"
|
||||
let args: CVarArg[] = [ world, 42 ]
|
||||
expectEqual("Hello, world!%42",
|
||||
String(format: "Hello, %@!%%%ld", arguments: args))
|
||||
}
|
||||
|
||||
NSStringAPIs.test("init(format:locale:_:...)") {
|
||||
@@ -824,7 +846,12 @@ NSStringAPIs.test("init(format:locale:_:...)") {
|
||||
}
|
||||
|
||||
NSStringAPIs.test("init(format:locale:arguments:)") {
|
||||
// FIXME
|
||||
let world: NSString = "world"
|
||||
let args: CVarArg[] = [ world, 42 ]
|
||||
expectEqual("Hello, world!%42", String(format: "Hello, %@!%%%ld",
|
||||
locale: nil, arguments: args))
|
||||
expectEqual("Hello, world!%42", String(format: "Hello, %@!%%%ld",
|
||||
locale: NSLocale.systemLocale(), arguments: args))
|
||||
}
|
||||
|
||||
NSStringAPIs.test("lastPathComponent") {
|
||||
|
||||
Reference in New Issue
Block a user