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

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

Issue 984463002: Improve the formatting of strings that contain unprintable ASCII characters. (Closed) Base URL: git@github.com:dart-lang/matcher@master
Patch Set: Code review changes Created 5 years, 9 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 | « CHANGELOG.md ('k') | pubspec.yaml » ('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. 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 }; 19 };
19 20
20 /// A [RegExp] that matches whitespace characters that should be escaped. 21 /// A [RegExp] that matches whitespace characters that should be escaped.
21 final _escapeRegExp = 22 final _escapeRegExp = new RegExp(
22 new RegExp("[${_escapeMap.keys.map(_getHexLiteral).join()}]"); 23 "[\\x00-\\x07\\x0E-\\x1F${_escapeMap.keys.map(_getHexLiteral).join()}]");
23 24
24 /// Useful utility for nesting match states. 25 /// Useful utility for nesting match states.
25 void addStateInfo(Map matchState, Map values) { 26 void addStateInfo(Map matchState, Map values) {
26 var innerState = new Map.from(matchState); 27 var innerState = new Map.from(matchState);
27 matchState.clear(); 28 matchState.clear();
28 matchState['state'] = innerState; 29 matchState['state'] = innerState;
29 matchState.addAll(values); 30 matchState.addAll(values);
30 } 31 }
31 32
32 /// Takes an argument and returns an equivalent [Matcher]. 33 /// Takes an argument and returns an equivalent [Matcher].
(...skipping 11 matching lines...) Expand all
44 } 45 }
45 } 46 }
46 47
47 /// Returns [str] with all whitespace characters represented as their escape 48 /// Returns [str] with all whitespace characters represented as their escape
48 /// sequences. 49 /// sequences.
49 /// 50 ///
50 /// Backslash characters are escaped as `\\` 51 /// Backslash characters are escaped as `\\`
51 String escape(String str) { 52 String escape(String str) {
52 str = str.replaceAll('\\', r'\\'); 53 str = str.replaceAll('\\', r'\\');
53 return str.replaceAllMapped(_escapeRegExp, (match) { 54 return str.replaceAllMapped(_escapeRegExp, (match) {
54 return _escapeMap[match[0]]; 55 var mapped = _escapeMap[match[0]];
56 if (mapped != null) return mapped;
57 return _getHexLiteral(match[0]);
55 }); 58 });
56 } 59 }
57 60
58 /// Given single-character string, return the hex-escaped equivalent. 61 /// Given single-character string, return the hex-escaped equivalent.
59 String _getHexLiteral(String input) { 62 String _getHexLiteral(String input) {
60 int rune = input.runes.single; 63 int rune = input.runes.single;
61 return r'\x' + rune.toRadixString(16).padLeft(2, '0'); 64 return r'\x' + rune.toRadixString(16).toUpperCase().padLeft(2, '0');
62 } 65 }
OLDNEW
« no previous file with comments | « CHANGELOG.md ('k') | pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698