Chromium Code Reviews| Index: lib/src/util.dart |
| diff --git a/lib/src/util.dart b/lib/src/util.dart |
| index 3ecc47c5846d14842aca546698e828441a676497..d38495f561090e34cd7cbe166af3c3c1522579dd 100644 |
| --- a/lib/src/util.dart |
| +++ b/lib/src/util.dart |
| @@ -7,6 +7,20 @@ library matcher.util; |
| import 'core_matchers.dart'; |
| import 'interfaces.dart'; |
| +/// A [Map] between whitespace characters and their escape sequences. |
| +const _escapeMap = const { |
| + '\n': r'\n', |
| + '\r': r'\r', |
| + '\f': r'\f', |
| + '\b': r'\b', |
| + '\t': r'\t', |
| + '\v': r'\v', |
| +}; |
| + |
| +/// A [RegExp] that matches whitespace characters. |
|
nweiz
2015/02/06 00:23:13
"whitespace characters that should be escaped"
kevmoo
2015/02/06 02:58:59
Done.
|
| +final _escapeRegExp = |
| + new RegExp("[${_escapeMap.keys.map(_getHexLiteral).join()}]"); |
| + |
| /// Useful utility for nesting match states. |
| void addStateInfo(Map matchState, Map values) { |
| var innerState = new Map.from(matchState); |
| @@ -29,3 +43,20 @@ Matcher wrapMatcher(x) { |
| return equals(x); |
| } |
| } |
| + |
| +/// Returns [str] with all whitespace characters represented as their escape |
| +/// sequences. |
| +/// |
| +/// Backslash characters are escaped as `\\` |
| +String escape(String str) { |
| + str = str.replaceAll('\\', r'\\'); |
| + return str.replaceAllMapped(_escapeRegExp, (m) { |
|
nweiz
2015/02/06 00:23:13
Don't use single-letter variables.
kevmoo
2015/02/06 02:58:59
Done.
|
| + return _escapeMap[m[0]]; |
| + }); |
| +} |
| + |
| +/// Given single-character string, return the hex-escaped equivalent. |
| +String _getHexLiteral(String input) { |
| + int rune = input.runes.single; |
| + return r'\x' + rune.toRadixString(16).padLeft(2, '0'); |
| +} |