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

Side by Side Diff: lib/src/util.dart

Issue 891463004: Correctly match and print strings with escaped values (Closed) Base URL: https://github.com/dart-lang/matcher.git@master
Patch Set: nits Created 5 years, 10 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 unified diff | Download patch
« no previous file with comments | « lib/src/pretty_print.dart ('k') | test/escape_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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; 5 library matcher.util;
6 6
7 import 'core_matchers.dart'; 7 import 'core_matchers.dart';
8 import 'interfaces.dart'; 8 import 'interfaces.dart';
9 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
10 /// Useful utility for nesting match states. 24 /// Useful utility for nesting match states.
11 void addStateInfo(Map matchState, Map values) { 25 void addStateInfo(Map matchState, Map values) {
12 var innerState = new Map.from(matchState); 26 var innerState = new Map.from(matchState);
13 matchState.clear(); 27 matchState.clear();
14 matchState['state'] = innerState; 28 matchState['state'] = innerState;
15 matchState.addAll(values); 29 matchState.addAll(values);
16 } 30 }
17 31
18 /// Takes an argument and returns an equivalent [Matcher]. 32 /// Takes an argument and returns an equivalent [Matcher].
19 /// 33 ///
20 /// If the argument is already a matcher this does nothing, 34 /// If the argument is already a matcher this does nothing,
21 /// else if the argument is a function, it generates a predicate 35 /// else if the argument is a function, it generates a predicate
22 /// function matcher, else it generates an equals matcher. 36 /// function matcher, else it generates an equals matcher.
23 Matcher wrapMatcher(x) { 37 Matcher wrapMatcher(x) {
24 if (x is Matcher) { 38 if (x is Matcher) {
25 return x; 39 return x;
26 } else if (x is Function) { 40 } else if (x is Function) {
27 return predicate(x); 41 return predicate(x);
28 } else { 42 } else {
29 return equals(x); 43 return equals(x);
30 } 44 }
31 } 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 }
OLDNEW
« no previous file with comments | « lib/src/pretty_print.dart ('k') | test/escape_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698