Sebastien Mondet avatar Sebastien Mondet committed 6f8b195

lib: add `index_of_string_reverse`

Comments (0)

Files changed (1)

   (** Find the first occurrence of the substring [(sub, sub_index,
       sub_length)] in a given string, starting at index [from].  *)
 
+  val index_of_string_reverse: ?from:int ->
+    ?sub_index:int -> ?sub_length:int -> t -> sub:t -> int option
+  (** Do like [index_of_string] but start from the end of the string. *)
+
 
   module Make_output: functor (Model: OUTPUT_MODEL) -> sig
 
     end in
     With_exn.f ()
 
+  let index_of_string_reverse ?(from=0) ?(sub_index=0) ?sub_length t ~sub =
+    let module With_exn = struct
+      exception Found of int
+
+      let f () =
+        let length_of_sub =
+          match sub_length with Some s -> s | None -> length sub - sub_index in
+        if from < 0 || sub_index < 0 || length_of_sub < 0
+        then None
+        else
+          begin try
+            let length_of_t = length t in
+            for i = length_of_t - length_of_sub downto from do
+              if compare_substring
+                  (t, i, length_of_sub)
+                  (sub, sub_index, length_of_sub) = 0
+              then raise (Found (i))
+            done;
+            None
+          with Found f -> Some f
+          end
+    end in
+    With_exn.f ()
 
 end
 
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.