From 5dbd2810123ae85b2b1f30a5317178ba7685b3cb Mon Sep 17 00:00:00 2001 From: Renzo Olivares Date: Fri, 10 Feb 2023 09:31:07 -0800 Subject: [PATCH] Use String.codeUnitAt instead of String.codeUnits[] in ParagraphBoundary (#120234) * paragraph-boundary-opt * address comments * address comments --------- Co-authored-by: Renzo Olivares --- .../lib/src/services/text_boundary.dart | 18 ++++++++---------- .../lib/src/services/text_layout_metrics.dart | 8 ++++---- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/packages/flutter/lib/src/services/text_boundary.dart b/packages/flutter/lib/src/services/text_boundary.dart index efbea24505..4e2414ef14 100644 --- a/packages/flutter/lib/src/services/text_boundary.dart +++ b/packages/flutter/lib/src/services/text_boundary.dart @@ -150,17 +150,16 @@ class ParagraphBoundary extends TextBoundary { return 0; } - final List 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; @@ -183,19 +182,18 @@ class ParagraphBoundary extends TextBoundary { return 0; } - final List 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; } diff --git a/packages/flutter/lib/src/services/text_layout_metrics.dart b/packages/flutter/lib/src/services/text_layout_metrics.dart index f4ee0a7b9e..e025ea1197 100644 --- a/packages/flutter/lib/src/services/text_layout_metrics.dart +++ b/packages/flutter/lib/src/services/text_layout_metrics.dart @@ -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