[stdlib] String API Review: NSString API

Many changes in how we're presenting the NSString APIs on String, most
notably that we now traffic in String.Index and Range<String.Index>
rather than Int and NSRange.  Also we present NSString initializers that
can fail only as factory functions, and factory functions that can't
fail only as init functions.

About 25% of the API changes here have been reviewd by the Foundation
guys, and testing is, as it has always been, admittedly spotty.  Dmitri
is going to be writing some more comprehensive tests.

Swift SVN r18553
This commit is contained in:
Dave Abrahams
2014-05-22 04:21:55 +00:00
parent 80f8b1a4ea
commit 0f9edadc41
5 changed files with 718 additions and 753 deletions

View File

@@ -5,20 +5,16 @@
import Foundation
func testFindFileAndURL(path: String) {
var err: NSError? = .None
var usedEncoding = NSStringEncoding()
var content = String.stringWithContentsOfFile(
path, usedEncoding: &usedEncoding, error: &err)
var err: NSError?
var content = String.stringWithContentsOfFile(path, error: &err)
println("error: " + (err ? err.description : "<no error>"))
println("content: " + (content ? content!._lines[0] : "<no content>"))
var url = NSURL.URLWithString("file://" + path)
err = .None
content = String.stringWithContentsOfURL(
url, usedEncoding: &usedEncoding, error: &err)
err = nil
content = String.stringWithContentsOfURL(url, error: &err)
println("error: " + (err ? err.description : "<no error>"))
println("content: " + (content ? content!._lines[0] : "<no content>"))
@@ -40,18 +36,19 @@ func testClassMethods() {
}
// CHECK-NEXT: It is called
println("It is called \"\(String.localizedNameOfStringEncoding(defaultCStringEncoding))\"")
println(
"It is called \"" +
String.localizedNameOfStringEncoding(defaultCStringEncoding) + "\"")
var path = String.pathWithComponents(["flugelhorn", "baritone", "bass"])
// CHECK-NEXT: <flugelhorn/baritone/bass>
println("<\(path)>")
// CHECK-NEXT: true
println(String.string() == "")
// CHECK-NEXT: <sox>
var chars: unichar[] = [ unichar("s".value), unichar("o".value), unichar("x".value) ]
var sox: String = String.stringWithCharacters(chars)
var chars: unichar[] = [
unichar("s".value), unichar("o".value), unichar("x".value) ]
var sox: String = String(utf16CodeUnits: chars, count: chars.count)
println("<\(sox)>")
var pathToThisSource = Process.arguments[1]
@@ -70,22 +67,32 @@ func testClassMethods() {
testFindFileAndURL(pathToThisSource)
// CHECK-NEXT: foo, a basmati bar!
println(String.stringWithCString("foo, a basmati bar!", encoding: String.defaultCStringEncoding()))
println(
String.stringWithCString(
"foo, a basmati bar!", encoding: String.defaultCStringEncoding()))
var emptyString = ""
// CHECK-NEXT: {{.*}} has 0 completions and the longest is <>
var outputName: String? = ""
var count = nonExistentPath.completePathIntoString(&outputName, caseSensitive: false)
println("<\(nonExistentPath)> has \(count) completions and the longest is <\(outputName ? outputName! : emptyString)>")
// CHECK-NEXT: {{.*}} has 0 completions and the longest is <None Found>
var outputName = "None Found"
var count = nonExistentPath.completePathIntoString(
&outputName, caseSensitive: false)
println(
"<\(nonExistentPath)> has \(count) "
+ "completions and the longest is <\(outputName)>")
// CHECK-NEXT: <[[THISPATH:.*]]> has 1 completions and the longest is <[[THISPATH]]>
count = pathToThisSource.completePathIntoString(&outputName, caseSensitive: false)
println("<\(pathToThisSource)> has \(count) completions and the longest is <\(outputName ? outputName! : emptyString)>")
count = pathToThisSource.completePathIntoString(
&outputName, caseSensitive: false)
println(
"<\(pathToThisSource)> has \(count) "
+ "completions and the longest is <\(outputName)>")
var world: NSString = "world"
// CHECK-NEXT: Hello, world!%42
println(String.stringWithFormat("Hello, %@!%%%ld", world, 42))
println(String(format: "Hello, %@!%%%ld", world, 42))
}