Current section
Files
Jump to
Current section
Files
src/string_width_ffi.mjs
import { toList } from './gleam.mjs'
// for regex101: [\x1b\x9b][[\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\d\/#&.:=?%@~_]+)*|[a-zA-Z\d]+(?:;[-a-zA-Z\d\/#&.:=?%@~_]*)*)?(?:\x07|\x1b\x5c|\x9c))|(?:(?:\d{1,4}(?:;\d{0,4})*)?[\dA-PR-TZcf-nq-uy=><~]))
const ANSI_RE = new RegExp("[\x1B\x9B][[\\]()#;?]*(?:(?:(?:(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]+)*|[a-zA-Z\\d]+(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?(?:\x07|\x1B\x5C|\x9C))|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-nq-uy=><~]))", "mg")
const ASCII_RE = new RegExp("[\x20-\x7e]+", "g")
export function ansi_re() {
return ANSI_RE
}
export function ascii_re() {
return ASCII_RE
}
export function scan_poslen(re, str) {
let match
let matches = []
re.lastIndex = 0
while (null !== (match = re.exec(str))) {
matches.push([match.index, re.lastIndex-match.index])
}
return toList(matches)
}
export function encoded_byte_size(str) {
return str.length
}
export function unsafe_split(str, at) {
return [str.slice(0, at), str.slice(at)]
}
export function fold_codepoints(str, state, fun) {
for (const codePoint of str) {
state = fun(state, codePoint.codePointAt(0))
}
return state
}
const segmenter = new Intl.Segmenter();
export function fold_graphemes(str, state, fun) {
for(const { segment } of segmenter.segment(str)) {
state = fun(state, segment)
}
return state
}
export function utf_codepoint_to_string(cp) {
return String.fromCodePoint(cp)
}
export function table_lookup(table, value) {
let hi = value >> 8; // $int.bitwise_shift_right(value, 8);
let md = (value & 0xff) >> 3; // $int.bitwise_shift_right($int.bitwise_and(value, 0xff), 3);
let lo = value & 7; // $int.bitwise_and(value, 7);
// return (remainderInt(
// $int.bitwise_shift_right(at(table, at(table, hi) * 32 + md), lo),
// 2
// )) !== 0;
return ((table.byteAt(table.byteAt(hi) * 32 + md) >> lo) & 1) > 0;
}