@@ -43,30 +43,30 @@ export { GraphemeCategory };
4343 * @return {GraphemeSegmenter } iterator for grapheme cluster segments
4444 */
4545export function * graphemeSegments ( input ) {
46+ let cp = input . codePointAt ( 0 ) ;
47+
4648 // do nothing on empty string
47- if ( input === '' ) {
48- return ;
49- }
49+ if ( cp == null ) return ;
5050
51- /** @type { number } Current cursor position. */
51+ /** Current cursor position. */
5252 let cursor = 0 ;
5353
54- /** @type { number } Total length of the input string. */
54+ /** Total length of the input string. */
5555 let len = input . length ;
5656
57- /** @type {GraphemeCategoryNum | null } Category of codepoint immediately preceding cursor, if known. */
58- let catBefore = null ;
57+ /** @type {import('./_grapheme_data.js').GraphemeCategoryRange } */
58+ let cache = [ 0 , 0 , 2 /* GC_Control */ ] ;
5959
60- /** @type { GraphemeCategoryNum | null } Category of codepoint immediately preceding cursor, if known. */
61- let catAfter = null ;
60+ /** Category of codepoint immediately preceding cursor */
61+ let catBefore = cat ( cp , cache ) ;
6262
63- /** @type { GraphemeCategoryNum | null } Beginning category of a segment */
64- let catBegin = null ;
63+ /** Beginning category of a segment */
64+ let catBegin = catBefore ;
6565
66- /** @type {import('./_grapheme_data.js').GraphemeCategoryRange } */
67- let cache = [ 0 , 0 , 2 /* GC_Control */ ] ;
66+ /** @type {GraphemeCategoryNum | null } Category of codepoint immediately preceding cursor. */
67+ let catAfter = null ;
6868
69- /** @type { number } The number of RIS codepoints preceding `cursor`. */
69+ /** The number of RIS codepoints preceding `cursor`. */
7070 let risCount = 0 ;
7171
7272 /** Emoji state */
@@ -81,8 +81,6 @@ export function* graphemeSegments(input) {
8181 /** InCB=Consonant InCB=Linker x InCB=Consonant */
8282 let incb = false ;
8383
84- let cp = /** @type {number } */ ( input . codePointAt ( cursor ) ) ;
85-
8684 /** Memoize the beginnig code point a the segment. */
8785 let _hd = cp ;
8886
@@ -91,21 +89,13 @@ export function* graphemeSegments(input) {
9189 while ( true ) {
9290 cursor += cp < 0xFFFF ? 1 : 2 ;
9391
94- // Note: Of course the nullish coalescing is useful here,
95- // but avoid it for aggressive compatibility and perf claim
96- catBefore = catAfter ;
97- if ( catBefore === null ) {
98- catBefore = cat ( cp , cache ) ;
99- catBegin = catBefore ;
100- }
101-
10292 if ( cursor >= len ) {
10393 yield {
10494 segment : input . slice ( index , cursor ) ,
10595 index,
10696 input,
10797 _hd,
108- _catBegin : /** @type { typeof catBefore } */ ( catBegin ) ,
98+ _catBegin : catBegin ,
10999 _catEnd : catBefore ,
110100 } ;
111101 return ;
@@ -149,7 +139,7 @@ export function* graphemeSegments(input) {
149139 index,
150140 input,
151141 _hd,
152- _catBegin : /** @type { typeof catBefore } */ ( catBegin ) ,
142+ _catBegin : catBegin ,
153143 _catEnd : catBefore ,
154144 } ;
155145
@@ -160,6 +150,8 @@ export function* graphemeSegments(input) {
160150 catBegin = catAfter ;
161151 _hd = cp ;
162152 }
153+
154+ catBefore = catAfter ;
163155 }
164156}
165157
0 commit comments