forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring.js
272 lines (242 loc) · 6.24 KB
/
string.js
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
'use strict';
var List = require("./list.js");
var Bytes = require("./bytes.js");
var Caml_bytes = require("./caml_bytes.js");
var Caml_int32 = require("./caml_int32.js");
var Caml_primitive = require("./caml_primitive.js");
var Caml_builtin_exceptions = require("./caml_builtin_exceptions.js");
function make(n, c) {
return Caml_bytes.bytes_to_string(Bytes.make(n, c));
}
function init(n, f) {
return Caml_bytes.bytes_to_string(Bytes.init(n, f));
}
function copy(s) {
return Caml_bytes.bytes_to_string(Bytes.copy(Caml_bytes.bytes_of_string(s)));
}
function sub(s, ofs, len) {
return Caml_bytes.bytes_to_string(Bytes.sub(Caml_bytes.bytes_of_string(s), ofs, len));
}
function concat(sep, l) {
if (l) {
var hd = l[0];
var num = /* record */[/* contents */0];
var len = /* record */[/* contents */0];
List.iter((function (s) {
num[0] = num[0] + 1 | 0;
len[0] = len[0] + s.length | 0;
return /* () */0;
}), l);
var r = Caml_bytes.caml_create_bytes(len[0] + Caml_int32.imul(sep.length, num[0] - 1 | 0) | 0);
Caml_bytes.caml_blit_string(hd, 0, r, 0, hd.length);
var pos = /* record */[/* contents */hd.length];
List.iter((function (s) {
Caml_bytes.caml_blit_string(sep, 0, r, pos[0], sep.length);
pos[0] = pos[0] + sep.length | 0;
Caml_bytes.caml_blit_string(s, 0, r, pos[0], s.length);
pos[0] = pos[0] + s.length | 0;
return /* () */0;
}), l[1]);
return Caml_bytes.bytes_to_string(r);
} else {
return "";
}
}
function iter(f, s) {
return Bytes.iter(f, Caml_bytes.bytes_of_string(s));
}
function iteri(f, s) {
return Bytes.iteri(f, Caml_bytes.bytes_of_string(s));
}
function map(f, s) {
return Caml_bytes.bytes_to_string(Bytes.map(f, Caml_bytes.bytes_of_string(s)));
}
function mapi(f, s) {
return Caml_bytes.bytes_to_string(Bytes.mapi(f, Caml_bytes.bytes_of_string(s)));
}
function is_space(param) {
var switcher = param - 9 | 0;
if (switcher > 4 || switcher < 0) {
return switcher === 23;
} else {
return switcher !== 2;
}
}
function trim(s) {
if (s === "" || !(is_space(s.charCodeAt(0)) || is_space(s.charCodeAt(s.length - 1 | 0)))) {
return s;
} else {
return Caml_bytes.bytes_to_string(Bytes.trim(Caml_bytes.bytes_of_string(s)));
}
}
function escaped(s) {
var needs_escape = function (_i) {
while(true) {
var i = _i;
if (i >= s.length) {
return false;
} else {
var match = s.charCodeAt(i);
if (match >= 32) {
var switcher = match - 34 | 0;
if (switcher > 58 || switcher < 0) {
if (switcher >= 93) {
return true;
} else {
_i = i + 1 | 0;
continue ;
}
} else if (switcher > 57 || switcher < 1) {
return true;
} else {
_i = i + 1 | 0;
continue ;
}
} else {
return true;
}
}
};
};
if (needs_escape(0)) {
return Caml_bytes.bytes_to_string(Bytes.escaped(Caml_bytes.bytes_of_string(s)));
} else {
return s;
}
}
function index_rec(s, lim, _i, c) {
while(true) {
var i = _i;
if (i >= lim) {
throw Caml_builtin_exceptions.not_found;
}
if (s.charCodeAt(i) === c) {
return i;
} else {
_i = i + 1 | 0;
continue ;
}
};
}
function index(s, c) {
return index_rec(s, s.length, 0, c);
}
function index_from(s, i, c) {
var l = s.length;
if (i < 0 || i > l) {
throw [
Caml_builtin_exceptions.invalid_argument,
"String.index_from / Bytes.index_from"
];
}
return index_rec(s, l, i, c);
}
function rindex_rec(s, _i, c) {
while(true) {
var i = _i;
if (i < 0) {
throw Caml_builtin_exceptions.not_found;
}
if (s.charCodeAt(i) === c) {
return i;
} else {
_i = i - 1 | 0;
continue ;
}
};
}
function rindex(s, c) {
return rindex_rec(s, s.length - 1 | 0, c);
}
function rindex_from(s, i, c) {
if (i < -1 || i >= s.length) {
throw [
Caml_builtin_exceptions.invalid_argument,
"String.rindex_from / Bytes.rindex_from"
];
}
return rindex_rec(s, i, c);
}
function contains_from(s, i, c) {
var l = s.length;
if (i < 0 || i > l) {
throw [
Caml_builtin_exceptions.invalid_argument,
"String.contains_from / Bytes.contains_from"
];
}
try {
index_rec(s, l, i, c);
return true;
}
catch (exn){
if (exn === Caml_builtin_exceptions.not_found) {
return false;
} else {
throw exn;
}
}
}
function contains(s, c) {
return contains_from(s, 0, c);
}
function rcontains_from(s, i, c) {
if (i < 0 || i >= s.length) {
throw [
Caml_builtin_exceptions.invalid_argument,
"String.rcontains_from / Bytes.rcontains_from"
];
}
try {
rindex_rec(s, i, c);
return true;
}
catch (exn){
if (exn === Caml_builtin_exceptions.not_found) {
return false;
} else {
throw exn;
}
}
}
function uppercase(s) {
return Caml_bytes.bytes_to_string(Bytes.uppercase(Caml_bytes.bytes_of_string(s)));
}
function lowercase(s) {
return Caml_bytes.bytes_to_string(Bytes.lowercase(Caml_bytes.bytes_of_string(s)));
}
function capitalize(s) {
return Caml_bytes.bytes_to_string(Bytes.capitalize(Caml_bytes.bytes_of_string(s)));
}
function uncapitalize(s) {
return Caml_bytes.bytes_to_string(Bytes.uncapitalize(Caml_bytes.bytes_of_string(s)));
}
var compare = Caml_primitive.caml_string_compare;
var fill = Bytes.fill;
var blit = Bytes.blit_string;
exports.make = make;
exports.init = init;
exports.copy = copy;
exports.sub = sub;
exports.fill = fill;
exports.blit = blit;
exports.concat = concat;
exports.iter = iter;
exports.iteri = iteri;
exports.map = map;
exports.mapi = mapi;
exports.trim = trim;
exports.escaped = escaped;
exports.index = index;
exports.rindex = rindex;
exports.index_from = index_from;
exports.rindex_from = rindex_from;
exports.contains = contains;
exports.contains_from = contains_from;
exports.rcontains_from = rcontains_from;
exports.uppercase = uppercase;
exports.lowercase = lowercase;
exports.capitalize = capitalize;
exports.uncapitalize = uncapitalize;
exports.compare = compare;
/* No side effect */