ocaml-core / base / core / lib / ordered_collection_common.ml

The branch 'bash' does not exist.
let invalid_argf = Core_printf.invalid_argf

let normalize ~length_fun t i =
  if i < 0 then length_fun t + i else i

let slice ~length_fun ~sub_fun t start stop =
  let stop = if stop = 0 then length_fun t else stop in
  let pos = normalize ~length_fun t start in
  let len = (normalize ~length_fun t stop) - pos in
  sub_fun t ~pos ~len

let get_pos_len_exn ?(pos=0) ?len ~length =
  if pos < 0 then
    invalid_argf "Negative position: %d < 0" pos ();
  if pos > length then
    invalid_argf "Start position after the end: %d > %d"
      pos length ();
  let maxlen = length - pos in
  let len =
    match len with
    | None -> maxlen
    | Some len ->
      if len < 0 then
        invalid_argf "Negative length: %d" len ()
      else if len > maxlen then
        invalid_argf "pos + len past end: %d + %d > %d"
          pos len length ()
      else
        len
  in
  (pos, len)
;;

let get_pos_len ?pos ?len ~length =
  try Result.Ok (get_pos_len_exn ?pos ?len ~length)
  with Invalid_argument s -> Result.Error s
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.