Implementation of the SE-130 proposal.

It replaces String initializers taking Character or UnicodeScalar as a repeating value by a more general initializer that takes a String as a repeating value. This is done to avoid the ambiguities in the current String API, which can be only resolved by explicit casting.
String.append(_:UnicodeScalar) APIs is also removed to match these changes.
This commit is contained in:
Roman Levenstein
2016-07-25 23:12:20 -07:00
parent 8887175db8
commit a798852cb4
5 changed files with 69 additions and 31 deletions

View File

@@ -22,15 +22,9 @@ extension String {
/// let zeroes = String("0" as Character, count: 10)
/// print(zeroes)
/// // Prints "0000000000"
@available(*, unavailable, message: "Replaced by init(repeating: String, count: Int)")
public init(repeating repeatedValue: Character, count: Int) {
let s = String(repeatedValue)
self = String(_storage: _StringBuffer(
capacity: s._core.count * count,
initialSize: 0,
elementWidth: s._core.elementWidth))
for _ in 0..<count {
self += s
}
Builtin.unreachable()
}
/// Creates a string representing the given Unicode scalar repeated the
@@ -42,12 +36,37 @@ extension String {
/// let zeroes = String("0" as UnicodeScalar, count: 10)
/// print(zeroes)
/// // Prints "0000000000"
@available(*, unavailable, message: "Replaced by init(repeating: String, count: Int)")
public init(repeating repeatedValue: UnicodeScalar, count: Int) {
self = String._fromWellFormedCodeUnitSequence(
UTF32.self,
input: repeatElement(repeatedValue.value, count: count))
Builtin.unreachable()
}
/// Creates a string representing the given string repeated the
/// specified number of times.
///
/// For example, use this initializer to create a string with ten `"00"`
/// strings in a row.
///
/// let zeroes = String(repeating: "00", count: 10)
/// print(zeroes)
/// // Prints "00000000000000000000"
public init(repeating repeatedValue: String, count: Int) {
if count == 0 {
self = ""
return
}
precondition(count > 0, "Negative count not allowed")
let s = repeatedValue
self = String(_storage: _StringBuffer(
capacity: s._core.count * count,
initialSize: 0,
elementWidth: s._core.elementWidth))
for _ in 0..<count {
self += s
}
}
public var _lines : [String] {
return _split(separator: "\n")
}
@@ -65,7 +84,9 @@ extension String {
extension String {
public init(_ _c: UnicodeScalar) {
self = String(repeating: _c, count: 1)
self = String._fromWellFormedCodeUnitSequence(
UTF32.self,
input: repeatElement(_c.value, count: 1))
}
}