camlspotter avatar camlspotter committed 627a015

fix of prefix/postfix things

Comments (0)

Files changed (2)

lib/xfilename.mli

 
 module Pervasives : sig
   val (^/) : string -> string -> string
-  (** Same as Filename.concat *)
+  (** Filename concatenation. If the second argument is absolute,
+      the first is ignored and the second is just returened.
+
+      "hello" ^/ "world" = "hello/world"
+      "hello" ^/ "/world" = "/world"
+  *)
 end
 
 
 end
 
 let split_at len str = String.sub str 0 len, String.sub str len (String.length str - len)
+
+TEST_UNIT "Xstring.split_at" = 
+    assert (split_at 3 "hello world" = ("hel", "lo world"))
+;;
+
 let take len str = String.sub str 0 len
 let prefix = take
 let drop len str = String.sub str len (String.length str - len)
 let drop_postfix len str = String.sub str 0 (String.length str - len)
+
+TEST_UNIT "Xstring.drop_postfix" = 
+    assert (drop_postfix 6 "hello world" = "hello")
+;;
+
 let postfix len str = 
   let l = String.length str in
   String.sub str (l-len) len
 
+TEST_UNIT "Xstring.drop_postfix" = 
+    assert (postfix 5 "hello world" = "world")
+;;
+
 let contains ?from:(pos=0) ~needle:sub str =
   let str_len = String.length str in
   let sub_len = String.length sub in
     in
     iter 0
 
-let is_prefix' ?from sub str =
-  if contains ?from ~needle:sub str then
-    Some (drop (String.length sub) str)
-  else None
+TEST_UNIT "Xstring.contains" = 
+    assert (contains ~needle:"hello" "hello world")
+    assert (contains ~needle:"hello" "bye world" = false)
+;;    
 
-let is_prefix ?from sub str = contains ?from ~needle:sub str
+let is_prefix' ?(from=0) sub str =
+  let sublen = String.length sub in
+  try 
+    if String.sub str from sublen = sub then Some (drop (from + sublen) str)
+    else None
+  with _ -> None
+
+TEST_UNIT "Xstring.is_prefix'" = 
+    assert (is_prefix' "hello" "hello world" = Some " world")
+;;
+
+let is_prefix ?(from=0) sub str =
+  let sublen = String.length sub in
+  try 
+    String.sub str from sublen = sub
+  with _ -> false
+
+TEST_UNIT "Xstring.is_prefix" = 
+    assert (is_prefix "hello" "hello world")
+;;
 
 let is_postfix sub str =
-  contains ~from:(String.length str - String.length sub) ~needle:sub str
+  let sublen = String.length sub in
+  try postfix sublen str = sub with _ -> false
+  
+TEST_UNIT "Xstring.is_postfix" = 
+    assert (is_postfix "world" "hello world")
+;;
 
 let is_postfix' sub str =
-  if contains ~from:(String.length str - String.length sub) ~needle:sub str then
-    Some (take (String.length str - String.length sub) str)
-  else None
+  let sublen = String.length sub in
+  try
+    if postfix sublen str = sub then Some (drop_postfix sublen str)
+    else None
+  with _ -> None
+
+TEST_UNIT "Xstring.is_postfix" = 
+    assert (is_postfix' "world" "hello world" = Some "hello ")
+;;
 
 let index_string_from str pos sub =
   let sub_len = String.length sub in
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.