Skip to content

Commit

Permalink
string_search: add Text::prefix and Text::suffix
Browse files Browse the repository at this point in the history
Signed-off-by: Jean Privat <jean@pryen.org>
  • Loading branch information
privat committed Nov 11, 2015
1 parent 2f75423 commit f064f34
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions lib/core/text/string_search.nit
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,37 @@ redef class Text
return null
end

# Extract a given prefix, if any.
#
# ~~~
# var p = "hello world".prefix("hello")
# assert p != null
# assert p.text_after == " world"
# ~~~
fun prefix(t: Text): nullable Match do
var len = t.length
if substring(0, len) == t then
return new Match(self.to_s, 0, len)
end
return null
end

# Extract a given suffix, if any.
#
# ~~~
# var p = "hello world".suffix("world")
# assert p != null
# assert p.text_before == "hello "
# ~~~
fun suffix(t: Text): nullable Match do
var len = t.length
var from = length - len
if substring(from, len) == t then
return new Match(self.to_s, from, len)
end
return null
end

# Search all occurrences of `pattern` into self.
#
# var a = new Array[Int]
Expand Down

0 comments on commit f064f34

Please sign in to comment.