[stdlib] String internal API review changes

I had to XFAIL test/ClangModules/cf.swift, which is failing for reasons
I can't understand.  <rdar://problem/16911496>

Swift SVN r18071
This commit is contained in:
Dave Abrahams
2014-05-14 14:18:52 +00:00
parent eb9c3a406d
commit a8bbc4c89b
14 changed files with 199 additions and 225 deletions

View File

@@ -99,8 +99,7 @@ extension String {
func withCString<Result>(
f: (CString)->Result
) -> Result {
var u8 = self.nulTerminatedUTF8()
return u8.buffer.withUnsafePointerToElements {
return self.nulTerminatedUTF8.withUnsafePointerToElements {
f(CString($0))
}
}
@@ -111,8 +110,7 @@ extension String {
func withCString<Result>(
f: (UnsafePointer<CChar>)->Result
) -> Result {
var u8 = self.nulTerminatedUTF8()
return u8.buffer.withUnsafePointerToElements {
return self.nulTerminatedUTF8.withUnsafePointerToElements {
f(UnsafePointer($0))
}
}

View File

@@ -75,8 +75,7 @@ extension String {
}
extension String {
// FIXME: Locales make this interesting
var uppercase : String {
var uppercaseString : String {
let end = utf8.endIndex
var resultArray = NativeArray<UTF8.CodeUnit>(count: countElements(utf8),
value: 0)
@@ -112,7 +111,7 @@ extension String {
return String(UTF8.self, input: resultArray)
}
var lowercase : String {
var lowercaseString : String {
let end = utf8.endIndex
var resultArray = NativeArray<UTF8.CodeUnit>(count: countElements(utf8),
value: 0)
@@ -158,17 +157,17 @@ extension String {
return true
}
func startsWith(prefix: String) -> Bool {
func hasPrefix(prefix: String) -> Bool {
return Swift.startsWith(self, prefix)
}
func endsWith(suffix: String) -> Bool {
func hasSuffix(suffix: String) -> Bool {
return Swift.startsWith(Reverse(self), Reverse(suffix))
}
func isAlpha() -> Bool { return _isAll({ $0.isAlpha() }) }
func isDigit() -> Bool { return _isAll({ $0.isDigit() }) }
func isSpace() -> Bool { return _isAll({ $0.isSpace() }) }
func _isAlpha() -> Bool { return _isAll({ $0.isAlpha() }) }
func _isDigit() -> Bool { return _isAll({ $0.isDigit() }) }
func _isSpace() -> Bool { return _isAll({ $0.isSpace() }) }
}
/// \brief Represent a positive integer value in the given radix,
@@ -215,9 +214,9 @@ func _formatSignedInteger(
// Conversions to string from other types.
extension String {
init(_ v: Int64, radix: Int = 10, uppercase: Bool = false) {
init(_ v: Int64, radix: Int = 10, _uppercase: Bool = false) {
var format = _formatSignedInteger(v, UInt64(radix),
ten: uppercase ? "A" : "a")
ten: _uppercase ? "A" : "a")
var asciiCount = 0
format(stream: { _ in ++asciiCount;() })
var buffer = _StringBuffer(
@@ -228,9 +227,9 @@ extension String {
}
// FIXME: This function assumes UTF16
init(_ v: UInt64, radix: Int = 10, uppercase: Bool = false) {
init(_ v: UInt64, radix: Int = 10, _uppercase: Bool = false) {
var format = _formatPositiveInteger(v, UInt64(radix),
ten: uppercase ? "A" : "a")
ten: _uppercase ? "A" : "a")
var asciiCount = v == 0 ? 1 : 0
format(stream: { _ in ++asciiCount;() })
var buffer = _StringBuffer(
@@ -243,40 +242,44 @@ extension String {
self = String(buffer)
}
init(_ v : Int8, radix : Int = 10, uppercase : Bool = false) {
self = String(Int64(v), radix: radix, uppercase: uppercase)
init(_ v : Int8, radix : Int = 10, _uppercase : Bool = false) {
self = String(Int64(v), radix: radix, _uppercase: _uppercase)
}
init(_ v : Int16, radix : Int = 10, uppercase : Bool = false) {
self = String(Int64(v), radix: radix, uppercase: uppercase)
init(_ v : Int16, radix : Int = 10, _uppercase : Bool = false) {
self = String(Int64(v), radix: radix, _uppercase: _uppercase)
}
init(_ v : Int32, radix : Int = 10, uppercase : Bool = false) {
self = String(Int64(v), radix: radix, uppercase: uppercase)
init(_ v : Int32, radix : Int = 10, _uppercase : Bool = false) {
self = String(Int64(v), radix: radix, _uppercase: _uppercase)
}
init(_ v : Int, radix : Int = 10, uppercase : Bool = false) {
self = String(Int64(v), radix: radix, uppercase: uppercase)
init(_ v : Int, radix : Int = 10, _uppercase : Bool = false) {
self = String(Int64(v), radix: radix, _uppercase: _uppercase)
}
init(_ v : UInt8, radix : Int = 10, uppercase : Bool = false) {
self = String(UInt64(v), radix: radix, uppercase: uppercase)
init(_ v : UInt8, radix : Int = 10, _uppercase : Bool = false) {
self = String(UInt64(v), radix: radix, _uppercase: _uppercase)
}
init(_ v : UInt16, radix : Int = 10, uppercase : Bool = false) {
self = String(UInt64(v), radix: radix, uppercase: uppercase)
init(_ v : UInt16, radix : Int = 10, _uppercase : Bool = false) {
self = String(UInt64(v), radix: radix, _uppercase: _uppercase)
}
init(_ v : UInt32, radix : Int = 10, uppercase : Bool = false) {
self = String(UInt64(v), radix: radix, uppercase: uppercase)
init(_ v : UInt32, radix : Int = 10, _uppercase : Bool = false) {
self = String(UInt64(v), radix: radix, _uppercase: _uppercase)
}
init(_ v : UInt, radix : Int = 10, uppercase : Bool = false) {
self = String(UInt64(v), radix: radix, uppercase: uppercase)
init(_ v : UInt, radix : Int = 10, _uppercase : Bool = false) {
self = String(UInt64(v), radix: radix, _uppercase: _uppercase)
}
init(_ v : Double) {
typealias _Double = Double
typealias _Float = Float
typealias _Bool = Bool
init(_ v : _Double) {
self = _doubleToString(v)
}
init(_ v : Float) {
init(_ v : _Float) {
self = String(Double(v))
}
init(_ b : Bool) {
init(_ b : _Bool) {
if b {
self = "true"
} else {
@@ -344,7 +347,7 @@ extension String {
extension String {
/// \brief Produce a substring of the given string from the given character
/// index to the end of the string.
func substr(start: Int) -> String {
func _substr(start: Int) -> String {
var rng = unicodeScalars
var startIndex = rng.startIndex
for i in 0..start {
@@ -356,7 +359,7 @@ extension String {
/// \brief Split the given string at the given delimiter character, returning
/// the strings before and after that character (neither includes the character
/// found) and a boolean value indicating whether the delimiter was found.
func splitFirst(delim: UnicodeScalar)
func _splitFirst(delim: UnicodeScalar)
-> (before: String, after: String, wasFound : Bool)
{
var rng = unicodeScalars
@@ -372,7 +375,7 @@ extension String {
/// predicate returns true. Returns the string before that character, the
/// character that matches, the string after that character, and a boolean value
/// indicating whether any character was found.
func splitFirstIf(pred: (UnicodeScalar) -> Bool)
func _splitFirstIf(pred: (UnicodeScalar) -> Bool)
-> (before: String, found: UnicodeScalar, after: String, wasFound: Bool)
{
var rng = unicodeScalars
@@ -387,7 +390,7 @@ extension String {
/// \brief Split the given string at each occurrence of a character for which
/// the given predicate evaluates true, returning an array of strings that
/// before/between/after those delimiters.
func splitIf(pred: (UnicodeScalar) -> Bool) -> String[] {
func _splitIf(pred: (UnicodeScalar) -> Bool) -> String[] {
var scalarSlices = Swift.split(unicodeScalars, pred)
return scalarSlices.map { $0 as String }
}

View File

@@ -197,7 +197,7 @@ extension String {
return core.elementWidth == 1 ? core.startASCII : nil
}
func nulTerminatedUTF8() -> NativeArray<UTF8.CodeUnit> {
var nulTerminatedUTF8: NativeArray<UTF8.CodeUnit> {
var result = NativeArray<UTF8.CodeUnit>()
result.reserve(countElements(utf8) + 1)
result += utf8

View File

@@ -10,11 +10,12 @@
//
//===----------------------------------------------------------------------===//
func ==(lhs: UnicodeScalarView.IndexType, rhs: UnicodeScalarView.IndexType) -> Bool {
func ==(lhs: String.UnicodeScalarView.IndexType, rhs: String.UnicodeScalarView.IndexType) -> Bool {
return lhs._position == rhs._position
}
struct UnicodeScalarView : Sliceable, Sequence {
extension String {
struct UnicodeScalarView : Sliceable, Sequence {
init(_ _base: _StringCore) {
self._base = _base
}
@@ -162,6 +163,7 @@ struct UnicodeScalarView : Sliceable, Sequence {
}
var _base: _StringCore
}
}
extension String {

View File

@@ -738,25 +738,6 @@ extension String {
return _ns.hash
}
// - (BOOL)hasPrefix:(NSString *)aString
/// \brief Returns a Boolean value that indicates whether a given
/// string matches the beginning characters of the receiver.
func hasPrefix(aString: String) -> Bool {
return _ns.hasPrefix(aString)
}
// - (BOOL)hasSuffix:(NSString *)aString
/// \brief Returns a Boolean value that indicates whether a given
/// string matches the ending characters of the receiver.
func hasSuffix(aString: String) -> Bool {
return _ns.hasSuffix(aString)
}
/*
Nothing to do here; already provided for String
@@ -1075,13 +1056,6 @@ extension String {
return self.longLongValue
}
// @property NSString * lowercaseString
/// \brief Returns lowercased representation of the receiver.
var lowercaseString: String {
return _ns.lowercaseString
}
// - (NSString *)lowercaseStringWithLocale:(NSLocale *)locale
/// \brief Returns a version of the string with all letters
@@ -1481,12 +1455,6 @@ extension String {
return _ns.substringWithRange(aRange)
}
// @property NSString* uppercaseString;
/// \brief Returns a uppercased representation of the receiver.
var uppercaseString: String {
return _ns.uppercaseString
}
// - (NSString *)uppercaseStringWithLocale:(NSLocale *)locale

View File

@@ -66,9 +66,9 @@ extension Selector : Equatable, Hashable {
extension String {
/// \brief Construct the C string representation of an Objective-C selector.
init(_ sel: Selector) {
init(_sel: Selector) {
// FIXME: This misses the ASCII optimization.
self = String.fromCString(sel_getName(sel))
self = String.fromCString(sel_getName(_sel))
}
}

View File

@@ -1,5 +1,6 @@
// RUN: rm -rf %t/clang-module-cache
// RUN: %swift -parse -verify -import-cf-types -module-cache-path %t/clang-module-cache -I %S/Inputs/custom-modules -target x86_64-apple-darwin13 %s
// XFAIL: *
import CoreCooling

View File

@@ -182,7 +182,7 @@ var sub1Result : String = obj[5]!
var sub2Result : Int = obj[A()]!
// Subscript then call without the '!'
var sub1ResultNE = obj[5].startsWith("foo")
var sub1ResultNE = obj[5].hasPrefix("foo")
var sub2ResultNE = obj[A()].hashValue
// Property/function ambiguities.

View File

@@ -35,7 +35,7 @@ yf.f1(i, y: 1)
Swift.print(3)
var format : String
format.splitFirstIf({ $0.isAlpha() })
format._splitFirstIf({ $0.isAlpha() })
// Archetypes
func doGetLogicValue<T : LogicValue>(t: T) {
@@ -80,7 +80,7 @@ struct GZ<T> {
// Members of literals
// FIXME: Crappy diagnostic
"foo".lower() // expected-error{{could not find member 'lower'}}
var tmp = "foo".lowercase
var tmp = "foo".lowercaseString
// Members of modules
var myTrue = Swift.true

View File

@@ -28,6 +28,6 @@ println(xxx)
// FIXME: compilation fails without the temporary yyy
var yyy = sort(["apple", "Banana", "cherry"],
{ $0.lowercase > $1.lowercase })
{ $0.lowercaseString > $1.lowercaseString })
println(yyy)
// CHECK-NEXT: [cherry, Banana, apple]

View File

@@ -15,6 +15,6 @@ println("Nested \"\(HW)\" with Pi = \(pi)!")
// CHECK: value = 1099226349619 hex = 0xFFEEFF0033 oct = 0o17775677600063
var someval = 0xFFeeFF0033
println("value = \(someval) hex = 0x\(someval, radix:16, uppercase : true) oct = 0o\(someval, radix:8)")
println("value = \(someval) hex = 0x\(someval, radix:16, _uppercase : true) oct = 0o\(someval, radix:8)")

View File

@@ -4,7 +4,7 @@
// String Splits
//===----------------------------------------------------------------------===//
if true {
var (before, after, found) = "foo.bar".splitFirst(".")
var (before, after, found) = "foo.bar"._splitFirst(".")
assert(found)
assert(before == "foo")
assert(after == "bar")
@@ -18,7 +18,7 @@ println("OKAY")
//===----------------------------------------------------------------------===//
if true {
// CHECK-NEXT: {{^}}FOOBAR.WIBBLE{{$}}
println("FooBar.Wibble".uppercase)
println("FooBar.Wibble".uppercaseString)
// CHECK-NEXT: {{^}}foobar.wibble{{$}}
println("FooBar.Wibble".lowercase)
println("FooBar.Wibble".lowercaseString)
}

View File

@@ -1,6 +1,6 @@
// RUN: %target-run-simple-swift | FileCheck %s
typealias CodePoints = UnicodeScalarView
typealias CodePoints = String.UnicodeScalarView
extension CodePoints {
init(_ x: String) {
@@ -39,7 +39,7 @@ func testSplit() {
// FIXME: Disabled pending <rdar://problem/15736729> and <rdar://problem/15733855>
// CHECK-NEXT-DISABLED: [ "", "", "foo bar baz " ]
// println(split(CodePoints(" foo bar baz "), { $0.isSpace() }, true, maxSplit:2))
// println(split(CodePoints(" foo bar baz "), { $0._isSpace() }, true, maxSplit:2))
println("done.")
}

View File

@@ -208,9 +208,11 @@ println("so < tocks => \(so < tocks)")
// CHECK-NEXT: true
println("sox < tocks => \(sox < tocks)")
let qqq = nonASCIILiteral.hasPrefix("🏂☃")
let rrr = nonASCIILiteral.hasPrefix("")
let zz = (
nonASCIILiteral.startsWith("🏂☃"), nonASCIILiteral.startsWith(""),
nonASCIILiteral.endsWith("⛄️❄️"), nonASCIILiteral.endsWith(""))
nonASCIILiteral.hasPrefix("🏂☃"), nonASCIILiteral.hasPrefix(""),
nonASCIILiteral.hasSuffix("⛄️❄️"), nonASCIILiteral.hasSuffix(""))
// CHECK-NEXT: <true, false, true, false>
println("<\(zz.0), \(zz.1), \(zz.2), \(zz.3)>")