UNPKG

openclaw

Version:

Multi-channel AI gateway with extensible messaging integrations

3 lines • 2.89 kB
import{t as avoidTrailingHighSurrogateBreak}from"./utf16-slice-DA3Mllbx.mjs"; //!packages/normalization-core/src/grapheme.ts let graphemeSegmenter;function getGraphemeSegmenter(){graphemeSegmenter??=new Intl.Segmenter(void 0,{granularity:`grapheme`});return graphemeSegmenter}function findGraphemeChunkEnd(e,t,n,r=n,i=true){return graphemeChunkEndFinder(e,false)(t,n,r,i)}function indexGraphemeBoundaries(e){const t=new Uint32Array(Math.ceil(e.length/32));for(const{index:n}of getGraphemeSegmenter().segment(e)){const e=Math.floor(n/32);t[e]=(t[e]??0)|1<<n%32}const n=new Uint32Array(t.length);let r=0;for(const[e,i]of t.entries()){n[e]=r;if(i!==0)r=e*32+31-Math.clz32(i)}return e=>{const r=Math.floor(e/32);const i=4294967295>>>31-(e&31);const a=(t[r]??0)&i;return a===0?n[r]??0:r*32+31-Math.clz32(a)}}function createGraphemeChunkEndFinder(e){return graphemeChunkEndFinder(e,true)}function graphemeChunkEndFinder(e,t){let n=0;let r;return(i,a,o=a,s=true)=>{const c=Math.min(a,e.length);if(c<=i)return i;const l=Number.isInteger(o)&&o>i&&o<=c?o:c;if(l===e.length)return l;const u=e.charCodeAt(l-1);const d=e.charCodeAt(l);if(Number.isInteger(l)&&u<=127&&d<=127&&!(u===13&&d===10)){n=l;return l}const f=i===0?0:n??0;if(t&&i!==0&&(n===void 0||f>i||i-f>c-i))r??=indexGraphemeBoundaries(e);const p=r?void 0:getGraphemeSegmenter().segment(e.slice(f,c+2));const m=r??(e=>f+(p?.containing(e-f)?.index??e-f));let h=m(l);if(h<=i&&l<c)h=c===e.length?c:m(c);if(h>i){n=h;return h}if(s){n=void 0;return avoidTrailingHighSurrogateBreak(e,i,c)}return i}}function firstGraphemeClusterLength(e){if(!e)return 0;return getGraphemeSegmenter().segment(e).containing(0)?.segment.length??0}const WHITESPACE_GRAPHEME_RE=/^\s+$/u;const INITIAL_WINDOW=256;function skipWhitespaceGraphemes(e,t=0,n=Number.POSITIVE_INFINITY){if(!/\s/u.test(e.charAt(t)))return t;const r=Math.min(t,2);const i=getGraphemeSegmenter().segment(e.slice(t-r,t+1)).containing(r);if(i&&i.index<r)return t;let a=t;let o=0;for(let t=INITIAL_WINDOW;o<n&&a<e.length;t*=2){let r=Math.min(e.length,a+t);if((e.codePointAt(r-1)??0)>65535)r+=1;for(const{segment:t}of getGraphemeSegmenter().segment(e.slice(a,r))){if(o>=n)return a;if(a+t.length===r&&r<e.length)break;if(!WHITESPACE_GRAPHEME_RE.test(t))return a;a+=t.length;o+=1}}return a}function trimEndWhitespaceGraphemes(e,t=e.length){if(!/\s/u.test(e.charAt(t-1)))return e.slice(0,t);for(let n=INITIAL_WINDOW;;n*=2){let r=Math.max(0,t-n);if((e.codePointAt(r-1)??0)>65535)r-=1;const i=getGraphemeSegmenter().segment(e.slice(r,t+2));let a=t-r;while(a>0){const t=i.containing(a-1);if(!t||t.index+t.segment.length>a||!WHITESPACE_GRAPHEME_RE.test(t.segment))return e.slice(0,r+a);if(t.index===0&&r>0)break;a=t.index}if(a===0)return e.slice(0,r)}}export{trimEndWhitespaceGraphemes as a,skipWhitespaceGraphemes as i,findGraphemeChunkEnd as n,firstGraphemeClusterLength as r,createGraphemeChunkEndFinder as t};