mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
[stdlib] API naming guidelines applied to split and join
- `separator` label for first argument of `split` - `join` and related types are renamed to `joined`
This commit is contained in:
@@ -19,6 +19,6 @@ public func run_Join(N: Int) {
|
|||||||
for x in 0..<1000 * N {
|
for x in 0..<1000 * N {
|
||||||
array.append(String(x))
|
array.append(String(x))
|
||||||
}
|
}
|
||||||
_ = array.join(separator: "")
|
_ = array.joined(separator: "")
|
||||||
_ = array.join(separator: " ")
|
_ = array.joined(separator: " ")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1699,7 +1699,7 @@ self.test("\(testNamePrefix).split/separator/semantics") {
|
|||||||
)
|
)
|
||||||
let separator = wrapValueIntoEquatable(MinimalEquatableValue(test.separator))
|
let separator = wrapValueIntoEquatable(MinimalEquatableValue(test.separator))
|
||||||
let result = s.split(
|
let result = s.split(
|
||||||
by: separator,
|
separator: separator,
|
||||||
maxSplits: test.maxSplits,
|
maxSplits: test.maxSplits,
|
||||||
omittingEmptySubsequences: test.omittingEmptySubsequences)
|
omittingEmptySubsequences: test.omittingEmptySubsequences)
|
||||||
expectEqualSequence(
|
expectEqualSequence(
|
||||||
@@ -1726,7 +1726,7 @@ self.test("\(testNamePrefix).split/semantics/separator/negativeMaxSplit") {
|
|||||||
let s = makeWrappedSequenceWithEquatableElement([MinimalEquatableValue(1)])
|
let s = makeWrappedSequenceWithEquatableElement([MinimalEquatableValue(1)])
|
||||||
let separator = wrapValueIntoEquatable(MinimalEquatableValue(1))
|
let separator = wrapValueIntoEquatable(MinimalEquatableValue(1))
|
||||||
expectCrashLater()
|
expectCrashLater()
|
||||||
_ = s.split(by: separator, maxSplits: -1, omittingEmptySubsequences: false)
|
_ = s.split(separator: separator, maxSplits: -1, omittingEmptySubsequences: false)
|
||||||
}
|
}
|
||||||
|
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ public func asHex<
|
|||||||
where
|
where
|
||||||
S.Iterator.Element : Integer
|
S.Iterator.Element : Integer
|
||||||
>(x: S) -> String {
|
>(x: S) -> String {
|
||||||
return "[ " + x.lazy.map { asHex($0) }.join(separator: ", ") + " ]"
|
return "[ " + x.lazy.map { asHex($0) }.joined(separator: ", ") + " ]"
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Compute the prefix sum of `seq`.
|
/// Compute the prefix sum of `seq`.
|
||||||
|
|||||||
@@ -489,7 +489,7 @@ extension Collection where Iterator.Element : Equatable {
|
|||||||
/// - Precondition: `maxSplits >= 0`
|
/// - Precondition: `maxSplits >= 0`
|
||||||
@warn_unused_result
|
@warn_unused_result
|
||||||
public func split(
|
public func split(
|
||||||
by separator: Iterator.Element,
|
separator separator: Iterator.Element,
|
||||||
maxSplits: Int = Int.max,
|
maxSplits: Int = Int.max,
|
||||||
omittingEmptySubsequences: Bool = true
|
omittingEmptySubsequences: Bool = true
|
||||||
) -> [SubSequence] {
|
) -> [SubSequence] {
|
||||||
@@ -764,9 +764,9 @@ extension Collection {
|
|||||||
}
|
}
|
||||||
|
|
||||||
extension Collection where Iterator.Element : Equatable {
|
extension Collection where Iterator.Element : Equatable {
|
||||||
@available(*, unavailable, message="Please use split(_:maxSplits:omittingEmptySubsequences:) instead")
|
@available(*, unavailable, message="Please use split(separator:maxSplits:omittingEmptySubsequences:) instead")
|
||||||
public func split(
|
public func split(
|
||||||
by separator: Iterator.Element,
|
separator: Iterator.Element,
|
||||||
maxSplit: Int = Int.max,
|
maxSplit: Int = Int.max,
|
||||||
allowEmptySlices: Bool = false
|
allowEmptySlices: Bool = false
|
||||||
) -> [SubSequence] {
|
) -> [SubSequence] {
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ internal enum _JoinIteratorState {
|
|||||||
|
|
||||||
/// An iterator that presents the elements of the sequences traversed
|
/// An iterator that presents the elements of the sequences traversed
|
||||||
/// by `Base`, concatenated using a given separator.
|
/// by `Base`, concatenated using a given separator.
|
||||||
public struct JoinIterator<
|
public struct JoinedIterator<
|
||||||
Base : IteratorProtocol where Base.Element : Sequence
|
Base : IteratorProtocol where Base.Element : Sequence
|
||||||
> : IteratorProtocol {
|
> : IteratorProtocol {
|
||||||
|
|
||||||
@@ -90,7 +90,7 @@ public struct JoinIterator<
|
|||||||
|
|
||||||
/// A sequence that presents the elements of the `Base` sequences
|
/// A sequence that presents the elements of the `Base` sequences
|
||||||
/// concatenated using a given separator.
|
/// concatenated using a given separator.
|
||||||
public struct JoinSequence<
|
public struct JoinedSequence<
|
||||||
Base : Sequence where Base.Iterator.Element : Sequence
|
Base : Sequence where Base.Iterator.Element : Sequence
|
||||||
> : Sequence {
|
> : Sequence {
|
||||||
|
|
||||||
@@ -110,8 +110,8 @@ public struct JoinSequence<
|
|||||||
/// Return an iterator over the elements of this sequence.
|
/// Return an iterator over the elements of this sequence.
|
||||||
///
|
///
|
||||||
/// - Complexity: O(1).
|
/// - Complexity: O(1).
|
||||||
public func iterator() -> JoinIterator<Base.Iterator> {
|
public func iterator() -> JoinedIterator<Base.Iterator> {
|
||||||
return JoinIterator(
|
return JoinedIterator(
|
||||||
base: _base.iterator(),
|
base: _base.iterator(),
|
||||||
separator: _separator)
|
separator: _separator)
|
||||||
}
|
}
|
||||||
@@ -163,37 +163,37 @@ extension Sequence where Iterator.Element : Sequence {
|
|||||||
/// `separator` between the elements of the sequence `self`.
|
/// `separator` between the elements of the sequence `self`.
|
||||||
///
|
///
|
||||||
/// For example,
|
/// For example,
|
||||||
/// `[[1, 2, 3], [4, 5, 6], [7, 8, 9]].join(separator: [-1, -2])`
|
/// `[[1, 2, 3], [4, 5, 6], [7, 8, 9]].joined(separator: [-1, -2])`
|
||||||
/// yields `[1, 2, 3, -1, -2, 4, 5, 6, -1, -2, 7, 8, 9]`.
|
/// yields `[1, 2, 3, -1, -2, 4, 5, 6, -1, -2, 7, 8, 9]`.
|
||||||
@warn_unused_result
|
@warn_unused_result
|
||||||
public func join<
|
public func joined<
|
||||||
Separator : Sequence
|
Separator : Sequence
|
||||||
where
|
where
|
||||||
Separator.Iterator.Element == Iterator.Element.Iterator.Element
|
Separator.Iterator.Element == Iterator.Element.Iterator.Element
|
||||||
>(separator separator: Separator) -> JoinSequence<Self> {
|
>(separator separator: Separator) -> JoinedSequence<Self> {
|
||||||
return JoinSequence(base: self, separator: separator)
|
return JoinedSequence(base: self, separator: separator)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@available(*, unavailable, renamed="JoinIterator")
|
@available(*, unavailable, renamed="JoinedIterator")
|
||||||
public struct JoinGenerator<
|
public struct JoinGenerator<
|
||||||
Base : IteratorProtocol where Base.Element : Sequence
|
Base : IteratorProtocol where Base.Element : Sequence
|
||||||
> {}
|
> {}
|
||||||
|
|
||||||
extension JoinSequence {
|
extension JoinedSequence {
|
||||||
@available(*, unavailable, renamed="iterator")
|
@available(*, unavailable, renamed="iterator")
|
||||||
public func generate() -> JoinIterator<Base.Iterator> {
|
public func generate() -> JoinedIterator<Base.Iterator> {
|
||||||
fatalError("unavailable function can't be called")
|
fatalError("unavailable function can't be called")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension Sequence where Iterator.Element : Sequence {
|
extension Sequence where Iterator.Element : Sequence {
|
||||||
@available(*, unavailable, renamed="join")
|
@available(*, unavailable, renamed="joined")
|
||||||
public func joinWithSeparator<
|
public func joinWithSeparator<
|
||||||
Separator : Sequence
|
Separator : Sequence
|
||||||
where
|
where
|
||||||
Separator.Iterator.Element == Iterator.Element.Iterator.Element
|
Separator.Iterator.Element == Iterator.Element.Iterator.Element
|
||||||
>(separator: Separator) -> JoinSequence<Self> {
|
>(separator: Separator) -> JoinedSequence<Self> {
|
||||||
fatalError("unavailable function can't be called")
|
fatalError("unavailable function can't be called")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -519,7 +519,7 @@ extension Sequence where Iterator.Element : Equatable {
|
|||||||
/// - Precondition: `maxSplits >= 0`
|
/// - Precondition: `maxSplits >= 0`
|
||||||
@warn_unused_result
|
@warn_unused_result
|
||||||
public func split(
|
public func split(
|
||||||
by separator: Iterator.Element,
|
separator separator: Iterator.Element,
|
||||||
maxSplits: Int = Int.max,
|
maxSplits: Int = Int.max,
|
||||||
omittingEmptySubsequences: Bool = true
|
omittingEmptySubsequences: Bool = true
|
||||||
) -> [AnySequence<Iterator.Element>] {
|
) -> [AnySequence<Iterator.Element>] {
|
||||||
@@ -671,9 +671,9 @@ extension Sequence {
|
|||||||
}
|
}
|
||||||
|
|
||||||
extension Sequence where Iterator.Element : Equatable {
|
extension Sequence where Iterator.Element : Equatable {
|
||||||
@available(*, unavailable, message="call 'split(_:omittingEmptySubsequences:isSeparator:)' and invert the 'allowEmptySlices' argument")
|
@available(*, unavailable, message="call 'split(separator:omittingEmptySubsequences:isSeparator:)' and invert the 'allowEmptySlices' argument")
|
||||||
public func split(
|
public func split(
|
||||||
by separator: Iterator.Element,
|
separator: Iterator.Element,
|
||||||
maxSplit: Int = Int.max,
|
maxSplit: Int = Int.max,
|
||||||
allowEmptySlices: Bool = false
|
allowEmptySlices: Bool = false
|
||||||
) -> [AnySequence<Iterator.Element>] {
|
) -> [AnySequence<Iterator.Element>] {
|
||||||
|
|||||||
@@ -610,9 +610,9 @@ extension Sequence where Iterator.Element == String {
|
|||||||
/// Interpose the `separator` between elements of `self`, then concatenate
|
/// Interpose the `separator` between elements of `self`, then concatenate
|
||||||
/// the result. For example:
|
/// the result. For example:
|
||||||
///
|
///
|
||||||
/// ["foo", "bar", "baz"].join(separator: "-|-") // "foo-|-bar-|-baz"
|
/// ["foo", "bar", "baz"].joined(separator: "-|-") // "foo-|-bar-|-baz"
|
||||||
@warn_unused_result
|
@warn_unused_result
|
||||||
public func join(separator separator: String) -> String {
|
public func joined(separator separator: String) -> String {
|
||||||
var result = ""
|
var result = ""
|
||||||
|
|
||||||
// FIXME(performance): this code assumes UTF-16 in-memory representation.
|
// FIXME(performance): this code assumes UTF-16 in-memory representation.
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ extension Mirror {
|
|||||||
return "[" +
|
return "[" +
|
||||||
children.lazy
|
children.lazy
|
||||||
.map { "\($0.0 ?? nil_): \(String(reflecting: $0.1))" }
|
.map { "\($0.0 ?? nil_): \(String(reflecting: $0.1))" }
|
||||||
.join(separator: ", ")
|
.joined(separator: ", ")
|
||||||
+ "]"
|
+ "]"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -16,5 +16,5 @@
|
|||||||
// from ApplyInst to match what GenericSignature expects.
|
// from ApplyInst to match what GenericSignature expects.
|
||||||
|
|
||||||
func asHex(a: [UInt8]) -> String {
|
func asHex(a: [UInt8]) -> String {
|
||||||
return a.map { "0x" + String($0, radix: 16) }.join(separator: "")
|
return a.map { "0x" + String($0, radix: 16) }.joined(separator: "")
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -674,8 +674,8 @@ func invalidDictionaryLiteral() {
|
|||||||
|
|
||||||
|
|
||||||
// FIXME: The issue here is a type compatibility problem, there is no ambiguity.
|
// FIXME: The issue here is a type compatibility problem, there is no ambiguity.
|
||||||
[4].join(separator: [1]) // expected-error {{type of expression is ambiguous without more context}}
|
[4].joined(separator: [1]) // expected-error {{type of expression is ambiguous without more context}}
|
||||||
[4].join(separator: [[[1]]]) // expected-error {{type of expression is ambiguous without more context}}
|
[4].joined(separator: [[[1]]]) // expected-error {{type of expression is ambiguous without more context}}
|
||||||
|
|
||||||
//===----------------------------------------------------------------------===//
|
//===----------------------------------------------------------------------===//
|
||||||
// nil/metatype comparisons
|
// nil/metatype comparisons
|
||||||
|
|||||||
@@ -3,5 +3,5 @@
|
|||||||
// Test case submitted to project by https://github.com/tmu (Teemu Kurppa)
|
// Test case submitted to project by https://github.com/tmu (Teemu Kurppa)
|
||||||
// rdar://18174611
|
// rdar://18174611
|
||||||
|
|
||||||
["ab", "cd"].join(separator: "")
|
["ab", "cd"].joined(separator: "")
|
||||||
|
|
||||||
|
|||||||
@@ -282,7 +282,7 @@ JoinTestSuite.test("${Base}.join()") {
|
|||||||
${Base}(${label} $0)
|
${Base}(${label} $0)
|
||||||
})
|
})
|
||||||
let separator = ${Base}(${label} test.separator)
|
let separator = ${Base}(${label} test.separator)
|
||||||
let r = Array(elements.join(separator: separator))
|
let r = Array(elements.joined(separator: separator))
|
||||||
checkSequence(test.expected, r)
|
checkSequence(test.expected, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -293,7 +293,7 @@ JoinTestSuite.test("${Base}.join()/_copyToNativeArrayBuffer()") {
|
|||||||
${Base}(${label} $0)
|
${Base}(${label} $0)
|
||||||
})
|
})
|
||||||
let separator = ${Base}(${label} test.separator)
|
let separator = ${Base}(${label} test.separator)
|
||||||
let r = Array(elements.join(separator: separator))
|
let r = Array(elements.joined(separator: separator))
|
||||||
checkSequence(test.expected, r)
|
checkSequence(test.expected, r)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -301,10 +301,10 @@ JoinTestSuite.test("${Base}.join()/_copyToNativeArrayBuffer()") {
|
|||||||
% end
|
% end
|
||||||
|
|
||||||
func join(separator: String, _ elements: [String]) -> String {
|
func join(separator: String, _ elements: [String]) -> String {
|
||||||
return elements.join(separator: separator)
|
return elements.joined(separator: separator)
|
||||||
}
|
}
|
||||||
|
|
||||||
JoinTestSuite.test("String.join(separator:)") {
|
JoinTestSuite.test("String.joined(separator:)") {
|
||||||
//
|
//
|
||||||
// Test the free function.
|
// Test the free function.
|
||||||
//
|
//
|
||||||
@@ -341,7 +341,7 @@ JoinTestSuite.test("String.join(separator:)") {
|
|||||||
// Test forwarding instance function.
|
// Test forwarding instance function.
|
||||||
//
|
//
|
||||||
|
|
||||||
expectEqual("abxycdxyef", ["ab", "cd", "ef"].join(separator: "xy"))
|
expectEqual("abxycdxyef", ["ab", "cd", "ef"].joined(separator: "xy"))
|
||||||
}
|
}
|
||||||
|
|
||||||
runAllTests()
|
runAllTests()
|
||||||
|
|||||||
Reference in New Issue
Block a user