UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

58 lines (57 loc) • 2.42 kB
import { t as avoidTrailingHighSurrogateBreak } from "./utf16-slice-CuDyEXgS.mjs"; //#region packages/normalization-core/src/grapheme.ts let graphemeSegmenter; function getGraphemeSegmenter() { graphemeSegmenter ??= new Intl.Segmenter(void 0, { granularity: "grapheme" }); return graphemeSegmenter; } /** * Chooses a whole-grapheme cut within the hard budget, honoring a usable preference. * If no whole grapheme fits, allowPartial permits a surrogate-safe progress cut; * a leading surrogate pair can exceed maxEnd by one code unit. */ function findGraphemeChunkEnd(e, t, n, r = n, i = true) { const a = Math.min(n, e.length); if (a <= t) return t; const o = Number.isInteger(r) && r > t && r <= a ? r : a; if (o === e.length) return o; const s = e.charCodeAt(o - 1); const c = e.charCodeAt(o); if (Number.isInteger(o) && s <= 127 && c <= 127 && !(s === 13 && c === 10)) return o; const l = getGraphemeSegmenter().segment(e); let u = l.containing(o)?.index ?? o; if (u <= t && o < a) u = a === e.length ? a : l.containing(a)?.index ?? a; return u > t ? u : i ? avoidTrailingHighSurrogateBreak(e, t, a) : t; } /** Width to reserve for the first whole grapheme, or zero for empty text. */ function firstGraphemeClusterLength(e) { if (!e) return 0; return getGraphemeSegmenter().segment(e).containing(0)?.segment.length ?? 0; } const WHITESPACE_GRAPHEME_RE = /^\s+$/u; /** Skips only whole whitespace graphemes, never the base of a space-plus-mark cluster. */ function skipWhitespaceGraphemes(e, t = 0, n = Number.POSITIVE_INFINITY) { if (!/\s/u.test(e.charAt(t))) return t; const r = getGraphemeSegmenter().segment(e); let i = t; for (let t = 0; t < n && i < e.length; t += 1) { const e = r.containing(i); if (!e || e.index !== i || !WHITESPACE_GRAPHEME_RE.test(e.segment)) break; i += e.segment.length; } return i; } /** Trims only whole trailing whitespace graphemes from a source prefix. */ function trimEndWhitespaceGraphemes(e, t = e.length) { if (!/\s/u.test(e.charAt(t - 1))) return e.slice(0, t); const n = getGraphemeSegmenter().segment(e); let r = t; while (r > 0) { const e = n.containing(r - 1); if (!e || e.index + e.segment.length > r || !WHITESPACE_GRAPHEME_RE.test(e.segment)) break; r = e.index; } return e.slice(0, r); } //#endregion export { trimEndWhitespaceGraphemes as i, firstGraphemeClusterLength as n, skipWhitespaceGraphemes as r, findGraphemeChunkEnd as t };