lib: use a simple iteration in `html_escape` and move to `string`
authorAlexis Laferrière <alexis.laf@xymus.net>
Tue, 12 Aug 2014 21:05:14 +0000 (17:05 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Tue, 12 Aug 2014 21:13:37 +0000 (17:13 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/standard/string.nit
lib/standard/string_search.nit

index 07a7e1f..6007588 100644 (file)
@@ -554,6 +554,29 @@ abstract class Text
                return buf.to_s
        end
 
+       # Escape the four characters `<`, `>`, `&`, and `"` with their html counterpart
+       #
+       #     assert "a&b->\"x\"".html_escape      ==  "a&amp;b-&gt;&quot;x&quot;"
+       fun html_escape: SELFTYPE
+       do
+               var buf = new FlatBuffer
+
+               for i in [0..length[ do
+                       var c = chars[i]
+                       if c == '&' then
+                               buf.append "&amp;"
+                       else if c == '<' then
+                               buf.append "&lt;"
+                       else if c == '>' then
+                               buf.append "&gt;"
+                       else if c == '"' then
+                               buf.append "&quot;"
+                       else buf.add c
+               end
+
+               return buf.to_s
+       end
+
        # Equality of text
        # Two pieces of text are equals if thez have the same characters in the same order.
        #
index 0786ec9..ef18e08 100644 (file)
@@ -374,17 +374,4 @@ redef class Text
        do
                return self.split_with(p).join(string)
        end
-
-       # Escape the four characters `<`, `>`, `&`, and `"` with their html counterpart
-       #
-       #     assert "a&b->\"x\"".html_escape      ==  "a&amp;b-&gt;&quot;x&quot;"
-       fun html_escape: SELFTYPE
-       do
-               var ret = self
-               if ret.chars.has('&') then ret = ret.replace('&', "&amp;")
-               if ret.chars.has('<') then ret = ret.replace('<', "&lt;")
-               if ret.chars.has('>') then ret = ret.replace('>', "&gt;")
-               if ret.chars.has('"') then ret = ret.replace('"', "&quot;")
-               return ret
-       end
 end