| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 library matcher.util; | |
| 6 | |
| 7 import 'core_matchers.dart'; | 5 import 'core_matchers.dart'; |
| 8 import 'interfaces.dart'; | 6 import 'interfaces.dart'; |
| 9 | 7 |
| 8 typedef bool _Predicate(value); |
| 9 |
| 10 /// A [Map] between whitespace characters and their escape sequences. | 10 /// A [Map] between whitespace characters and their escape sequences. |
| 11 const _escapeMap = const { | 11 const _escapeMap = const { |
| 12 '\n': r'\n', | 12 '\n': r'\n', |
| 13 '\r': r'\r', | 13 '\r': r'\r', |
| 14 '\f': r'\f', | 14 '\f': r'\f', |
| 15 '\b': r'\b', | 15 '\b': r'\b', |
| 16 '\t': r'\t', | 16 '\t': r'\t', |
| 17 '\v': r'\v', | 17 '\v': r'\v', |
| 18 '\x7F': r'\x7F', // delete | 18 '\x7F': r'\x7F', // delete |
| 19 }; | 19 }; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 31 } | 31 } |
| 32 | 32 |
| 33 /// Takes an argument and returns an equivalent [Matcher]. | 33 /// Takes an argument and returns an equivalent [Matcher]. |
| 34 /// | 34 /// |
| 35 /// If the argument is already a matcher this does nothing, | 35 /// If the argument is already a matcher this does nothing, |
| 36 /// else if the argument is a function, it generates a predicate | 36 /// else if the argument is a function, it generates a predicate |
| 37 /// function matcher, else it generates an equals matcher. | 37 /// function matcher, else it generates an equals matcher. |
| 38 Matcher wrapMatcher(x) { | 38 Matcher wrapMatcher(x) { |
| 39 if (x is Matcher) { | 39 if (x is Matcher) { |
| 40 return x; | 40 return x; |
| 41 } else if (x is Function) { | 41 } else if (x is _Predicate) { |
| 42 return predicate(x); | 42 return predicate(x); |
| 43 } else { | 43 } else { |
| 44 return equals(x); | 44 return equals(x); |
| 45 } | 45 } |
| 46 } | 46 } |
| 47 | 47 |
| 48 /// Returns [str] with all whitespace characters represented as their escape | 48 /// Returns [str] with all whitespace characters represented as their escape |
| 49 /// sequences. | 49 /// sequences. |
| 50 /// | 50 /// |
| 51 /// Backslash characters are escaped as `\\` | 51 /// Backslash characters are escaped as `\\` |
| 52 String escape(String str) { | 52 String escape(String str) { |
| 53 str = str.replaceAll('\\', r'\\'); | 53 str = str.replaceAll('\\', r'\\'); |
| 54 return str.replaceAllMapped(_escapeRegExp, (match) { | 54 return str.replaceAllMapped(_escapeRegExp, (match) { |
| 55 var mapped = _escapeMap[match[0]]; | 55 var mapped = _escapeMap[match[0]]; |
| 56 if (mapped != null) return mapped; | 56 if (mapped != null) return mapped; |
| 57 return _getHexLiteral(match[0]); | 57 return _getHexLiteral(match[0]); |
| 58 }); | 58 }); |
| 59 } | 59 } |
| 60 | 60 |
| 61 /// Given single-character string, return the hex-escaped equivalent. | 61 /// Given single-character string, return the hex-escaped equivalent. |
| 62 String _getHexLiteral(String input) { | 62 String _getHexLiteral(String input) { |
| 63 int rune = input.runes.single; | 63 int rune = input.runes.single; |
| 64 return r'\x' + rune.toRadixString(16).toUpperCase().padLeft(2, '0'); | 64 return r'\x' + rune.toRadixString(16).toUpperCase().padLeft(2, '0'); |
| 65 } | 65 } |
| OLD | NEW |