Skip to content

Commit 27ea284

Browse files
authored
Merge pull request #11159 from airspeedswift/4.0-substring-filter
[4.0]Have Substring.filter return a String
2 parents 8034962 + 8353712 commit 27ea284

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
@@ -434,6 +434,12 @@ extension Substring {
434434
public func uppercased() -> String {
435435
return String(self).uppercased()
436436
}
437+
438+
public func filter(
439+
_ isIncluded: (Element) throws -> Bool
440+
) rethrows -> String {
441+
return try String(self.lazy.filter(isIncluded))
442+
}
437443
}
438444

439445
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)