[stdlib] Don't allow String() + Character("x")

Symmetry with what we did for Arrays says that Strings shoudl only
concatenate to Strings using "+".  We have append() for adding single
characters.

Swift SVN r20997
This commit is contained in:
Dave Abrahams
2014-08-04 06:44:34 +00:00
parent 70b388ba77
commit e70c19f021
3 changed files with 16 additions and 22 deletions

View File

@@ -214,7 +214,7 @@ public func <(lhs: String, rhs: String) -> Bool {
// Support for copy-on-write // Support for copy-on-write
extension String { extension String {
mutating func _append(rhs: String) { mutating func extend(rhs: String) {
_core.append(rhs._core) _core.append(rhs._core)
} }
@@ -273,19 +273,10 @@ public func +(var lhs: String, rhs: String) -> String {
if (lhs.isEmpty) { if (lhs.isEmpty) {
return rhs return rhs
} }
lhs._append(rhs) lhs._core.extend(rhs._core)
return lhs return lhs
} }
public func +(var lhs: String, rhs: Character) -> String {
lhs._append(String(rhs))
return lhs
}
public func +(lhs: Character, rhs: String) -> String {
var result = String(lhs)
result._append(rhs)
return result
}
public func +(lhs: Character, rhs: Character) -> String { public func +(lhs: Character, rhs: Character) -> String {
var result = String(lhs) var result = String(lhs)
result += String(rhs) result += String(rhs)
@@ -299,14 +290,10 @@ public func += (inout lhs: String, rhs: String) {
lhs = rhs lhs = rhs
} }
else { else {
lhs._append(rhs) lhs._core.extend(rhs._core)
} }
} }
public func += (inout lhs: String, rhs: Character) {
lhs += String(rhs)
}
// Comparison operators // Comparison operators
// FIXME: Compare Characters, not code units // FIXME: Compare Characters, not code units
extension String : Comparable { extension String : Comparable {
@@ -543,10 +530,19 @@ extension String : ExtensibleCollectionType {
S : SequenceType S : SequenceType
where S.Generator.Element == Character where S.Generator.Element == Character
>(seq: S) { >(seq: S) {
reserveCapacity(underestimateCount(seq))
for c in seq { for c in seq {
self += c self.append(c)
} }
} }
public init<
S : SequenceType
where S.Generator.Element == Character
>(seq: S) {
self = ""
self.extend(seq)
}
} }
// Algorithms // Algorithms

View File

@@ -9,7 +9,7 @@ for letter in puzzleInput {
case "a", "e", "i", "o", "u", " ": case "a", "e", "i", "o", "u", " ":
continue continue
default: default:
puzzleOutput += letter puzzleOutput.append(letter)
} }
} }
println(puzzleOutput) println(puzzleOutput)

View File

@@ -31,16 +31,14 @@ let eager: Array = reverse(lazy(2..<8).map { $0 * 3 })
println(equal(eager, r)) println(equal(eager, r))
let raboof = reduce(lazy("foobar").reverse(), "") { let raboof = reduce(lazy("foobar").reverse(), "") {
// FIXME: "$0 + $1" should work just fine here (s: String, c: Character) in s + String(c)
(s: String, c: Character) in s + c
} }
// CHECK-NEXT: "raboof" // CHECK-NEXT: "raboof"
debugPrintln(raboof) debugPrintln(raboof)
// Prove that the result is at least bidirectional, i.e. reversible // Prove that the result is at least bidirectional, i.e. reversible
let foobar = reduce(lazy("foobar").reverse().reverse(), "") { let foobar = reduce(lazy("foobar").reverse().reverse(), "") {
// FIXME: "$0 + $1" should work just fine here (s: String, c: Character) in s + String(c)
(s: String, c: Character) in s + c
} }
// CHECK-NEXT: "foobar" // CHECK-NEXT: "foobar"
debugPrintln(foobar) debugPrintln(foobar)