bs-platform
Version:
bucklescript compiler, ocaml standard libary by bucklescript and its required runtime support
275 lines (244 loc) • 6.49 kB
JavaScript
;
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;
} else 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"
];
} else {
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;
} else 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"
];
} else {
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"
];
} else {
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"
];
} else {
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 */