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.
23 fun search_index_in
(s
: String, from
: Int): Int is abstract
25 # Search `self` into `s` from a certain position.
26 # Return null if not found.
27 fun search_in
(s
: String, from
: Int): nullable Match is abstract
29 # Search all `self` occurrences into `s`.
30 fun search_all_in
(s
: String): Array[Match]
32 var res
= new Array[Match] # Result
33 var match
= search_in
(s
, 0)
34 while match
!= null do
36 match
= search_in
(s
, match
.after
)
41 # Split `s` using `self` is separator.
42 fun split_in
(s
: String): Array[Match]
44 var res
= new Array[Match] # Result
46 var match
= search_in
(s
, 0)
47 while match
!= null do
48 # Compute the splited part length
49 var len
= match
.from
- i
50 res
.add
(new Match(s
, i
, len
))
52 match
= search_in
(s
, i
)
55 res
.add
(new Match(s
, i
, s
.length
- i
))
60 # BM_Pattern are pre-compiled string motif for the Boyer-Moore algorithm.
61 # (cf. A Fast String Searching Algorithm, with R.S. Boyer. Communications
62 # of the Association for Computing Machinery, 20(10), 1977, pp. 762-772.)
63 # http://www.cs.utexas.edu/users/moore/best-ideas/string-searching/index.html
67 redef fun to_s
do return _motif
69 # boyer-moore search gives the position of the first occurrence of a pattern starting at position `from`
70 redef fun search_index_in
(s
, from
)
77 while j
< n
- m
+ 1 do
78 var i
= m
- 1 # Cursor in the pattern
79 while i
>= 0 and _motif
[i
] == s
[i
+ j
] do i
-= 1
83 var gs
= _gs
[i
] # Good shift
84 var bc
= bc
(s
[i
+j
]) - m
+ 1 + i
# Bad char
85 # Both are true, do move to the best
93 return -1 # found nothing
96 # boyer-moore search. Return null if not found
97 redef fun search_in
(s
, from
)
99 var to
= search_index_in
(s
, from
)
103 return new Match(s
, to
, _length
)
107 # Compile a new motif
111 _length
= motif
.length
112 _gs
= new Array[Int].with_capacity
(_length
)
113 _bc_table
= new ArrayMap[Char, Int]
121 # length of the motif
124 private fun bc
(e
: Char): Int
126 if _bc_table
.has_key
(e
) then
137 var _bc_table
: Map[Char, Int]
139 private fun compute_bc
145 _bc_table
[x
[i
]] = m
- i
- 1
150 private fun suffixes
: Array[Int]
154 var suff
= new Array[Int].filled_with
(m
, m
)
160 if i
> g
and suff
[i
+m-1-f
] < i
- g
then
161 suff
[i
] = suff
[i
+m-1-f
]
165 while g
>= 0 and x
[g
] == x
[g
+ m
- 1 - f
] do g
-= 1
173 private fun compute_gs
186 if i
== -1 or suff
[i
] == i
+ 1 then
187 while j
< m
- 1 - i
do
188 if _gs
[j
] == m
then _gs
[j
] = m
- 1 - i
196 _gs
[m
- 1 - suff
[i
]] = m
- 1 - i
201 redef fun hash
do return _motif
.hash
202 redef fun ==(o
) do return o
isa BM_Pattern and o
._motif
== _motif
205 # Matches are a part of a string.
207 # The base string matched
208 readable var _string
: String
210 # The starting position in the string
211 readable var _from
: Int
213 # The length of the matching part
214 readable var _length
: Int
216 # The position of the first character just after the matching part.
217 # May be out of the base string
218 fun after
: Int do return _from
+ _length
220 # The contents of the matching part
221 redef fun to_s
do return _string
.substring
(_from
, _length
)
223 # Matches `len` characters of `s` from `f`.
224 init(s
: String, f
: Int, len
: Int)
226 assert positive_length
: len
>= 0
227 assert valid_from
: f
>= 0
228 assert valid_after
: f
+ len
<= s
.length
238 redef fun search_index_in
(s
, from
)
242 if s
[from
] == self then return from
248 redef fun search_in
(s
, from
)
250 var pos
= search_index_in
(s
, from
)
254 return new Match(s
, pos
, 1)
262 redef fun search_index_in
(s
, from
)
265 var stop
= s
.length
- length
+ 1
268 while i
>= 0 and self[i
] == s
[i
+ from
] do i
-= 1
270 if i
< 0 then return from
271 # Not found so try next one
277 redef fun search_in
(s
, from
)
279 var pos
= search_index_in
(s
, from
)
283 return new Match(s
, pos
, length
)
287 # Like `search_from` but from the first character.
288 fun search
(p
: Pattern): nullable Match do return p
.search_in
(self, 0)
290 # Search the given pattern into self from a.
291 # The search starts at `from`.
292 # Return null if not found.
293 fun search_from
(p
: Pattern, from
: Int): nullable Match do return p
.search_in
(self, from
)
295 # Search all occurrences of p into self.
297 # var a = new Array[Int]
298 # for i in "hello world".search_all('o') do
302 fun search_all
(p
: Pattern): Array[Match] do return p
.search_all_in
(self)
304 # Split `self` using `p` as separator.
306 # assert "hello world".split('o') == ["hell", " w", "rld"]
307 fun split
(p
: Pattern): Array[String]
309 var matches
= p
.split_in
(self)
310 var res
= new Array[String].with_capacity
(matches
.length
)
311 for m
in matches
do res
.add
(m
.to_s
)
315 # @deprecated alias for `split`
316 fun split_with
(p
: Pattern): Array[String] do return self.split
(p
)
318 # Replace all occurences of a pattern with a string
320 # assert "hlelo".replace("le", "el") == "hello"
321 # assert "hello".replace('l', "") == "heo"
322 fun replace
(p
: Pattern, string
: String): String
324 return self.split_with
(p
).join
(string
)
327 # Escape the four characters < > & and " with their html counterpart
329 # assert "a&b->\"x\"".html_escape == "a&b->"x""
330 fun html_escape
: String
333 if ret
.has
('&') then ret
= ret
.replace
('&', "&")
334 if ret
.has
('<') then ret
= ret
.replace
('<', "<")
335 if ret
.has
('>') then ret
= ret
.replace
('>', ">")
336 if ret
.has
('"') then ret
= ret
.replace
('"', """)