Skip to content

Implementation of the SE-130 proposal. #3758

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion benchmark/single-source/StringTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import TestsUtils

public func run_StringWithCString(_ N: Int) {
let str = String(repeating: "x" as UnicodeScalar, count: 100 * (1 << 16))
let str = String(repeating: "x", count: 100 * (1 << 16))
for _ in 0 ..< N {
str.withCString { _ in }
}
Expand Down
3 changes: 2 additions & 1 deletion stdlib/public/core/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -651,8 +651,9 @@ extension String {
///
/// - Complexity: Appending a Unicode scalar to a string averages to O(1)
/// over many additions.
@available(*, unavailable, message: "Replaced by append(_: String)")
public mutating func append(_ x: UnicodeScalar) {
_core.append(x)
Builtin.unreachable()
}

public // SPI(Foundation)
Expand Down
47 changes: 34 additions & 13 deletions stdlib/public/core/StringLegacy.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
}
Expand All @@ -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))
}
}

Expand Down
6 changes: 6 additions & 0 deletions test/1_stdlib/Renames.swift
Original file line number Diff line number Diff line change
Expand Up @@ -462,9 +462,15 @@ func _StringCharacterView<S, C>(x: String.CharacterView, s: S, c: C, i: String.C
x.appendContentsOf(s) // expected-error {{'appendContentsOf' has been renamed to 'append(contentsOf:)'}} {{5-21=append}} {{22-22=contentsOf: }} {{none}}
}

func _StringAppend(s: inout String, u: UnicodeScalar) {
s.append(u) // expected-error {{'append' is unavailable: Replaced by append(_: String)}} {{none}}
}

func _StringLegacy(c: Character, u: UnicodeScalar) {
_ = String(count: 1, repeatedValue: c) // expected-error {{'init(count:repeatedValue:)' is unavailable: Renamed to init(repeating:count:) and reordered parameters}} {{none}}
_ = String(count: 1, repeatedValue: u) // expected-error {{'init(count:repeatedValue:)' is unavailable: Renamed to init(repeating:count:) and reordered parameters}} {{none}}
_ = String(repeating: c, count: 1) // expected-error {{'init(repeating:count:)' is unavailable: Replaced by init(repeating: String, count: Int)}} {{none}}
_ = String(repeating: u, count: 1) // expected-error {{'init(repeating:count:)' is unavailable: Replaced by init(repeating: String, count: Int)}} {{none}}
}

