package slice

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file bytes_labels.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
(*
 * Copyright (c) 2024 Romain Calascibetta <romain.calascibetta@gmail.com>
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *)

include Bytes

let[@inline always] blit src ~src_off dst ~dst_off ~len =
  Bytes.blit src src_off dst dst_off len

let[@inline always] blit_to_bytes src ~src_off dst ~dst_off ~len =
  Bytes.blit src src_off dst dst_off len

let[@inline always] blit_from_bytes src ~src_off dst ~dst_off ~len =
  Bytes.blit src src_off dst dst_off len

let[@inline always] blit_from_string src ~src_off dst ~dst_off ~len =
  Bytes.blit_string src src_off dst dst_off len

let[@inline always] unsafe_blit src ~src_off dst ~dst_off ~len =
  Bytes.unsafe_blit src src_off dst dst_off len

let[@inline always] unsafe_blit_from_bytes src ~src_off dst ~dst_off ~len =
  Bytes.unsafe_blit src src_off dst dst_off len

let[@inline always] unsafe_blit_to_bytes src ~src_off dst ~dst_off ~len =
  Bytes.unsafe_blit src src_off dst dst_off len

let[@inline always] unsafe_blit_from_string src ~src_off dst ~dst_off ~len =
  Bytes.unsafe_blit_string src src_off dst dst_off len

let[@inline always] unsafe_fill t ~off ~len chr =
  Bytes.unsafe_fill t off len chr

let fill t ?(off = 0) ?len chr =
  let len = match len with Some len -> len | None -> Bytes.length t - off in
  Bytes.fill t off len chr

(* NOTE(dinosaure): according to our test, [string] must do a copy. We should
   not use [Bytes.unsafe_of_string] (even if we would like to have a sub-part
   of the given string) because it can returns an alias of it. *)
let string ?(off = 0) ?len str =
  let len =
    match len with Some len -> len | None -> String.length str - off
  in
  if len < 0 || off < 0 || off > String.length str - len then
    invalid_arg "Bytes.string";
  let buf = Bytes.create len in
  Bytes.blit_string str off buf 0 len;
  buf

let overlap a b = if a == b then Some (Bytes.length a, 0, 0) else None
let sub t ~off ~len = Bytes.sub t off len

let memchr t ~off ~len chr =
  if len < 0 || off < 0 || off > Bytes.length t - len then
    invalid_arg "Bytes.memchr";
  let max_idx = off + len - 1 in
  let rec go idx =
    if idx > max_idx then -1
    else if Bytes.unsafe_get t idx == chr then idx
    else go (idx + 1)
  in
  go off

external unsafe_get : bytes -> int -> char = "%bytes_unsafe_get"
external unsafe_set : bytes -> int -> char -> unit = "%bytes_unsafe_set"
external unsafe_get_uint16_ne : bytes -> int -> int = "%caml_bytes_get16u"

external unsafe_set_uint16_ne : bytes -> int -> int -> unit
  = "%caml_bytes_set16u"

external unsafe_get_int32_ne : bytes -> int -> int32 = "%caml_bytes_get32u"

external unsafe_set_int32_ne : bytes -> int -> int32 -> unit
  = "%caml_bytes_set32u"

external unsafe_get_int64_ne : bytes -> int -> int64 = "%caml_bytes_get64u"

external unsafe_set_int64_ne : bytes -> int -> int64 -> unit
  = "%caml_bytes_set64u"

external swap16 : int -> int = "%bswap16"
external swap32 : int32 -> int32 = "%bswap_int32"
external swap64 : int64 -> int64 = "%bswap_int64"

let unsafe_get_uint8 buf i = Char.code (unsafe_get buf i) [@@inline]
let unsafe_set_uint8 buf i v = unsafe_set buf i (Char.unsafe_chr v) [@@inline]

let unsafe_get_int8 buf i =
  (unsafe_get_uint8 buf i lsl (Sys.int_size - 8)) asr (Sys.int_size - 8)
[@@inline]

let unsafe_set_int8 = unsafe_set_uint8

let unsafe_get_uint16_le buf i =
  if Sys.big_endian then swap16 (unsafe_get_uint16_ne buf i)
  else unsafe_get_uint16_ne buf i
[@@inline]

let unsafe_get_uint16_be buf i =
  if not Sys.big_endian then swap16 (unsafe_get_uint16_ne buf i)
  else unsafe_get_uint16_ne buf i
[@@inline]

let unsafe_get_int16_ne buf i =
  (unsafe_get_uint16_ne buf i lsl (Sys.int_size - 16)) asr (Sys.int_size - 16)
[@@inline]

let unsafe_get_int16_le buf i =
  (unsafe_get_uint16_le buf i lsl (Sys.int_size - 16)) asr (Sys.int_size - 16)
[@@inline]

let unsafe_get_int16_be buf i =
  (unsafe_get_uint16_be buf i lsl (Sys.int_size - 16)) asr (Sys.int_size - 16)
[@@inline]

let unsafe_set_int16_ne = unsafe_set_uint16_ne

