Commits

camlspotter committed 0ce1cc0

bugfix of contains

  • Participants
  • Parent commits b44e00f

Comments (0)

Files changed (1)

File lib/xstring.ml

     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
-  if pos + sub_len > str_len then false
-  else 
-    let rec iter i = 
-      if str.[pos + i] <> sub.[i] then false
-      else 
-        let i' = i + 1 in
-        if i' = sub_len then true
-        else iter i'
-    in
-    iter 0
-
-TEST_UNIT "Xstring.contains" = 
-    assert (contains ~needle:"hello" "hello world")
-    assert (contains ~needle:"hello" "bye world" = false)
-;;    
-
 let is_prefix' ?(from=0) sub str =
   let sublen = String.length sub in
   try 
     assert (is_prefix "hello" "hello world")
 ;;
 
+let contains ?from:(pos=0) ~needle:sub str =
+  let str_len = String.length str in
+  let sub_len = String.length sub in
+  if pos + sub_len > str_len then false
+  else 
+    let rec iter pos = 
+      if pos + sub_len > str_len then false
+      else if is_prefix ~from:pos sub str then true
+      else iter (pos+1)
+    in
+    iter pos
+
+TEST_UNIT "Xstring.contains" = 
+    assert (contains ~needle:"hello" "hello world")
+    assert (contains ~needle:"hello" "bye world" = false)
+    assert (contains ~needle:"shindanmaker.com" "http://shindanmaker.com/341161")
+;;    
+
 let is_postfix sub str =
   let sublen = String.length sub in
   try postfix sublen str = sub with _ -> false