Skip to content

Commit

Permalink
Use String.codeUnitAt instead of String.codeUnits[] in ParagraphBound…
Browse files Browse the repository at this point in the history
…ary (#120234)

* paragraph-boundary-opt

* address comments

* address comments

---------

Co-authored-by: Renzo Olivares <[email protected]>
  • Loading branch information
Renzo-Olivares and Renzo Olivares authored Feb 10, 2023
1 parent 9682359 commit 5dbd281
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
18 changes: 8 additions & 10 deletions packages/flutter/lib/src/services/text_boundary.dart
Original file line number Diff line number Diff line change
Expand Up @@ -150,17 +150,16 @@ class ParagraphBoundary extends TextBoundary {
return 0;
}

final List<int> codeUnits = _text.codeUnits;
int index = position;

if (index > 1 && codeUnits[index] == 0xA && codeUnits[index - 1] == 0xD) {
if (index > 1 && _text.codeUnitAt(index) == 0x0A && _text.codeUnitAt(index - 1) == 0x0D) {
index -= 2;
} else if (TextLayoutMetrics.isLineTerminator(codeUnits[index])) {
} else if (TextLayoutMetrics.isLineTerminator(_text.codeUnitAt(index))) {
index -= 1;
}

while (index > 0) {
if (TextLayoutMetrics.isLineTerminator(codeUnits[index])) {
if (TextLayoutMetrics.isLineTerminator(_text.codeUnitAt(index))) {
return index + 1;
}
index -= 1;
Expand All @@ -183,19 +182,18 @@ class ParagraphBoundary extends TextBoundary {
return 0;
}

final List<int> codeUnits = _text.codeUnits;
int index = position;

while (!TextLayoutMetrics.isLineTerminator(codeUnits[index])) {
while (!TextLayoutMetrics.isLineTerminator(_text.codeUnitAt(index))) {
index += 1;
if (index == codeUnits.length) {
if (index == _text.length) {
return index;
}
}

return index < codeUnits.length - 1
&& codeUnits[index] == 0xD
&& codeUnits[index + 1] == 0xA
return index < _text.length - 1
&& _text.codeUnitAt(index) == 0x0D
&& _text.codeUnitAt(index + 1) == 0x0A
? index + 2
: index + 1;
}
Expand Down
8 changes: 4 additions & 4 deletions packages/flutter/lib/src/services/text_layout_metrics.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ abstract class TextLayoutMetrics {
/// (https://www.unicode.org/standard/reports/tr13/tr13-5.html).
static bool isLineTerminator(int codeUnit) {
switch (codeUnit) {
case 0xA: // line feed
case 0xB: // vertical feed
case 0xC: // form feed
case 0xD: // carriage return
case 0x0A: // line feed
case 0x0B: // vertical feed
case 0x0C: // form feed
case 0x0D: // carriage return
case 0x85: // new line
case 0x2028: // line separator
case 0x2029: // paragraph separator
Expand Down

0 comments on commit 5dbd281

Please sign in to comment.