Skip to content

Commit

Permalink
For character predicates use String.fromCharCode instead of extract…
Browse files Browse the repository at this point in the history
…ing the matched range from the source; it brings a small performance benefit.
  • Loading branch information
renggli committed Dec 31, 2024
1 parent aef231f commit 5e944ab
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
8 changes: 5 additions & 3 deletions lib/src/parser/predicate/single_character.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ class SingleCharacterParser extends CharacterParser {
Result<String> 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);
}
Expand Down
3 changes: 1 addition & 2 deletions lib/src/parser/predicate/unicode_character.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 5e944ab

Please sign in to comment.