From 5e944abe55945cab53b045453e835a610aa8d1d4 Mon Sep 17 00:00:00 2001 From: Lukas Renggli Date: Tue, 31 Dec 2024 17:35:54 +0100 Subject: [PATCH] For character predicates use `String.fromCharCode` instead of extracting the matched range from the source; it brings a small performance benefit. --- lib/src/parser/predicate/single_character.dart | 8 +++++--- lib/src/parser/predicate/unicode_character.dart | 3 +-- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/src/parser/predicate/single_character.dart b/lib/src/parser/predicate/single_character.dart index 0bb68cce..9c4fac90 100644 --- a/lib/src/parser/predicate/single_character.dart +++ b/lib/src/parser/predicate/single_character.dart @@ -18,9 +18,11 @@ class SingleCharacterParser extends CharacterParser { Result parseOn(Context context) { final buffer = context.buffer; final position = context.position; - if (position < buffer.length && - predicate.test(buffer.codeUnitAt(position))) { - return context.success(buffer[position], position + 1); + if (position < buffer.length) { + final codeUnit = buffer.codeUnitAt(position); + if (predicate.test(codeUnit)) { + return context.success(String.fromCharCode(codeUnit), position + 1); + } } return context.failure(message); } diff --git a/lib/src/parser/predicate/unicode_character.dart b/lib/src/parser/predicate/unicode_character.dart index 31d76db7..0aa335e8 100644 --- a/lib/src/parser/predicate/unicode_character.dart +++ b/lib/src/parser/predicate/unicode_character.dart @@ -28,8 +28,7 @@ class UnicodeCharacterParser extends CharacterParser { } } if (predicate.test(codeUnit)) { - return context.success( - buffer.substring(position, nextPosition), nextPosition); + return context.success(String.fromCharCode(codeUnit), nextPosition); } } return context.failure(message);