Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(55)

Unified Diff: lib/src/string_scanner.dart

Issue 2041813002: Add StringScanner.scanChar() and .expectChar(). (Closed) Base URL: git@github.com:dart-lang/string_scanner@master
Patch Set: Code review changes Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/src/line_scanner.dart ('k') | pubspec.yaml » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/string_scanner.dart
diff --git a/lib/src/string_scanner.dart b/lib/src/string_scanner.dart
index 775dd5e1cffabfcd7a1b348c89645c09540bb178..8334ccb3e62c92389f5730d55a02176748508221 100644
--- a/lib/src/string_scanner.dart
+++ b/lib/src/string_scanner.dart
@@ -2,6 +2,7 @@
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
+import 'package:charcode/charcode.dart';
import 'package:source_span/source_span.dart';
import 'exception.dart';
@@ -79,6 +80,38 @@ class StringScanner {
return string.codeUnitAt(index);
}
+ /// If the next character in the string is [character], consumes it.
+ ///
+ /// Returns whether or not [character] was consumed.
+ bool scanChar(int character) {
+ if (isDone) return false;
+ if (string.codeUnitAt(_position) != character) return false;
+ _position++;
+ return true;
+ }
+
+ /// If the next character in the string is [character], consumes it.
+ ///
+ /// If [character] could not be consumed, throws a [FormatException]
+ /// describing the position of the failure. [name] is used in this error as
+ /// the expected name of the character being matched; if it's `null`, the
+ /// character itself is used instead.
+ void expectChar(int character, {String name}) {
+ if (scanChar(character)) return;
+
+ if (name == null) {
+ if (character == $backslash) {
+ name = r'"\"';
+ } else if (character == $double_quote) {
+ name = r'"\""';
+ } else {
+ name = '"${new String.fromCharCode(character)}"';
+ }
+ }
+
+ _fail('Expected $name.');
+ }
+
/// If [pattern] matches at the current position of the string, scans forward
/// until the end of the match.
///
« no previous file with comments | « lib/src/line_scanner.dart ('k') | pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698