File: stdcompat__bytes.ml.in

package info (click to toggle)
ocaml-stdcompat 14-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 5,152 kB
  • sloc: ml: 22,329; makefile: 211; sh: 120
file content (281 lines) | stat: -rw-r--r-- 8,621 bytes parent folder | download
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
@BEGIN_BEFORE_4_02_0@
include Stdcompat__string

let empty = ""

let of_string = copy

let to_string = copy

let sub_string = sub

let extend s left right =
  let len = left + length s + right in
  let result = create len in
  let trim_left = max (- left) 0 in
  unsafe_blit s trim_left result (max left 0)
    (length s - trim_left - max (- right) 0);
  result

let blit_string = blit

let cat = ( ^ )

let unsafe_of_string s = s

let unsafe_to_string s = s
@END_BEFORE_4_02_0@
@BEGIN_FROM_4_02_0@
include Bytes

@BEGIN_BEFORE_4_05_0@
let index_opt s c =
  Stdcompat__tools.option_find (index s) c

let rindex_opt s c =
  Stdcompat__tools.option_find (rindex s) c

let index_from_opt s i c =
  Stdcompat__tools.option_find (index_from s i) c

let rindex_from_opt s i c =
  Stdcompat__tools.option_find (rindex_from s i) c
@END_BEFORE_4_05_0@
@END_FROM_4_02_0@

@BEGIN_BEFORE_4_03_0@
let uppercase_ascii = uppercase

let lowercase_ascii = lowercase

let capitalize_ascii = capitalize

let uncapitalize_ascii = uncapitalize

let equal : t -> t -> bool = ( = )
@END_BEFORE_4_03_0@

@BEGIN_BEFORE_4_07_0@
let of_seq g =
  Stdcompat__buffer.to_bytes (Stdcompat__buffer.of_seq g)

let to_seq s = Stdcompat__tools.vec_to_seq length unsafe_get s

let to_seqi s = Stdcompat__tools.vec_to_seqi length unsafe_get s
@END_BEFORE_4_07_0@

@BEGIN_BEFORE_4_08_0@
let pad_sign i w =
  let pad_width = Stdcompat__sys.int_size - w in
  (i lsl pad_width) asr pad_width

let index_out_of_bounds () =
  invalid_arg "index out of bounds"

let unsafe_get_uint8 b i =
  int_of_char (get b i)

let get_uint8 b i =
  if i < 0 || i >= length b then
    index_out_of_bounds ();
  unsafe_get_uint8 b i

let get_int8 b i =
  pad_sign (get_uint8 b i) 8

let get_uint16_le b i =
  if i < 0 || succ i >= length b then
    index_out_of_bounds ();
  let l = unsafe_get_uint8 b i in
  let u = unsafe_get_uint8 b (succ i) in
  l lor u lsl 8

let get_uint16_be b i =
  if i < 0 || succ i >= length b then
    index_out_of_bounds ();
  let u = unsafe_get_uint8 b i in
  let l = unsafe_get_uint8 b (succ i) in
  l lor u lsl 8

let get_uint16_ne b i =
  if Stdcompat__sys.big_endian then get_uint16_be b i
  else get_uint16_le b i

let get_int16_le b i =
  pad_sign (get_uint16_le b i) 16

let get_int16_be b i =
  pad_sign (get_uint16_be b i) 16

let get_int16_ne b i =
  pad_sign (get_uint16_ne b i) 16

let get_int32_le b i =
  if i < 0 || i + 3 >= length b then
    index_out_of_bounds ();
  let i0 = unsafe_get_uint8 b i in
  let i1 = unsafe_get_uint8 b (i + 1) in
  let i2 = unsafe_get_uint8 b (i + 2) in
  let i3 = unsafe_get_uint8 b (i + 3) in
  Int32.logor (Int32.of_int i0)
    (Int32.logor (Int32.shift_left (Int32.of_int i1) 8)
      (Int32.logor (Int32.shift_left (Int32.of_int i2) 16)
        (Int32.shift_left (Int32.of_int i3) 24)))

