lib: promote replace, html_escape and simplify_path to the lib
authorJean Privat <jean@pryen.org>
Tue, 14 Feb 2012 16:10:30 +0000 (11:10 -0500)
committerJean Privat <jean@pryen.org>
Tue, 14 Feb 2012 16:30:28 +0000 (11:30 -0500)
These function are generic ans useful enough to be included in the
standard library.

Signed-off-by: Jean Privat <jean@pryen.org>

lib/standard/file.nit
lib/standard/string_search.nit
src/nitdoc.nit

index 903554c..ffbd263 100644 (file)
@@ -235,6 +235,35 @@ redef class String
                return "."
        end
 
+       # Simplify a file path by remove useless ".", removing "//", and resolving ".."
+       # ".." are not resolved if they start the path
+       # starting "/" is not removed
+       # trainling "/" is removed
+       #
+       # Note that the method only wonrk on the string:
+       #  * no I/O access is performed
+       #  * the validity of the path is not checked
+       #
+       #     "some/./complex/../../path/from/../to/a////file//".simplify_path  # -> "path/to/a/file"
+       #     "../dir/file" # -> "../dir/file"
+       #     "dir/../../" # -> ".."
+       #     "//absolute//path/" # -> "/absolute/path"
+       fun simplify_path: String
+       do
+               var a = self.split_with("/")
+               var a2 = new Array[String]
+               for x in a do
+                       if x == "." then continue
+                       if x == "" and not a2.is_empty then continue
+                       if x == ".." and not a2.is_empty then
+                               a2.pop
+                               continue
+                       end
+                       a2.push(x)
+               end
+               return a2.join("/")
+       end
+
        # Create a directory (and all intermediate directories if needed)
        fun mkdir
        do
index 162beec..8d4c131 100644 (file)
@@ -309,4 +309,26 @@ redef class String
        # Split self using '\n' is separator.
        #   "hello\nworld".split     # -> ["hello","world"]
        fun split: Array[String] do return split_with('\n')
+
+       # Replace all occurences of a pattern with a string
+       #
+       #   "hlelo".replace("le", "el") # -> "hello"
+       #   "hello".replace('l', "")    # -> "heo"
+       fun replace(p: Pattern, string: String): String
+       do
+               return self.split_with(p).join(string)
+       end
+
+       # Escape the four characters < > & and " with their html counterpart
+       #
+       #    "a&b->\"x\"".html_escape # -> "a&amp;b-&gt;&quot;x&quot;"
+       fun html_escape: String
+       do
+               var ret = self
+               if ret.has('&') then ret = ret.replace('&', "&amp;")
+               if ret.has('<') then ret = ret.replace('<', "&lt;")
+               if ret.has('>') then ret = ret.replace('>', "&gt;")
+               if ret.has('"') then ret = ret.replace('"', "&quot;")
+               return ret
+       end
 end
index 5d35bf6..846cdf0 100644 (file)
@@ -343,42 +343,6 @@ class DocContext
        end
 end
 
-redef class String
-       # Replace all occurence of pattern ith string
-       fun replace(p: Pattern, string: String): String
-       do
-               return self.split_with(p).join(string)
-       end
-
-       # Escape the following characters < > & and " with their html counterpart
-       fun html_escape: String
-       do
-               var ret = self
-               if ret.has('&') then ret = ret.replace('&', "&amp;")
-               if ret.has('<') then ret = ret.replace('<', "&lt;")
-               if ret.has('>') then ret = ret.replace('>', "&gt;")
-               if ret.has('"') then ret = ret.replace('"', "&quot;")
-               return ret
-       end
-
-       # Remove "/./", "//" and "bla/../"
-       fun simplify_path: String
-       do
-               var a = self.split_with("/")
-               var a2 = new Array[String]
-               for x in a do
-                       if x == "." then continue
-                       if x == "" and not a2.is_empty then continue
-                       if x == ".." and not a2.is_empty then
-                               a2.pop
-                               continue
-                       end
-                       a2.push(x)
-               end
-               return a2.join("/")
-       end
-end
-
 # A virtual module is used to work as an implicit main module that combine unrelated modules
 # Since conflict may arrise in a virtual module (the main method for instance) conflicts are disabled
 class MMVirtualModule