let unsafe_set_int16_le buf i v =
  if Sys.big_endian then unsafe_set_uint16_ne buf i (swap16 v)
  else unsafe_set_uint16_ne buf i v
[@@inline]

let unsafe_set_int16_be buf i v =
  if not Sys.big_endian then unsafe_set_uint16_ne buf i (swap16 v)
  else unsafe_set_uint16_ne buf i v
[@@inline]

let unsafe_set_uint16_le = unsafe_set_int16_le
let unsafe_set_uint16_be = unsafe_set_int16_be

let unsafe_get_int32_le buf i =
  if Sys.big_endian then swap32 (unsafe_get_int32_ne buf i)
  else unsafe_get_int32_ne buf i
[@@inline]

let unsafe_get_int32_be buf i =
  if not Sys.big_endian then swap32 (unsafe_get_int32_ne buf i)
  else unsafe_get_int32_ne buf i
[@@inline]

let unsafe_set_int32_le buf i v =
  if Sys.big_endian then unsafe_set_int32_ne buf i (swap32 v)
  else unsafe_set_int32_ne buf i v
[@@inline]

let unsafe_set_int32_be buf i v =
  if not Sys.big_endian then unsafe_set_int32_ne buf i (swap32 v)
  else unsafe_set_int32_ne buf i v
[@@inline]

let unsafe_get_int64_le buf i =
  if Sys.big_endian then swap64 (unsafe_get_int64_ne buf i)
  else unsafe_get_int64_ne buf i
[@@inline]

let unsafe_get_int64_be buf i =
  if not Sys.big_endian then swap64 (unsafe_get_int64_ne buf i)
  else unsafe_get_int64_ne buf i
[@@inline]

let unsafe_set_int64_le buf i v =
  if Sys.big_endian then unsafe_set_int64_ne buf i (swap64 v)
  else unsafe_set_int64_ne buf i v
[@@inline]

let unsafe_set_int64_be buf i v =
  if not Sys.big_endian then unsafe_set_int64_ne buf i (swap64 v)
  else unsafe_set_int64_ne buf i v
[@@inline]

(* NOTE(dinosaure): here, we implement a fast equal function where we don't use
   local functions (to avoid closure allocations) and unroll the equality on
   words (8 and 64) to speed-up the computation. *)

let rec equal_bytes a src_off b dst_off idx len =
  idx >= len
  || Bytes.unsafe_get a (src_off + idx) == Bytes.unsafe_get b (dst_off + idx)
     && equal_bytes a src_off b dst_off (idx + 1) len

let[@inline always] eq_word a src_off b dst_off idx =
  Int64.equal
    (unsafe_get_int64_ne a (src_off + idx))
    (unsafe_get_int64_ne b (dst_off + idx))

(* unroll by 8 bytes *)
let rec equal_word a src_off b dst_off idx len =
  if idx + 8 > len then equal_bytes a src_off b dst_off idx len
  else
    eq_word a src_off b dst_off idx
    && equal_word a src_off b dst_off (idx + 8) len

(* unroll by 64 bytes *)
let rec equal_words a src_off b dst_off idx len =
  if idx + 64 > len then equal_word a src_off b dst_off idx len
  else
    eq_word a src_off b dst_off idx
    && eq_word a src_off b dst_off (idx + 8)
    && eq_word a src_off b dst_off (idx + 16)
    && eq_word a src_off b dst_off (idx + 24)
    && eq_word a src_off b dst_off (idx + 32)
    && eq_word a src_off b dst_off (idx + 40)
    && eq_word a src_off b dst_off (idx + 48)
    && eq_word a src_off b dst_off (idx + 56)
    && equal_words a src_off b dst_off (idx + 64) len

let[@inline always] unsafe_equal a ~src_off b ~dst_off ~len =
  equal_words a src_off b dst_off 0 len

let rec compare_bytes a src_off b dst_off idx len =
  if idx >= len then 0
  else
    let x = Bytes.unsafe_get a (src_off + idx)
    and y = Bytes.unsafe_get b (dst_off + idx) in
    if x == y then compare_bytes a src_off b dst_off (idx + 1) len
    else if x < y then -1
    else 1

let rec compare_words a src_off b dst_off idx len =
  if idx + 8 > len then compare_bytes a src_off b dst_off idx len
  else
    let x = unsafe_get_int64_be a (src_off + idx)
    and y = unsafe_get_int64_be b (dst_off + idx) in
    if Int64.equal x y then compare_words a src_off b dst_off (idx + 8) len
    else Int64.unsigned_compare x y

let[@inline always] unsafe_memcmp a ~src_off b ~dst_off ~len =
  compare_words a src_off b dst_off 0 len

let memcmp a ~src_off b ~dst_off ~len =
  if
    len < 0
    || src_off < 0
    || src_off > Bytes.length a - len
    || dst_off < 0
    || dst_off > Bytes.length b - len
  then invalid_arg "Bytes.memcmp";
  unsafe_memcmp a ~src_off b ~dst_off ~len