forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathext_bytes_test.ml
124 lines (106 loc) · 4.68 KB
/
ext_bytes_test.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
(* 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. *)
let suites : Mt.pair_suites ref = ref []
let test_id = ref 0
let eq loc x y = Mt.eq_suites ~test_id ~suites loc x y
external char_code: char -> int = "%identity"
external char_chr: int -> char = "%identity"
let escaped s =
let n = Pervasives.ref 0 in
for i = 0 to Bytes.length s - 1 do
n := !n +
(match Bytes.unsafe_get s i with
| '"' | '\\' | '\n' | '\t' | '\r' | '\b' -> 2
| ' ' .. '~' -> 1
| _ -> 4)
done;
if !n = Bytes.length s then Bytes.copy s else begin
let s' = Bytes.create !n in
n := 0;
for i = 0 to Bytes.length s - 1 do
begin match Bytes.unsafe_get s i with
| ('"' | '\\') as c ->
Bytes.unsafe_set s' !n '\\'; incr n; Bytes.unsafe_set s' !n c
| '\n' ->
Bytes.unsafe_set s' !n '\\'; incr n; Bytes.unsafe_set s' !n 'n'
| '\t' ->
Bytes.unsafe_set s' !n '\\'; incr n; Bytes.unsafe_set s' !n 't'
| '\r' ->
Bytes.unsafe_set s' !n '\\'; incr n; Bytes.unsafe_set s' !n 'r'
| '\b' ->
Bytes.unsafe_set s' !n '\\'; incr n; Bytes.unsafe_set s' !n 'b'
| (' ' .. '~') as c -> Bytes.unsafe_set s' !n c
| c ->
let a = char_code c in
Bytes.unsafe_set s' !n '\\';
incr n;
Bytes.unsafe_set s' !n (char_chr (48 + a / 100));
incr n;
Bytes.unsafe_set s' !n (char_chr (48 + (a / 10) mod 10));
incr n;
Bytes.unsafe_set s' !n (char_chr (48 + a mod 10));
end;
incr n
done;
s'
end
let starts_with (xs : bytes) prefix p =
let module X = struct exception H end in
let module Array = Bytes in
let len1, len2 = Array.(length xs, length prefix) in
if len2 > len1 then false
else
try
for i = 0 to len2 - 1 do
if not @@ p xs.(i) prefix.(i) then
raise X.H
done ;
true
with X.H -> false
let ()=
let a = Bytes.init 100 (fun i -> Char.chr i ) in
Bytes.blit a 5 a 10 10 ;
eq __LOC__ a
(Bytes.of_string "\000\001\002\003\004\005\006\007\b\t\005\006\007\b\t\n\011\012\r\014\020\021\022\023\024\025\026\027\028\029\030\031 !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abc")
let () =
let a = Bytes.init 100 (fun i -> Char.chr i ) in
Bytes.blit a 10 a 5 10 ;
eq __LOC__ a
(Bytes.of_string "\000\001\002\003\004\n\011\012\r\014\015\016\017\018\019\015\016\017\018\019\020\021\022\023\024\025\026\027\028\029\030\031 !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abc")
let () =
let a = String.init 100 (fun i -> Char.chr i) in
let b = Bytes.init 100 (fun i -> '\000') in
Bytes.blit_string a 10 b 5 10;
eq __LOC__ b (Bytes.of_string "\000\000\000\000\000\n\011\012\r\014\015\016\017\018\019\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000")
let () =
let s = Bytes.init 50_000 (fun i -> Char.chr (i mod 137)) in
let s1 = Bytes.to_string s in
let s2 = Bytes.of_string s1 in
eq __LOC__ s s2
let f (a : bytes) b =
a > b, a >= b , a < b, a <= b, a = b
let f_0 (a : int64) b =
a > b, a >= b , a < b, a <= b, a = b
let () =
Mt.from_pair_suites __MODULE__ !suites