forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcaml_bytes.ml
143 lines (132 loc) · 4.79 KB
/
caml_bytes.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
(* Copyright (C) 2015-2016 Bloomberg Finance L.P.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* In addition to the permissions granted to you by the LGPL, you may combine
* or link a "work that uses the Library" with a publicly distributed version
* of this file to produce a combined library or application, then distribute
* that combined work under the terms of your choosing, with no requirement
* to comply with the obligations normally placed on you by section 4 of the
* LGPL version 3 (or the corresponding section of a later version of the LGPL
* should you choose to use a later version).
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *)
open Caml_bytes_extern
let get s i =
if i < 0 || i >= length s then
raise (Invalid_argument "index out of bounds")
else unsafe_get s i
let caml_fill_bytes (s : bytes) i l (c : char) =
if l > 0 then
for k = i to l + i - 1 do
unsafe_set s k c
done
let caml_create_bytes len : bytes =
(* Node raise [RangeError] exception *)
if len < 0 then raise (Invalid_argument "String.create")
else
let result = new_uninitialized len in
for i = 0 to len - 1 do
unsafe_set result i '\000'
done ;
result
(** Same as {!Array.prototype.copyWithin} *)
let copyWithin (s1 : bytes) i1 i2 len =
if i1 < i2 then (* nop for i1 = i2 *)
let range_a = length s1 - i2 - 1 in
let range_b = len - 1 in
let range = if range_a > range_b then range_b else range_a in
for j = range downto 0 do
unsafe_set s1 (i2 + j) (unsafe_get s1 (i1 + j))
done
else if i1 > i2 then
let range_a = length s1 - i1 - 1 in
let range_b = len - 1 in
let range = if range_a > range_b then range_b else range_a in
for k = 0 to range do
unsafe_set s1 (i2 + k) (unsafe_get s1 (i1 + k))
done
(* TODO: when the compiler could optimize small function calls,
use high order functions instead
*)
let caml_blit_bytes (s1:bytes) i1 (s2:bytes) i2 len =
if len > 0 then
if s1 == s2 then
copyWithin s1 i1 i2 len
else
let off1 = length s1 - i1 in
if len <= off1 then
for i = 0 to len - 1 do
unsafe_set s2 (i2 + i) (unsafe_get s1 (i1 + i))
done
else
begin
for i = 0 to off1 - 1 do
unsafe_set s2 (i2 + i) (unsafe_get s1 (i1 + i))
done;
for i = off1 to len - 1 do
unsafe_set s2 (i2 + i) '\000'
done
end
external to_int_array : bytes -> int array = "%identity"
let string_of_large_bytes bytes i len =
let s = ref "" in
let s_len = ref len in
let seg = 1024 in
if i = 0 && len <= 4 * seg && len = length bytes then
Caml_string_extern.of_small_int_array (to_int_array bytes)
else
begin
let offset = ref 0 in
while !s_len > 0 do
let next = if !s_len < 1024 then !s_len else seg in
let tmp_bytes = new_uninitialized next in
let () = caml_blit_bytes bytes !offset tmp_bytes 0 next in
s := !s ^ (Caml_string_extern.of_small_int_array (to_int_array tmp_bytes));
s_len := !s_len - next ;
offset := !offset + next;
done;
!s
end
let bytes_to_string a =
string_of_large_bytes a 0 (length a)
(**
TODO: [min] is not type specialized in OCaml
*)
let caml_blit_string (s1 : string) i1 (s2 : bytes) i2 (len : int ) =
if len > 0 then
let off1 = Caml_string_extern.length s1 - i1 in
if len <= off1 then
for i = 0 to len - 1 do
unsafe_set s2 (i2 + i) (Caml_string_extern.unsafe_get s1 (i1 + i))
done
else
begin
for i = 0 to off1 - 1 do
unsafe_set s2 (i2 + i) (Caml_string_extern.unsafe_get s1 (i1 + i))
done;
for i = off1 to len - 1 do
unsafe_set s2 (i2 + i) '\000'
done
end
(** checkout [Bytes.empty] -- to be inlined? *)
let bytes_of_string s =
let len = Caml_string_extern.length s in
let res = new_uninitialized len in
for i = 0 to len - 1 do
unsafe_set res i (Caml_string_extern.unsafe_get s i)
(* Note that when get a char and convert it to int immedately, should be optimized
should be [s.charCodeAt[i]]
*)
done;
res