func _Unicode() {
Expand Down
42 changes: 26 additions & 16 deletions validation-test/stdlib/String.swift
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ StringTests.test("appendToSubstring") {
sliceEnd < sliceStart {
continue
}
var s0 = String(repeating: UnicodeScalar("x"), count: initialSize)
var s0 = String(repeating: "x", count: initialSize)
let originalIdentity = s0.bufferID
s0 = s0[s0.index(_nth: sliceStart)..<s0.index(_nth: sliceEnd)]
expectEqual(originalIdentity, s0.bufferID)
Expand All @@ -417,11 +417,15 @@ StringTests.test("appendToSubstring") {
// and we could get some unused capacity in the buffer. In that case,
// the identity would not change.
if sliceEnd != initialSize {
expectNotEqual(originalIdentity, s0.bufferID)
if sliceStart != sliceEnd {
expectNotEqual(originalIdentity, s0.bufferID)
} else {
expectEqual(0, s0.bufferID)
}
}
expectEqual(
String(
repeating: UnicodeScalar("x"),
repeating: "x",
count: sliceEnd - sliceStart + 1),
s0)
}
Expand Down Expand Up @@ -450,8 +454,8 @@ StringTests.test("appendToSubstringBug") {
let prefixSize = size - suffixSize
for i in 1..<10 {
// We will be overflowing s0 with s1.
var s0 = String(repeating: UnicodeScalar("x"), count: size)
let s1 = String(repeating: UnicodeScalar("x"), count: prefixSize)
var s0 = String(repeating: "x", count: size)
let s1 = String(repeating: "x", count: prefixSize)
let originalIdentity = s0.bufferID

// Turn s0 into a slice that points to the end.
Expand All @@ -460,16 +464,22 @@ StringTests.test("appendToSubstringBug") {
// Slicing should not reallocate.
expectEqual(originalIdentity, s0.bufferID)

let originalCapacity = s0.capacity

// Overflow.
s0 += s1

// We should correctly determine that the storage is too small and
// We should correctly determine if the storage is too small and
// reallocate.
expectNotEqual(originalIdentity, s0.bufferID)
if originalCapacity < suffixSize + prefixSize {
expectNotEqual(originalIdentity, s0.bufferID)
} else {
expectEqual(originalIdentity, s0.bufferID)
}

expectEqual(
String(
repeating: UnicodeScalar("x"),
repeating: "x",
count: suffixSize + prefixSize),
s0)
}
Expand Down Expand Up @@ -553,15 +563,15 @@ StringTests.test("COW/removeSubrange/end") {
expectEqual("12345678", slice)

// No more reallocations are expected.
str.append(UnicodeScalar("x"))
str.append("x")
str.removeSubrange(str.index(_nthLast: 1)..<str.endIndex)
expectEqual(heapStrIdentity, str.bufferID)
expectEqual(literalIdentity, slice.bufferID)
expectEqual("1234567", str)
expectEqual("12345678", slice)

str.removeSubrange(str.index(_nthLast: 1)..<str.endIndex)
str.append(UnicodeScalar("x"))
str.append("x")
str.removeSubrange(str.index(_nthLast: 1)..<str.endIndex)
expectEqual(heapStrIdentity, str.bufferID)
expectEqual(literalIdentity, slice.bufferID)
Expand Down Expand Up @@ -589,15 +599,15 @@ StringTests.test("COW/removeSubrange/end") {
expectEqual("123456", slice)

// No more reallocations are expected.
str.append(UnicodeScalar("x"))
str.append("x")
str.removeSubrange(str.index(_nthLast: 1)..<str.endIndex)
expectEqual(heapStrIdentity, str.bufferID)
expectEqual(heapStrIdentity1, slice.bufferID)
expectEqual("12345", str)
expectEqual("123456", slice)

str.removeSubrange(str.index(_nthLast: 1)..<str.endIndex)
str.append(UnicodeScalar("x"))
str.append("x")
str.removeSubrange(str.index(_nthLast: 1)..<str.endIndex)
expectEqual(heapStrIdentity, str.bufferID)
expectEqual(heapStrIdentity1, slice.bufferID)
Expand Down Expand Up @@ -1159,25 +1169,25 @@ StringTests.test("String.append(_: UnicodeScalar)") {
do {
// U+0061 LATIN SMALL LETTER A
let input: UnicodeScalar = "\u{61}"
s.append(input)
s.append(String(input))
expectEqual(["\u{61}"], Array(s.unicodeScalars))
}
do {
// U+304B HIRAGANA LETTER KA
let input: UnicodeScalar = "\u{304b}"
s.append(input)
s.append(String(input))
expectEqual(["\u{61}", "\u{304b}"], Array(s.unicodeScalars))
}
do {
// U+3099 COMBINING KATAKANA-HIRAGANA VOICED SOUND MARK
let input: UnicodeScalar = "\u{3099}"
s.append(input)
s.append(String(input))
expectEqual(["\u{61}", "\u{304b}", "\u{3099}"], Array(s.unicodeScalars))
}
do {
// U+1F425 FRONT-FACING BABY CHICK
let input: UnicodeScalar = "\u{1f425}"
s.append(input)
s.append(String(input))
expectEqual(
["\u{61}", "\u{304b}", "\u{3099}", "\u{1f425}"],
Array(s.unicodeScalars))
Expand Down