bs-platform
Version:
ReScript compiler, OCaml standard libary by ReScript and its required runtime support
423 lines (372 loc) • 8.66 kB
JavaScript
import * as Bytes from "./bytes.mjs";
import * as Curry from "./curry.mjs";
import * as Caml_bytes from "./caml_bytes.mjs";
import * as Caml_string from "./caml_string.mjs";
import * as Caml_primitive from "./caml_primitive.mjs";
import * as Caml_js_exceptions from "./caml_js_exceptions.mjs";
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 ensure_ge(x, y) {
if (x >= y) {
return x;
}
throw {
RE_EXN_ID: "Invalid_argument",
_1: "String.concat",
Error: new Error()
};
}
function sum_lengths(_acc, seplen, _param) {
while(true) {
var param = _param;
var acc = _acc;
if (!param) {
return acc;
}
var tl = param.tl;
var hd = param.hd;
if (!tl) {
return hd.length + acc | 0;
}
_param = tl;
_acc = ensure_ge((hd.length + seplen | 0) + acc | 0, acc);
continue ;
};
}
function unsafe_blits(dst, _pos, sep, seplen, _param) {
while(true) {
var param = _param;
var pos = _pos;
if (!param) {
return dst;
}
var tl = param.tl;
var hd = param.hd;
if (tl) {
Caml_bytes.caml_blit_string(hd, 0, dst, pos, hd.length);
Caml_bytes.caml_blit_string(sep, 0, dst, pos + hd.length | 0, seplen);
_param = tl;
_pos = (pos + hd.length | 0) + seplen | 0;
continue ;
}
Caml_bytes.caml_blit_string(hd, 0, dst, pos, hd.length);
return dst;
};
}
function concat(sep, l) {
if (!l) {
return "";
}
var seplen = sep.length;
return Caml_bytes.bytes_to_string(unsafe_blits(Caml_bytes.caml_create_bytes(sum_lengths(0, seplen, l)), 0, sep, seplen, l));
}
function iter(f, s) {
for(var i = 0 ,i_finish = s.length; i < i_finish; ++i){
Curry._1(f, s.charCodeAt(i));
}
}
function iteri(f, s) {
for(var i = 0 ,i_finish = s.length; i < i_finish; ++i){
Curry._2(f, i, s.charCodeAt(i));
}
}
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) {
if (param > 13 || param < 9) {
return param === 32;
} else {
return param !== 11;
}
}
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;
}
var match = s.charCodeAt(i);
if (match < 32) {
return true;
}
if (match > 92 || match < 34) {
if (match >= 127) {
return true;
}
_i = i + 1 | 0;
continue ;
}
if (match > 91 || match < 35) {
return true;
}
_i = i + 1 | 0;
continue ;
};
};
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 {
RE_EXN_ID: "Not_found",
Error: new Error()
};
}
if (s.charCodeAt(i) === c) {
return i;
}
_i = i + 1 | 0;
continue ;
};
}
function index(s, c) {
return index_rec(s, s.length, 0, c);
}
function index_rec_opt(s, lim, _i, c) {
while(true) {
var i = _i;
if (i >= lim) {
return ;
}
if (s.charCodeAt(i) === c) {
return i;
}
_i = i + 1 | 0;
continue ;
};
}
function index_opt(s, c) {
return index_rec_opt(s, s.length, 0, c);
}
function index_from(s, i, c) {
var l = s.length;
if (i < 0 || i > l) {
throw {
RE_EXN_ID: "Invalid_argument",
_1: "String.index_from / Bytes.index_from",
Error: new Error()
};
}
return index_rec(s, l, i, c);
}
function index_from_opt(s, i, c) {
var l = s.length;
if (i < 0 || i > l) {
throw {
RE_EXN_ID: "Invalid_argument",
_1: "String.index_from_opt / Bytes.index_from_opt",
Error: new Error()
};
}
return index_rec_opt(s, l, i, c);
}
function rindex_rec(s, _i, c) {
while(true) {
var i = _i;
if (i < 0) {
throw {
RE_EXN_ID: "Not_found",
Error: new Error()
};
}
if (s.charCodeAt(i) === c) {
return i;
}
_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 {
RE_EXN_ID: "Invalid_argument",
_1: "String.rindex_from / Bytes.rindex_from",
Error: new Error()
};
}
return rindex_rec(s, i, c);
}
function rindex_rec_opt(s, _i, c) {
while(true) {
var i = _i;
if (i < 0) {
return ;
}
if (s.charCodeAt(i) === c) {
return i;
}
_i = i - 1 | 0;
continue ;
};
}
function rindex_opt(s, c) {
return rindex_rec_opt(s, s.length - 1 | 0, c);
}
function rindex_from_opt(s, i, c) {
if (i < -1 || i >= s.length) {
throw {
RE_EXN_ID: "Invalid_argument",
_1: "String.rindex_from_opt / Bytes.rindex_from_opt",
Error: new Error()
};
}
return rindex_rec_opt(s, i, c);
}
function contains_from(s, i, c) {
var l = s.length;
if (i < 0 || i > l) {
throw {
RE_EXN_ID: "Invalid_argument",
_1: "String.contains_from / Bytes.contains_from",
Error: new Error()
};
}
try {
index_rec(s, l, i, c);
return true;
}
catch (raw_exn){
var exn = Caml_js_exceptions.internalToOCamlException(raw_exn);
if (exn.RE_EXN_ID === "Not_found") {
return false;
}
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 {
RE_EXN_ID: "Invalid_argument",
_1: "String.rcontains_from / Bytes.rcontains_from",
Error: new Error()
};
}
try {
rindex_rec(s, i, c);
return true;
}
catch (raw_exn){
var exn = Caml_js_exceptions.internalToOCamlException(raw_exn);
if (exn.RE_EXN_ID === "Not_found") {
return false;
}
throw exn;
}
}
function uppercase_ascii(s) {
return Caml_bytes.bytes_to_string(Bytes.uppercase_ascii(Caml_bytes.bytes_of_string(s)));
}
function lowercase_ascii(s) {
return Caml_bytes.bytes_to_string(Bytes.lowercase_ascii(Caml_bytes.bytes_of_string(s)));
}
function capitalize_ascii(s) {
return Caml_bytes.bytes_to_string(Bytes.capitalize_ascii(Caml_bytes.bytes_of_string(s)));
}
function uncapitalize_ascii(s) {
return Caml_bytes.bytes_to_string(Bytes.uncapitalize_ascii(Caml_bytes.bytes_of_string(s)));
}
var compare = Caml_primitive.caml_string_compare;
function split_on_char(sep, s) {
var r = /* [] */0;
var j = s.length;
for(var i = s.length - 1 | 0; i >= 0; --i){
if (s.charCodeAt(i) === sep) {
r = {
hd: sub(s, i + 1 | 0, (j - i | 0) - 1 | 0),
tl: r
};
j = i;
}
}
return {
hd: sub(s, 0, j),
tl: r
};
}
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 make = Caml_string.make;
var fill = Bytes.fill;
var blit = Bytes.blit_string;
function equal(prim, prim$1) {
return prim === prim$1;
}
export {
make ,
init ,
copy ,
sub ,
fill ,
blit ,
concat ,
iter ,
iteri ,
map ,
mapi ,
trim ,
escaped ,
index ,
index_opt ,
rindex ,
rindex_opt ,
index_from ,
index_from_opt ,
rindex_from ,
rindex_from_opt ,
contains ,
contains_from ,
rcontains_from ,
uppercase ,
lowercase ,
capitalize ,
uncapitalize ,
uppercase_ascii ,
lowercase_ascii ,
capitalize_ascii ,
uncapitalize_ascii ,
compare ,
equal ,
split_on_char ,
}
/* No side effect */