Skip to content

Commit

Permalink
Ohhhhh
Browse files Browse the repository at this point in the history
  • Loading branch information
dra27 committed Apr 15, 2020
1 parent 5e53738 commit 76cbc71
Showing 1 changed file with 10 additions and 40 deletions.
50 changes: 10 additions & 40 deletions stdlib/string.ml
Original file line number Diff line number Diff line change
Expand Up @@ -240,48 +240,18 @@ let of_seq g = B.of_seq g |> bts

(** {6 Binary encoding/decoding of integers} *)

(* These are all duplicated in bytes.ml *)

external get_uint8 : string -> int -> int = "%string_safe_get"
external get_uint16_ne : string -> int -> int = "%caml_string_get16"
external get_int32_ne : string -> int -> int32 = "%caml_string_get32"
external get_int64_ne : string -> int -> int64 = "%caml_string_get64"
external swap16 : int -> int = "%bswap16"
external swap32 : int32 -> int32 = "%bswap_int32"
external swap64 : int64 -> int64 = "%bswap_int64"

let get_int8 b i =
((get_uint8 b i) lsl (Sys.int_size - 8)) asr (Sys.int_size - 8)

let get_uint16_le b i =
if Sys.big_endian then swap16 (get_uint16_ne b i)
else get_uint16_ne b i

let get_uint16_be b i =
if not Sys.big_endian then swap16 (get_uint16_ne b i)
else get_uint16_ne b i

let get_int16_ne b i =
((get_uint16_ne b i) lsl (Sys.int_size - 16)) asr (Sys.int_size - 16)

let get_int16_le b i =
((get_uint16_le b i) lsl (Sys.int_size - 16)) asr (Sys.int_size - 16)

let get_int16_be b i =
((get_uint16_be b i) lsl (Sys.int_size - 16)) asr (Sys.int_size - 16)

let get_int32_le b i =
if Sys.big_endian then swap32 (get_int32_ne b i)
else get_int32_ne b i

let get_int32_be b i =
if not Sys.big_endian then swap32 (get_int32_ne b i)
else get_int32_ne b i

let get_int64_le b i =
if Sys.big_endian then swap64 (get_int64_ne b i)
else get_int64_ne b i

let get_int64_be b i =
if not Sys.big_endian then swap64 (get_int64_ne b i)
else get_int64_ne b i
let get_int8 s i = B.get_int8 (bos s) i
let get_uint16_le s i = B.get_uint16_le (bos s) i
let get_uint16_be s i = B.get_uint16_be (bos s) i
let get_int16_ne s i = B.get_int16_ne (bos s) i
let get_int16_le s i = B.get_int16_le (bos s) i
let get_int16_be s i = B.get_int16_be (bos s) i
let get_int32_le s i = B.get_int32_le (bos s) i
let get_int32_be s i = B.get_int32_be (bos s) i
let get_int64_le s i = B.get_int64_le (bos s) i
let get_int64_be s i = B.get_int64_be (bos s) i

0 comments on commit 76cbc71

Please sign in to comment.