let get_int32_be b i =
  if i < 0 || i + 3 >= length b then
    index_out_of_bounds ();
  let i3 = unsafe_get_uint8 b i in
  let i2 = unsafe_get_uint8 b (i + 1) in
  let i1 = unsafe_get_uint8 b (i + 2) in
  let i0 = unsafe_get_uint8 b (i + 3) in
  Int32.logor (Int32.of_int i0)
    (Int32.logor (Int32.shift_left (Int32.of_int i1) 8)
      (Int32.logor (Int32.shift_left (Int32.of_int i2) 16)
        (Int32.shift_left (Int32.of_int i3) 24)))

let get_int32_ne b i =
  if Stdcompat__sys.big_endian then get_int32_be b i
  else get_int32_le b i

let get_int64_le b i =
  if i < 0 || i + 7 >= length b then
    index_out_of_bounds ();
  let i0 = unsafe_get_uint8 b i in
  let i1 = unsafe_get_uint8 b (i + 1) in
  let i2 = unsafe_get_uint8 b (i + 2) in
  let i3 = unsafe_get_uint8 b (i + 3) in
  let i4 = unsafe_get_uint8 b (i + 4) in
  let i5 = unsafe_get_uint8 b (i + 5) in
  let i6 = unsafe_get_uint8 b (i + 6) in
  let i7 = unsafe_get_uint8 b (i + 7) in
  Int64.logor (Int64.of_int i0)
    (Int64.logor (Int64.shift_left (Int64.of_int i1) 8)
      (Int64.logor (Int64.shift_left (Int64.of_int i2) 16)
        (Int64.logor (Int64.shift_left (Int64.of_int i3) 24)
          (Int64.logor (Int64.shift_left (Int64.of_int i4) 32)
            (Int64.logor (Int64.shift_left (Int64.of_int i5) 40)
              (Int64.logor (Int64.shift_left (Int64.of_int i6) 48)
                (Int64.shift_left (Int64.of_int i7) 56)))))))

let get_int64_be b i =
  if i < 0 || i + 7 >= length b then
    index_out_of_bounds ();
  let i7 = unsafe_get_uint8 b i in
  let i6 = unsafe_get_uint8 b (i + 1) in
  let i5 = unsafe_get_uint8 b (i + 2) in
  let i4 = unsafe_get_uint8 b (i + 3) in
  let i3 = unsafe_get_uint8 b (i + 4) in
  let i2 = unsafe_get_uint8 b (i + 5) in
  let i1 = unsafe_get_uint8 b (i + 6) in
  let i0 = unsafe_get_uint8 b (i + 7) in
  Int64.logor (Int64.of_int i0)
    (Int64.logor (Int64.shift_left (Int64.of_int i1) 8)
      (Int64.logor (Int64.shift_left (Int64.of_int i2) 16)
        (Int64.logor (Int64.shift_left (Int64.of_int i3) 24)
          (Int64.logor (Int64.shift_left (Int64.of_int i4) 32)
            (Int64.logor (Int64.shift_left (Int64.of_int i5) 40)
              (Int64.logor (Int64.shift_left (Int64.of_int i6) 48)
                (Int64.shift_left (Int64.of_int i7) 56)))))))

let get_int64_ne b i =
  if Stdcompat__sys.big_endian then get_int64_be b i
  else get_int64_le b i

let unsafe_set_uint8 b i v =
  unsafe_set b i (char_of_int (v land 0xFF))

let set_uint8 b i v =
  if i < 0 || i >= length b then
    index_out_of_bounds ();
  unsafe_set_uint8 b i v

let set_int8 = set_uint8

let set_uint16_le b i v =
  if i < 0 || succ i >= length b then
    index_out_of_bounds ();
  unsafe_set_uint8 b i v;
  unsafe_set_uint8 b (succ i) (v lsr 8)

let set_uint16_be b i v =
  if i < 0 || succ i >= length b then
    index_out_of_bounds ();
  unsafe_set_uint8 b i (v lsr 8);
  unsafe_set_uint8 b (succ i) v

