[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
extension String {
mutating func _append(rhs: String) {
mutating func extend(rhs: String) {
_core.append(rhs._core)
}
@@ -273,19 +273,10 @@ public func +(var lhs: String, rhs: String) -> String {
if (lhs.isEmpty) {
return rhs
}
lhs._append(rhs)
lhs._core.extend(rhs._core)
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 {
var result = String(lhs)
result += String(rhs)
@@ -299,14 +290,10 @@ public func += (inout lhs: String, rhs: String) {
lhs = rhs
}
else {
lhs._append(rhs)
lhs._core.extend(rhs._core)
}
}
public func += (inout lhs: String, rhs: Character) {
lhs += String(rhs)
}
// Comparison operators
// FIXME: Compare Characters, not code units
extension String : Comparable {
@@ -543,10 +530,19 @@ extension String : ExtensibleCollectionType {
S : SequenceType
where S.Generator.Element == Character
>(seq: S) {
reserveCapacity(underestimateCount(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

View File

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

View File

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