Skip to content

Commit 22df6eb

Browse files
committed
Now without bug.
1 parent 0c4d7b3 commit 22df6eb

File tree

3 files changed

+31
-35
lines changed

3 files changed

+31
-35
lines changed

pkgs/characters/lib/src/characters_impl.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -509,16 +509,16 @@ class StringCharacterRange implements CharacterRange {
509509
var index = _end;
510510
while (index < _string.length) {
511511
var char = _string.codeUnitAt(index);
512-
var surrogateBits = char ^ 0xD800;
512+
var surrogate = char ^ 0xD800;
513513
var category = categoryControl;
514514
var nextIndex = index + 1;
515-
if (surrogateBits >= 0x3FF) {
515+
if (surrogate > 0x3FF) {
516516
category = low(char);
517517
} else if (nextIndex < _string.length) {
518-
var nextChar = _string.codeUnitAt(nextIndex) ^ 0xDC00;
519-
if (nextChar <= 0x3FF) {
518+
var nextSurrogate = _string.codeUnitAt(nextIndex) ^ 0xDC00;
519+
if (nextSurrogate <= 0x3FF) {
520520
nextIndex += 1;
521-
category = high(char, nextChar);
521+
category = high(surrogate, nextSurrogate);
522522
}
523523
}
524524
state = move(state, category);

pkgs/characters/lib/src/grapheme_clusters/breaks.dart

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ class Breaks {
7777
assert(cursor < end);
7878
var char = base.codeUnitAt(cursor++);
7979
var surrogate = char ^ 0xD800;
80-
if (surrogate >= 0x3FF) {
80+
if (surrogate > 0x3FF) {
8181
state = move(state, low(char));
8282
return;
8383
}
@@ -114,31 +114,35 @@ class Breaks {
114114
var cursorBefore = cursor - 1;
115115
var prevChar = base.codeUnitAt(cursorBefore);
116116
var prevSurrogate = prevChar ^ 0xD800;
117-
int prevCategory;
118117
if (prevSurrogate > 0x7FF) {
119118
// Not surrogate.
120-
prevCategory = low(prevChar);
121-
} else if (prevSurrogate <= 0x3FF) {
122-
// Lead surrogate. Check for a following tail surrogate.
123-
int tailSurrogate;
124-
if (cursor < end &&
125-
(tailSurrogate = base.codeUnitAt(cursor) ^ 0xDC00) <= 0x3FF) {
126-
cursor += 1;
127-
prevCategory = high(prevSurrogate, tailSurrogate);
128-
} else {
129-
prevCategory = categoryControl;
130-
}
131-
} else {
119+
var prevCategory = low(prevChar);
120+
state = move(stateCAny, prevCategory);
121+
return cursorBefore;
122+
}
123+
int prevCategory;
124+
if (prevSurrogate > 0x3FF) {
132125
// Tail surrogate, check for prior lead surrogate.
133126
int leadSurrogate;
134127
var leadIndex = cursorBefore - 1;
128+
prevSurrogate &= 0x3FF;
135129
if (leadIndex >= start &&
136130
(leadSurrogate = base.codeUnitAt(leadIndex) ^ 0xD800) <= 0x3FF) {
137131
prevCategory = high(leadSurrogate, prevSurrogate);
138132
cursorBefore = leadIndex;
139133
} else {
140134
prevCategory = categoryControl;
141135
}
136+
} else {
137+
// Lead surrogate. Check for a following tail surrogate.
138+
int tailSurrogate;
139+
if (cursor < end &&
140+
(tailSurrogate = base.codeUnitAt(cursor) ^ 0xDC00) <= 0x3FF) {
141+
cursor += 1;
142+
prevCategory = high(prevSurrogate, tailSurrogate);
143+
} else {
144+
prevCategory = categoryControl;
145+
}
142146
}
143147
state = move(stateCAny, prevCategory);
144148
return cursorBefore;
@@ -354,7 +358,7 @@ int previousBreak(String text, int start, int end, int index) {
354358
if (indexAfter < end) {
355359
var secondSurrogate = text.codeUnitAt(indexAfter) ^ 0xDC00;
356360
if (secondSurrogate <= 0x3FF) {
357-
category = high(nextChar, secondSurrogate);
361+
category = high(nextSurrogate, secondSurrogate);
358362
}
359363
}
360364
} else {

pkgs/characters/tool/bin/generate_tables.dart

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -320,18 +320,17 @@ String _lookupSurrogatesMethod(
320320
int startOffset,
321321
int chunkSize,
322322
) {
323-
if (chunkSize == 1024) {
323+
var shift = chunkSize.bitLength - 1;
324+
assert(shift <= 10);
325+
if (shift == 10) {
324326
return '''
325327
$preferInline
326328
int $name(int lead, int tail) {
327-
var chunkStart = $startName.codeUnitAt($startOffset + (0x3ff & lead));
328-
var index = chunkStart + (0x3ff & tail);
329-
return $dataName.codeUnitAt(index);
329+
var chunkStart = $startName.codeUnitAt($startOffset + lead);
330+
return $dataName.codeUnitAt(chunkStart + tail);
330331
}
331332
''';
332333
}
333-
var shift = chunkSize.bitLength - 1;
334-
assert(shift <= 10);
335334
if (shift < 10) {
336335
return '''
337336
$preferInline
@@ -341,18 +340,11 @@ int $name(int lead, int tail) {
341340
var chunkStart = $startName.codeUnitAt($startOffset + offset);
342341
return $dataName.codeUnitAt(chunkStart + tail);
343342
}
344-
''';
345-
} else {
346-
assert(shift == 10);
347-
return '''
348-
$preferInline
349-
int $name(int lead, int tail) {
350-
var chunkStart = $startName.codeUnitAt($startOffset + lead);
351-
return $dataName.codeUnitAt(chunkStart + tail);
352-
}
353343
''';
354344
}
355345
// Add code if shift > 10 ever becomes optimal for table size.
346+
// Example code:
347+
throw UnimplementedError('No code for chunk sizes > 10 bits');
356348
}
357349

358350
// -----------------------------------------------------------------------------

0 commit comments

Comments
 (0)