Skip to content

Commit 7135574

Browse files
committed
allow predicate functions to lstrip etc.
1 parent 5ba7f20 commit 7135574

File tree

2 files changed

+32
-21
lines changed

2 files changed

+32
-21
lines changed

base/strings/util.jl

+31-21
Original file line numberDiff line numberDiff line change
@@ -137,13 +137,15 @@ end
137137
const _default_delims = [' ','\t','\n','\v','\f','\r']
138138

139139
"""
140-
lstrip(s::AbstractString[, chars::Chars])
140+
lstrip(str::AbstractString[, chars])
141141
142-
Return `s` with any leading whitespace and delimiters removed.
143-
The default delimiters to remove are `' '`, `\\t`, `\\n`, `\\v`,
144-
`\\f`, and `\\r`.
145-
If `chars` (a character, or vector or set of characters) is provided,
146-
instead remove characters contained in it.
142+
Remove leading characters from `str`.
143+
144+
The default behaviour is to remove leading whitespace and delimiters: see
145+
[`isspace`](@ref) for precise details.
146+
147+
The optional `chars` argument specifies which characters to remove: it can be a single character,
148+
vector or set of characters, or a predicate function.
147149
148150
# Examples
149151
```jldoctest
@@ -154,22 +156,25 @@ julia> lstrip(a)
154156
"March"
155157
```
156158
"""
157-
function lstrip(s::AbstractString, chars::Chars=_default_delims)
159+
function lstrip(s::AbstractString, f=isspace)
158160
e = lastindex(s)
159161
for (i, c) in pairs(s)
160-
!(c in chars) && return SubString(s, i, e)
162+
!f(c) && return SubString(s, i, e)
161163
end
162164
SubString(s, e+1, e)
163165
end
166+
lstrip(s::AbstractString, chars::Chars) = lstrip(s, in(chars))
164167

165168
"""
166-
rstrip(s::AbstractString[, chars::Chars])
169+
rstrip(str::AbstractString[, chars])
170+
171+
Remove trailing characters from `str`.
167172
168-
Return `s` with any trailing whitespace and delimiters removed.
169-
The default delimiters to remove are `' '`, `\\t`, `\\n`, `\\v`,
170-
`\\f`, and `\\r`.
171-
If `chars` (a character, or vector or set of characters) is provided,
172-
instead remove characters contained in it.
173+
The default behaviour is to remove leading whitespace and delimiters: see
174+
[`isspace`](@ref) for precise details.
175+
176+
The optional `chars` argument specifies which characters to remove: it can be a single character,
177+
vector or set of characters, or a predicate function.
173178
174179
# Examples
175180
```jldoctest
@@ -180,19 +185,24 @@ julia> rstrip(a)
180185
"March"
181186
```
182187
"""
183-
function rstrip(s::AbstractString, chars::Chars=_default_delims)
188+
function rstrip(s::AbstractString, f=isspace)
184189
for (i, c) in Iterators.reverse(pairs(s))
185-
c in chars || return SubString(s, 1, i)
190+
f(c) in chars || return SubString(s, 1, i)
186191
end
187192
SubString(s, 1, 0)
188193
end
194+
rstrip(s, chars::Chars) = rstrip(s, in(chars))
189195

190196
"""
191-
strip(s::AbstractString, [chars::Chars])
197+
strip(str::AbstractString, [chars])
198+
199+
Remove leading and trailing characters from `str`.
200+
201+
The default behaviour is to remove leading whitespace and delimiters: see
202+
[`isspace`](@ref) for precise details.
192203
193-
Return `s` with any leading and trailing whitespace removed.
194-
If `chars` (a character, or vector or set of characters) is provided,
195-
instead remove characters contained in it.
204+
The optional `chars` argument specifies which characters to remove: it can be a single character,
205+
vector or set of characters, or a predicate function.
196206
197207
# Examples
198208
```jldoctest
@@ -201,7 +211,7 @@ julia> strip("{3, 5}\\n", ['{', '}', '\\n'])
201211
```
202212
"""
203213
strip(s::AbstractString) = lstrip(rstrip(s))
204-
strip(s::AbstractString, chars::Chars) = lstrip(rstrip(s, chars), chars)
214+
strip(s::AbstractString, chars) = lstrip(rstrip(s, chars), chars)
205215

206216
## string padding functions ##
207217

test/strings/util.jl

+1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ end
5050
@test strip(" ") == ""
5151
@test strip(" ") == ""
5252
@test strip("\t hi \n") == "hi"
53+
@test strip(" \u2009 hi \u2009 ") == "hi"
5354
@test strip("foobarfoo", ['f','o']) == "bar"
5455
@test strip("foobarfoo", ('f','o')) == "bar"
5556

0 commit comments

Comments
 (0)