Index: pkg/shelf/lib/src/string_scanner.dart |
diff --git a/pkg/shelf/lib/src/string_scanner.dart b/pkg/shelf/lib/src/string_scanner.dart |
new file mode 100644 |
index 0000000000000000000000000000000000000000..834c2d433c53a7289510caa9647b5f068e2827c6 |
--- /dev/null |
+++ b/pkg/shelf/lib/src/string_scanner.dart |
@@ -0,0 +1,69 @@ |
+// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
+// 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. |
+ |
+library shelf.string_scanner; |
+ |
+/// A class that scans through a string using [Pattern]s. |
+class StringScanner { |
+ /// The string being scanned through. |
+ final String string; |
+ |
+ /// The current position of the scanner in the string, in characters. |
+ int get position => _position; |
+ set position(int position) { |
+ if (position < 0 || position > string.length) { |
+ throw new ArgumentError("Invalid position $position"); |
+ } |
+ |
+ _position = position; |
+ } |
+ int _position = 0; |
+ |
+ /// The data about the previous match made by the scanner. |
+ /// |
+ /// If the last match failed, this will be `null`. |
+ Match get lastMatch => _lastMatch; |
+ Match _lastMatch; |
+ |
+ /// The portion of the string that hasn't yet been scanned. |
+ String get rest => string.substring(position); |
+ |
+ /// Whether the scanner has completely consumed [string]. |
+ bool get isDone => position == string.length; |
+ |
+ /// Creates a new [StringScanner] that starts scanning from [position]. |
+ /// |
+ /// [position] defaults to 0, the beginning of the string. |
+ StringScanner(this.string, {int position}) { |
+ if (position != null) this.position = position; |
+ } |
+ |
+ /// If [pattern] matches at the current position of the string, scans forward |
+ /// until the end of the match. |
+ /// |
+ /// Returns whether or not [pattern] matched. |
+ bool scan(Pattern pattern) { |
+ var success = matches(pattern); |
+ if (success) _position = _lastMatch.end; |
+ return success; |
+ } |
+ |
+ /// If [pattern] matches at the current position of the string, scans forward |
+ /// until the end of the match. |
+ /// |
+ /// If [pattern] did not match, throws a [FormatException] with [message]. |
+ void expect(Pattern pattern, String message) { |
+ if (scan(pattern)) return; |
+ throw new FormatException(message); |
+ } |
+ |
+ /// Returns whether or not [pattern] matches at the current position of the |
+ /// string. |
+ /// |
+ /// This doesn't move the scan pointer forward. |
+ bool matches(Pattern pattern) { |
+ _lastMatch = pattern.matchAsPrefix(string, position); |
+ return _lastMatch != null; |
+ } |
+} |