All pastes #985796 Raw Edit

OCaml return example

public text v1 · immutable
·published 2008-04-15 09:43 UTC
rendered paste body
(* Find the index of string 'sub' within 'str' *)
let find str sub =
  let sublen = String.length sub in
  if sublen = 0 then return 0;

  let len = String.length str in
  for i = 0 to len - sublen do
    let j = ref 0 in
    while String.unsafe_get str (i + !j) = String.unsafe_get sub !j do
      incr j;
      if !j = sublen then return i
    done;
  done;
  raise Not_found