let set_uint16_ne b i v =
  if Stdcompat__sys.big_endian then set_uint16_be b i v
  else set_uint16_le b i v

let set_int16_le = set_uint16_le

let set_int16_be = set_uint16_be

let set_int16_ne = set_uint16_ne

let set_int32_le b i v =
  if i < 0 || i + 3 >= length b then
    index_out_of_bounds ();
  unsafe_set_uint8 b i (Int32.to_int v);
  unsafe_set_uint8 b (i + 1) (Int32.to_int (Int32.shift_right v 8));
  unsafe_set_uint8 b (i + 2) (Int32.to_int (Int32.shift_right v 16));
  unsafe_set_uint8 b (i + 3) (Int32.to_int (Int32.shift_right v 24))

let set_int32_be b i v =
  if i < 0 || i + 3 >= length b then
    index_out_of_bounds ();
  unsafe_set_uint8 b i (Int32.to_int (Int32.shift_right v 24));
  unsafe_set_uint8 b (i + 1) (Int32.to_int (Int32.shift_right v 16));
  unsafe_set_uint8 b (i + 2) (Int32.to_int (Int32.shift_right v 8));
  unsafe_set_uint8 b (i + 3) (Int32.to_int v)

let set_int32_ne b i v =
  if Stdcompat__sys.big_endian then set_int32_be b i v
  else set_int32_le b i v

let set_int64_le b i v =
  if i < 0 || i + 7 >= length b then
    index_out_of_bounds ();
  unsafe_set_uint8 b i (Int64.to_int v);
  unsafe_set_uint8 b (i + 1) (Int64.to_int (Int64.shift_right v 8));
  unsafe_set_uint8 b (i + 2) (Int64.to_int (Int64.shift_right v 16));
  unsafe_set_uint8 b (i + 3) (Int64.to_int (Int64.shift_right v 24));
  unsafe_set_uint8 b (i + 4) (Int64.to_int (Int64.shift_right v 32));
  unsafe_set_uint8 b (i + 5) (Int64.to_int (Int64.shift_right v 40));
  unsafe_set_uint8 b (i + 6) (Int64.to_int (Int64.shift_right v 48));
  unsafe_set_uint8 b (i + 7) (Int64.to_int (Int64.shift_right v 56))

let set_int64_be b i v =
  if i < 0 || i + 7 >= length b then
    index_out_of_bounds ();
  unsafe_set_uint8 b i (Int64.to_int (Int64.shift_right v 56));
  unsafe_set_uint8 b (i + 1) (Int64.to_int (Int64.shift_right v 48));
  unsafe_set_uint8 b (i + 2) (Int64.to_int (Int64.shift_right v 40));
  unsafe_set_uint8 b (i + 3) (Int64.to_int (Int64.shift_right v 32));
  unsafe_set_uint8 b (i + 4) (Int64.to_int (Int64.shift_right v 24));
  unsafe_set_uint8 b (i + 5) (Int64.to_int (Int64.shift_right v 16));
  unsafe_set_uint8 b (i + 6) (Int64.to_int (Int64.shift_right v 8));
  unsafe_set_uint8 b (i + 7) (Int64.to_int v)

let set_int64_ne b i v =
  if Stdcompat__sys.big_endian then set_int64_be b i v
  else set_int64_le b i v
@END_BEFORE_4_08_0@

@BEGIN_FROM_4_03_0@
external unsafe_blit_string :
  string -> int -> bytes -> int -> int -> unit = "caml_blit_string"[@@noalloc]
@END_FROM_4_03_0@
@BEGIN_BEFORE_4_03_0@
@BEGIN_FROM_3_08_0@
external unsafe_blit_string :
  string -> int -> Stdcompat__init.bytes -> int -> int -> unit =
    "caml_blit_string" "noalloc"
@END_FROM_3_08_0@
@BEGIN_BEFORE_3_08_0@
external unsafe_blit_string :
  string -> int -> Stdcompat__init.bytes -> int -> int -> unit =
    "blit_string" "noalloc"
@END_BEFORE_3_08_0@
@END_BEFORE_4_03_0@