From 39643a8c9b11e797c415ba5400d1d251ea8e8b4d Mon Sep 17 00:00:00 2001 From: LongCatIsLooong <31859944+LongCatIsLooong@users.noreply.github.com> Date: Fri, 26 Jul 2024 14:36:04 -0700 Subject: [PATCH] Update text_painter.dart (#152398) Reuse `RegExp`s in `TextPainter`. --- packages/flutter/lib/src/painting/text_painter.dart | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/flutter/lib/src/painting/text_painter.dart b/packages/flutter/lib/src/painting/text_painter.dart index 61d93d7b94..9fbeeefe4f 100644 --- a/packages/flutter/lib/src/painting/text_painter.dart +++ b/packages/flutter/lib/src/painting/text_painter.dart @@ -219,6 +219,7 @@ class WordBoundary extends TextBoundary { }; } + static final RegExp _regExpSpaceSeparatorOrPunctuaion = RegExp(r'[\p{Space_Separator}\p{Punctuation}]', unicode: true); bool _skipSpacesAndPunctuations(int offset, bool forward) { // Use code point since some punctuations are supplementary characters. // "inner" here refers to the code unit that's before the break in the @@ -235,7 +236,7 @@ class WordBoundary extends TextBoundary { final bool hardBreakRulesApply = innerCodePoint == null || outerCodeUnit == null // WB3a & WB3b: always break before and after newlines. || _isNewline(innerCodePoint) || _isNewline(outerCodeUnit); - return hardBreakRulesApply || !RegExp(r'[\p{Space_Separator}\p{Punctuation}]', unicode: true).hasMatch(String.fromCharCode(innerCodePoint)); + return hardBreakRulesApply || !_regExpSpaceSeparatorOrPunctuaion.hasMatch(String.fromCharCode(innerCodePoint)); } /// Returns a [TextBoundary] suitable for handling keyboard navigation @@ -332,6 +333,7 @@ class _TextLayout { }; } + static final RegExp _regExpSpaceSeparators = RegExp(r'\p{Space_Separator}', unicode: true); /// The line caret metrics representing the end of text location. /// /// This is usually used when the caret is placed at the end of the text @@ -364,7 +366,7 @@ class _TextLayout { 0x00A0 || // no-break space 0x2007 || // figure space 0x202F => false, // narrow no-break space - _ => RegExp(r'\p{Space_Separator}', unicode: true).hasMatch(lastCodeUnit), + _ => _regExpSpaceSeparators.hasMatch(lastCodeUnit), }; final double baseline = lineMetrics.baseline;