1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2005-2008 Jean Privat <jean@pryen.org>
5 # This file is free software, which comes along with NIT. This software is
6 # distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
7 # without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
8 # PARTICULAR PURPOSE. You can modify it is you want, provided this header
9 # is kept unaltered, and a notification of the changes is added.
10 # You are allowed to redistribute it and sell it, alone or is a part of
13 # Basic string search, match and replace.
18 # Patterns are abstract string motifs (include `String` and `Char`).
20 # Search `self` into `s` from a certain position.
21 # Return the position of the first character of the matching section.
22 # Return -1 if not found.
24 # assert 'l'.search_index_in("hello world", 0) == 2
25 # assert 'l'.search_index_in("hello world", 3) == 3
26 # assert 'z'.search_index_in("hello world", 0) == -1
28 # This method is usually faster than `search_in` if what is
29 # required is only the index.
30 # Note: in most implementation, `search_in` is implemented with this method.
31 protected fun search_index_in
(s
: Text, from
: Int): Int is abstract
33 # Search `self` into `s` from a certain position.
34 # Return null if not found.
36 # assert 'l'.search_in("hello world", 0).from == 2
37 # assert 'l'.search_in("hello world", 3).from == 3
38 # assert 'z'.search_in("hello world", 0) == null
40 # If only the index of the first character if required, see `search_index_in`.
42 # Note: Is used by `String::search`, `String::search_from`, and others.
43 protected fun search_in
(s
: Text, from
: Int): nullable Match is abstract
45 # Search all `self` occurrences into `s`.
47 # assert 'l'.search_all_in("hello world").length == 3
48 # assert 'z'.search_all_in("hello world").length == 0
50 # Note: Is used by `String::search_all`.
51 protected fun search_all_in
(s
: Text): Array[Match]
53 var res
= new Array[Match] # Result
54 var match
= search_in
(s
, 0)
55 while match
!= null do
57 match
= search_in
(s
, match
.after
)
62 # Split `s` using `self` is separator.
64 # Returns an array of matches that are between each occurence of `self`.
65 # If self is not present, an array with a single match on `s` is retunred.
67 # assert 'l'.split_in("hello world").join("|") == "he||o wor|d"
68 # assert 'z'.split_in("hello world").join("|") == "hello world"
70 # Note: is used by `String::split`
71 protected fun split_in
(s
: Text): Array[Match]
73 var res
= new Array[Match] # Result
75 var match
= search_in
(s
, 0)
76 while match
!= null do
77 # Compute the splited part length
78 var len
= match
.from
- i
79 res
.add
(new Match(s
.to_s
, i
, len
))
81 match
= search_in
(s
, i
)
84 res
.add
(new Match(s
.to_s
, i
, s
.length
- i
))
89 protected fun is_in
(s
: Text): Bool do return search_index_in
(s
, 0) != -1
92 # BM_Pattern are pre-compiled string motif for the Boyer-Moore algorithm.
93 # (cf. A Fast String Searching Algorithm, with R.S. Boyer. Communications
94 # of the Association for Computing Machinery, 20(10), 1977, pp. 762-772.)
95 # http://www.cs.utexas.edu/users/moore/best-ideas/string-searching/index.html
97 # var pat = new BM_Pattern("hello")
98 # assert "I say hello to the world!".search(pat).from == 6
99 # assert "I say goodbye to the world!".search(pat) == null
103 redef fun to_s
do return _motif
105 # boyer-moore search gives the position of the first occurrence of a pattern starting at position `from`
106 redef fun search_index_in
(s
, from
)
113 while j
< n
- m
+ 1 do
114 var i
= m
- 1 # Cursor in the pattern
115 while i
>= 0 and _motif
.chars
[i
] == s
.chars
[i
+ j
] do i
-= 1
119 var gs
= _gs
[i
] # Good shift
120 var bc
= bc
(s
.chars
[i
+j
]) - m
+ 1 + i
# Bad char
121 # Both are true, do move to the best
129 return -1 # found nothing
132 # boyer-moore search. Return null if not found
133 redef fun search_in
(s
, from
)
135 var to
= search_index_in
(s
, from
)
139 return new Match(s
.to_s
, to
, _length
)
145 _length
= _motif
.length
146 _gs
= new Array[Int].with_capacity
(_length
)
152 private var motif
: String
154 # length of the motif
155 private var length
: Int is noinit
157 private fun bc
(e
: Char): Int
159 if _bc_table
.has_key
(e
) then
167 private var gs
: Array[Int] is noinit
170 private var bc_table
= new ArrayMap[Char, Int]
172 private fun compute_bc
178 _bc_table
[x
.chars
[i
]] = m
- i
- 1
183 private fun suffixes
: Array[Int]
187 var suff
= new Array[Int].filled_with
(m
, m
)
193 if i
> g
and suff
[i
+m-1-f
] < i
- g
then
194 suff
[i
] = suff
[i
+m-1-f
]
198 while g
>= 0 and x
.chars
[g
] == x
.chars
[g
+ m
- 1 - f
] do g
-= 1
206 private fun compute_gs
218 if i
== -1 or suff
[i
] == i
+ 1 then
219 while j
< m
- 1 - i
do
220 if _gs
[j
] == m
then _gs
[j
] = m
- 1 - i
228 _gs
[m
- 1 - suff
[i
]] = m
- 1 - i
233 redef fun hash
do return _motif
.hash
234 redef fun ==(o
) do return o
isa BM_Pattern and o
._motif
== _motif
237 # Matches are a part of a `Text` found by a `Pattern`.
239 # The base string matched
242 # The starting position in the string
245 # The length of the matching part
248 # The position of the first character just after the matching part.
249 # May be out of the base string
250 fun after
: Int do return from
+ length
252 # The contents of the matching part
253 redef fun to_s
do return string
.substring
(from
,length
)
257 assert positive_length
: length
>= 0
258 assert valid_from
: from
>= 0
259 assert valid_after
: from
+ length
<= string
.length
266 redef fun search_index_in
(s
, from
)
270 if s
.chars
[from
] == self then return from
276 redef fun search_in
(s
, from
)
278 var pos
= search_index_in
(s
, from
)
282 return new Match(s
.to_s
, pos
, 1)
290 redef fun search_index_in
(s
, from
)
293 var stop
= s
.length
- length
+ 1
296 while i
>= 0 and self.chars
[i
] == s
.chars
[i
+ from
] do i
-= 1
298 if i
< 0 then return from
299 # Not found so try next one
305 redef fun search_in
(s
, from
)
307 var pos
= search_index_in
(s
, from
)
311 return new Match(s
.to_s
, pos
, length
)
315 # Search the first occurence of the pattern `p`.
316 # Return null if not found.
318 # assert "I say hello to the world!".search("hello").from == 6
319 # assert "I say goodbye to the world!".search("hello") == null
320 fun search
(p
: Pattern): nullable Match do return p
.search_in
(self, 0)
322 # Search the first occurence of the pattern `p` after `from`.
323 # The search starts at `from`.
324 # Return null if not found.
326 # assert "I say hello to the world!".search_from("hello",4).from == 6
327 # assert "I say hello to the world!".search_from("hello",7) == null
328 fun search_from
(p
: Pattern, from
: Int): nullable Match do return p
.search_in
(self, from
)
330 # Search the last occurence of the text `t`.
332 # assert "bob".search_last("b").from == 2
333 # assert "bob".search_last("bo").from == 0
334 # assert "bob".search_last("ob").from == 1
335 # assert "bobob".search_last("ob").from == 3
336 # assert "bobbob".search_last("bb").from == 2
337 # assert "bobbob".search_last("bob").from == 3
338 # assert "bob".search_last("z") == null
339 # assert "".search_last("b") == null
340 fun search_last
(t
: Text): nullable Match do
341 return search_last_up_to
(t
, length
)
344 # Search the last occurence of the text `t` before `up_to`.
346 # assert "bobbob".search_last_up_to("b", 3).from == 2
347 # assert "bobbob".search_last_up_to("b", 6).from == 5
348 # assert "bobbob".search_last_up_to("b", 0) == null
349 fun search_last_up_to
(t
: Text, up_to
: Int): nullable Match do
350 var i
= up_to
- t
.length
353 if substring
(i
, t
.length
) == t
then
354 return new Match(self.to_s
, i
, t
.length
)
361 # Search all occurrences of p into self.
363 # var a = new Array[Int]
364 # for i in "hello world".search_all('o') do
368 fun search_all
(p
: Pattern): Array[Match] do return p
.search_all_in
(self)
370 # Split `self` using `p` as separator.
372 # assert "hello world".split('o') == ["hell", " w", "rld"]
373 fun split
(p
: Pattern): Array[SELFTYPE]
375 var matches
= p
.split_in
(self)
376 var res
= new Array[SELFTYPE].with_capacity
(matches
.length
)
377 for m
in matches
do res
.add
(m
.to_s
)
381 # @deprecated alias for `split`
382 fun split_with
(p
: Pattern): Array[SELFTYPE] do return self.split
(p
)
384 # Split `self` on the first `=`
386 # assert "hello".split_once_on('l') == ["he", "lo"]
387 # assert "a, b, c, d, e".split_once_on(", ") == ["a", "b, c, d, e"]
388 fun split_once_on
(p
: Pattern): Array[SELFTYPE]
390 var m
= p
.search_in
(self, 0)
391 if m
== null then return [self]
392 return new Array[SELFTYPE].with_items
(substring
(0, m
.from
), substring_from
(m
.after
))
395 # Replace all occurences of a pattern with a string
397 # assert "hlelo".replace("le", "el") == "hello"
398 # assert "hello".replace('l', "") == "heo"
399 fun replace
(p
: Pattern, string
: SELFTYPE): SELFTYPE
401 return self.split_with
(p
).join
(string
)
404 # Does `self` contains at least one instance of `pattern`?
406 # assert "hello".has('l')
407 # assert "hello".has("ll")
408 # assert not "hello".has("lll")
409 fun has
(pattern
: Pattern): Bool do return pattern
.is_in
(self)