package base

  1. Overview
  2. Docs
Full standard library replacement for OCaml

Install

dune-project
 Dependency

Authors

Maintainers

Sources

v0.17.3.tar.gz
md5=2100b0ed13fecf43be86ed45c5b2cc4d
sha512=628610caff7e124631870fa1e29661caac28bdfdb18750ee43b868037da3d65d6dd9023b4be7c4c52405679efb5e865a6632d95606a22b28a36636a6bf706ef3

doc/src/base/ordered_collection_common0.ml.html

Source file ordered_collection_common0.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
(* Split off to avoid a cyclic dependency with [Or_error]. *)

open! Import

let invalid_argf = Printf.invalid_argf

let slow_check_pos_len_exn ~pos ~len ~total_length =
  if pos < 0 then invalid_argf "Negative position: %d" pos ();
  if len < 0 then invalid_argf "Negative length: %d" len ();
  (* We use [pos > total_length - len] rather than [pos + len > total_length] to avoid the
     possibility of overflow. *)
  if pos > total_length - len
  then invalid_argf "pos + len past end: %d + %d > %d" pos len total_length ()
  [@@cold] [@@inline never] [@@local never] [@@specialise never]
;;

let check_pos_len_exn ~pos ~len ~total_length =
  (* This is better than [slow_check_pos_len_exn] for two reasons:

     - much less inlined code
     - only one conditional jump

     The reason it works is that checking [< 0] is testing the highest order bit, so
     [a < 0 || b < 0] is the same as [a lor b < 0].

     [pos + len] can overflow, so [pos > total_length - len] is not equivalent to
     [total_length - len - pos < 0], we need to test for [pos + len] overflow as
     well. *)
  let stop = pos + len in
  if pos lor len lor stop lor (total_length - stop) < 0
  then slow_check_pos_len_exn ~pos ~len ~total_length
  [@@inline always]
;;

let get_pos_len_exn ?(pos = 0) ?len () ~total_length =
  let len =
    match len with
    | Some i -> i
    | None -> total_length - pos
  in
  check_pos_len_exn ~pos ~len ~total_length;
  pos, len
;;

module Private = struct
  let slow_check_pos_len_exn = slow_check_pos_len_exn
end