| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 library matcher.util; | |
| 6 | |
| 7 import 'core_matchers.dart'; | |
| 8 import 'interfaces.dart'; | |
| 9 | |
| 10 /// A [Map] between whitespace characters and their escape sequences. | |
| 11 const _escapeMap = const { | |
| 12 '\n': r'\n', | |
| 13 '\r': r'\r', | |
| 14 '\f': r'\f', | |
| 15 '\b': r'\b', | |
| 16 '\t': r'\t', | |
| 17 '\v': r'\v', | |
| 18 }; | |
| 19 | |
| 20 /// A [RegExp] that matches whitespace characters that should be escaped. | |
| 21 final _escapeRegExp = | |
| 22 new RegExp("[${_escapeMap.keys.map(_getHexLiteral).join()}]"); | |
| 23 | |
| 24 /// Useful utility for nesting match states. | |
| 25 void addStateInfo(Map matchState, Map values) { | |
| 26 var innerState = new Map.from(matchState); | |
| 27 matchState.clear(); | |
| 28 matchState['state'] = innerState; | |
| 29 matchState.addAll(values); | |
| 30 } | |
| 31 | |
| 32 /// Takes an argument and returns an equivalent [Matcher]. | |
| 33 /// | |
| 34 /// If the argument is already a matcher this does nothing, | |
| 35 /// else if the argument is a function, it generates a predicate | |
| 36 /// function matcher, else it generates an equals matcher. | |
| 37 Matcher wrapMatcher(x) { | |
| 38 if (x is Matcher) { | |
| 39 return x; | |
| 40 } else if (x is Function) { | |
| 41 return predicate(x); | |
| 42 } else { | |
| 43 return equals(x); | |
| 44 } | |
| 45 } | |
| 46 | |
| 47 /// Returns [str] with all whitespace characters represented as their escape | |
| 48 /// sequences. | |
| 49 /// | |
| 50 /// Backslash characters are escaped as `\\` | |
| 51 String escape(String str) { | |
| 52 str = str.replaceAll('\\', r'\\'); | |
| 53 return str.replaceAllMapped(_escapeRegExp, (match) { | |
| 54 return _escapeMap[match[0]]; | |
| 55 }); | |
| 56 } | |
| 57 | |
| 58 /// Given single-character string, return the hex-escaped equivalent. | |
| 59 String _getHexLiteral(String input) { | |
| 60 int rune = input.runes.single; | |
| 61 return r'\x' + rune.toRadixString(16).padLeft(2, '0'); | |
| 62 } | |
| OLD | NEW |