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
))
88 protected fun is_in
(s
: Text): Bool do return search_index_in
(s
, 0) != -1
91 # BM_Pattern are pre-compiled string motif for the Boyer-Moore algorithm.
92 # (cf. A Fast String Searching Algorithm, with R.S. Boyer. Communications
93 # of the Association for Computing Machinery, 20(10), 1977, pp. 762-772.)
94 # http://www.cs.utexas.edu/users/moore/best-ideas/string-searching/index.html
96 # var pat = new BM_Pattern("hello")
97 # assert "I say hello to the world!".search(pat).from == 6
98 # assert "I say goodbye to the world!".search(pat) == null
102 redef fun to_s
do return _motif
104 # boyer-moore search gives the position of the first occurrence of a pattern starting at position `from`
105 redef fun search_index_in
(s
, from
)
112 while j
< n
- m
+ 1 do
113 var i
= m
- 1 # Cursor in the pattern
114 while i
>= 0 and _motif
.chars
[i
] == s
.chars
[i
+ j
] do i
-= 1
118 var gs
= _gs
[i
] # Good shift
119 var bc
= bc
(s
.chars
[i
+j
]) - m
+ 1 + i
# Bad char
120 # Both are true, do move to the best
128 return -1 # found nothing
131 # boyer-moore search. Return null if not found
132 redef fun search_in
(s
, from
)
134 var to
= search_index_in
(s
, from
)
138 return new Match(s
.to_s
, to
, _length
)
142 # Compile a new motif
146 _length
= _motif
.length
147 _gs
= new Array[Int].with_capacity
(_length
)
148 _bc_table
= new ArrayMap[Char, Int]
154 private var motif
: String
156 # length of the motif
157 private var length
: Int
159 private fun bc
(e
: Char): Int
161 if _bc_table
.has_key
(e
) then
169 private var gs
: Array[Int]
172 private var bc_table
: Map[Char, Int]
174 private fun compute_bc
180 _bc_table
[x
.chars
[i
]] = m
- i
- 1
185 private fun suffixes
: Array[Int]
189 var suff
= new Array[Int].filled_with
(m
, m
)
195 if i
> g
and suff
[i
+m-1-f
] < i
- g
then
196 suff
[i
] = suff
[i
+m-1-f
]
200 while g
>= 0 and x
.chars
[g
] == x
.chars
[g
+ m
- 1 - f
] do g
-= 1
208 private fun compute_gs
221 if i
== -1 or suff
[i
] == i
+ 1 then
222 while j
< m
- 1 - i
do
223 if _gs
[j
] == m
then _gs
[j
] = m
- 1 - i
231 _gs
[m
- 1 - suff
[i
]] = m
- 1 - i
236 redef fun hash
do return _motif
.hash
237 redef fun ==(o
) do return o
isa BM_Pattern and o
._motif
== _motif
240 # Matches are a part of a `Text` found by a `Pattern`.
242 # The base string matched
245 # The starting position in the string
248 # The length of the matching part
251 # The position of the first character just after the matching part.
252 # May be out of the base string
253 fun after
: Int do return from
+ length
255 # The contents of the matching part
256 redef fun to_s
do return string
.substring
(from
,length
)
258 # Matches `len` characters of `s` from `f`.
259 init(s
: String, f
: Int, len
: Int)
261 assert positive_length
: len
>= 0
262 assert valid_from
: f
>= 0
263 assert valid_after
: f
+ len
<= s
.length
273 redef fun search_index_in
(s
, from
)
277 if s
.chars
[from
] == self then return from
283 redef fun search_in
(s
, from
)
285 var pos
= search_index_in
(s
, from
)
289 return new Match(s
.to_s
, pos
, 1)
297 redef fun search_index_in
(s
, from
)
300 var stop
= s
.length
- length
+ 1
303 while i
>= 0 and self.chars
[i
] == s
.chars
[i
+ from
] do i
-= 1
305 if i
< 0 then return from
306 # Not found so try next one
312 redef fun search_in
(s
, from
)
314 var pos
= search_index_in
(s
, from
)
318 return new Match(s
.to_s
, pos
, length
)
322 # Search the first occurence of the pattern `p`.
323 # Return null if not found.
325 # assert "I say hello to the world!".search("hello").from == 6
326 # assert "I say goodbye to the world!".search("hello") == null
327 fun search
(p
: Pattern): nullable Match do return p
.search_in
(self, 0)
329 # Search the first occurence of the pattern `p` after `from`.
330 # The search starts at `from`.
331 # Return null if not found.
333 # assert "I say hello to the world!".search_from("hello",4).from == 6
334 # assert "I say hello to the world!".search_from("hello",7) == null
335 fun search_from
(p
: Pattern, from
: Int): nullable Match do return p
.search_in
(self, from
)
337 # Search all occurrences of p into self.
339 # var a = new Array[Int]
340 # for i in "hello world".search_all('o') do
344 fun search_all
(p
: Pattern): Array[Match] do return p
.search_all_in
(self)
346 # Split `self` using `p` as separator.
348 # assert "hello world".split('o') == ["hell", " w", "rld"]
349 fun split
(p
: Pattern): Array[SELFTYPE]
351 var matches
= p
.split_in
(self)
352 var res
= new Array[SELFTYPE].with_capacity
(matches
.length
)
353 for m
in matches
do res
.add
(m
.to_s
)
357 # @deprecated alias for `split`
358 fun split_with
(p
: Pattern): Array[SELFTYPE] do return self.split
(p
)
360 # Split `self` on the first `=`
362 # assert "hello".split_once_on('l') == ["he", "lo"]
363 # assert "a, b, c, d, e".split_once_on(", ") == ["a", "b, c, d, e"]
364 fun split_once_on
(p
: Pattern): Array[SELFTYPE]
366 var m
= p
.search_in
(self, 0)
367 if m
== null then return [self]
368 return new Array[SELFTYPE].with_items
(substring
(0, m
.from
), substring_from
(m
.after
))
371 # Replace all occurences of a pattern with a string
373 # assert "hlelo".replace("le", "el") == "hello"
374 # assert "hello".replace('l', "") == "heo"
375 fun replace
(p
: Pattern, string
: SELFTYPE): SELFTYPE
377 return self.split_with
(p
).join
(string
)
380 # Does `self` contains at least one instance of `pattern`?
382 # assert "hello".has('l')
383 # assert "hello".has("ll")
384 # assert not "hello".has("lll")
385 fun has
(pattern
: Pattern): Bool do return pattern
.is_in
(self)