Skip to content

Commit 0800ab2

Browse files
authored
cleanup the escape condition (#96)
1 parent a7d73f8 commit 0800ab2

File tree

2 files changed

+16
-18
lines changed

2 files changed

+16
-18
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ Since [Hermes doesn't support the `Intl.Segmenter` API](https://github.com/faceb
220220

221221
| Name | Unicode® | ESM? | Size | Size (min) | Size (min+gzip) | Size (min+br) | Size (min+zstd) |
222222
|------------------------------|----------|------|----------:|-----------:|----------------:|--------------:|----------------:|
223-
| `unicode-segmenter/grapheme` | 16.0.0 | ✔️ | 15,607 | 12,166 | 5,042 | 3,747 | 4,725 |
223+
| `unicode-segmenter/grapheme` | 16.0.0 | ✔️ | 15,586 | 12,166 | 5,037 | 3,717 | 4,726 |
224224
| `graphemer` | 15.0.0 | ✖️ ️| 410,435 | 95,104 | 15,752 | 10,660 | 15,911 |
225225
| `grapheme-splitter` | 10.0.0 | ✖️ | 122,252 | 23,680 | 7,852 | 4,841 | 6,750 |
226226
| `@formatjs/intl-segmenter`* | 15.0.0 | ✖️ | 603,285 | 369,560 | 72,218 | 49,416 | 67,975 |
@@ -236,7 +236,7 @@ Since [Hermes doesn't support the `Intl.Segmenter` API](https://github.com/faceb
236236

237237
| Name | Bytecode size | Bytecode size (gzip)* |
238238
|------------------------------|--------------:|----------------------:|
239-
| `unicode-segmenter/grapheme` | 20,932 | 10,991 |
239+
| `unicode-segmenter/grapheme` | 21,001 | 11,063 |
240240
| `graphemer` | 133,978 | 31,713 |
241241
| `grapheme-splitter` | 63,835 | 19,137 |
242242

src/grapheme.js

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export function* graphemeSegments(input) {
4949
if (cp == null) return;
5050

5151
/** Current cursor position. */
52-
let cursor = 0;
52+
let cursor = cp < 0xFFFF ? 1 : 2;
5353

5454
/** Total length of the input string. */
5555
let len = input.length;
@@ -86,21 +86,7 @@ export function* graphemeSegments(input) {
8686
/** Memoize the beginnig code point a the segment. */
8787
let _hd = cp;
8888

89-
while (true) {
90-
cursor += cp < 0xFFFF ? 1 : 2;
91-
92-
if (cursor >= len) {
93-
yield {
94-
segment: input.slice(index, cursor),
95-
index,
96-
input,
97-
_hd,
98-
_catBegin,
99-
_catEnd: catBefore,
100-
};
101-
return;
102-
}
103-
89+
while (cursor < len) {
10490
// Note: Lazily update `consonant` and `linker` state
10591
// which is a extra overhead only for Hindi text.
10692
if (cp >= 2325) {
@@ -151,8 +137,20 @@ export function* graphemeSegments(input) {
151137
_hd = cp;
152138
}
153139

140+
cursor += cp < 0xFFFF ? 1 : 2;
154141
catBefore = catAfter;
155142
}
143+
144+
if (index < len) {
145+
yield {
146+
segment: input.slice(index),
147+
index,
148+
input,
149+
_hd,
150+
_catBegin,
151+
_catEnd: catBefore,
152+
};
153+
}
156154
}
157155

158156
/**

0 commit comments

Comments
 (0)