Skip to content

Commit ddea636

Browse files
[stdlib] Have Substring.filter return a String (#10871)
* Have Substring.filter return a String * fix spacing nits new machine so editor misconfigured excuses excuses * fix spacing nits
1 parent 9ccb27e commit ddea636

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

stdlib/public/core/Substring.swift.gyb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,12 @@ extension Substring {
428428
public func uppercased() -> String {
429429
return String(self).uppercased()
430430
}
431+
432+
public func filter(
433+
_ isIncluded: (Element) throws -> Bool
434+
) rethrows -> String {
435+
return try String(self.lazy.filter(isIncluded))
436+
}
431437
}
432438

433439
extension Substring : TextOutputStream {

test/stdlib/subString.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,14 @@ SubstringTests.test("Comparison") {
9292
["apple", "pen", "pen", "pineapple"])
9393
}
9494

95+
SubstringTests.test("Filter") {
96+
var name = "😂Edward Woodward".dropFirst()
97+
var filtered = name.filter { $0 != "d" }
98+
expectType(Substring.self, &name)
99+
expectType(String.self, &filtered)
100+
expectEqual("Ewar Woowar", filtered)
101+
}
102+
95103
SubstringTests.test("CharacterView") {
96104
let s = "abcdefg"
97105
var t = s.characters.dropFirst(2)

0 commit comments

Comments
 (0)