Merge: lib/popcorn: introduce BoolValidator in popcorn::pop_validation
[nit.git] / lib / core / text / string_search.nit
index 62f3b5e..7648816 100644 (file)
@@ -112,12 +112,12 @@ class BM_Pattern
                var j = from
                while j < n - m + 1 do
                        var i = m - 1 # Cursor in the pattern
-                       while i >= 0 and _motif.chars[i] == s.chars[i + j] do i -= 1
+                       while i >= 0 and _motif[i] == s[i + j] do i -= 1
                        if i < 0 then
                                return j
                        else
                                var gs = _gs[i] # Good shift
-                               var bc = bc(s.chars[i+j]) - m + 1 + i # Bad char
+                               var bc = bc(s[i+j]) - m + 1 + i # Bad char
                                # Both are true, do move to the best
                                if gs > bc then
                                        j += gs
@@ -308,7 +308,7 @@ redef class Char
        do
                var stop = s.length
                while from < stop do
-                       if s.chars[from] == self then return from
+                       if s[from] == self then return from
                        from += 1
                end
                return -1
@@ -334,7 +334,7 @@ redef class Text
                var stop = s.length - length + 1
                while from < stop do
                        var i = length - 1
-                       while i >= 0 and self.chars[i] == s.chars[i + from] do i -= 1
+                       while i >= 0 and self[i] == s[i + from] do i -= 1
                        # Test if we found
                        if i < 0 then return from
                        # Not found so try next one
@@ -470,15 +470,27 @@ redef class Text
                return res
        end
 
-       # Replace all occurences of `pattern` with `string`
+       # Replace all occurrences of `pattern` with `string`
        #
-       #     assert "hlelo".replace("le", "el")             ==  "hello"
-       #     assert "hello".replace('l', "")        ==  "heo"
-       fun replace(pattern: Pattern, string: SELFTYPE): String
+       #     assert "hlelo".replace("le", "el") == "hello"
+       #     assert "hello".replace('l', "")    == "heo"
+       #
+       #     var t: Text = "hello"
+       #     assert t.replace("hello", new FlatBuffer) == ""
+       fun replace(pattern: Pattern, string: Text): String
        do
                return self.split_with(pattern).join(string)
        end
 
+       # Replace the first occurrence of `pattern` with `string`
+       #
+       #     assert "hlelo".replace_first("le", "el") == "hello"
+       #     assert "hello".replace_first('l', "")    == "helo"
+       fun replace_first(pattern: Pattern, string: Text): String
+       do
+               return self.split_once_on(pattern).join(string)
+       end
+
        # Does `self` contains at least one instance of `pattern`?
        #
        #     assert "hello".has('l')