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

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: 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 '\v': r'\v',
kevmoo 2015/03/04 23:07:29 double entry?
nweiz 2015/03/05 00:50:11 Done.
19 '\x7F': r'\x7F',
kevmoo 2015/03/04 23:07:29 Add a comment for which character this is?
nweiz 2015/03/05 00:50:11 Done.
18 }; 20 };
19 21
20 /// A [RegExp] that matches whitespace characters that should be escaped. 22 /// A [RegExp] that matches whitespace characters that should be escaped.
21 final _escapeRegExp = 23 final _escapeRegExp = new RegExp(
22 new RegExp("[${_escapeMap.keys.map(_getHexLiteral).join()}]"); 24 "[\\x00-\\x07\\x0E-\\x1F${_escapeMap.keys.map(_getHexLiteral).join()}]");
23 25
24 /// Useful utility for nesting match states. 26 /// Useful utility for nesting match states.
25 void addStateInfo(Map matchState, Map values) { 27 void addStateInfo(Map matchState, Map values) {
26 var innerState = new Map.from(matchState); 28 var innerState = new Map.from(matchState);
27 matchState.clear(); 29 matchState.clear();
28 matchState['state'] = innerState; 30 matchState['state'] = innerState;
29 matchState.addAll(values); 31 matchState.addAll(values);
30 } 32 }
31 33
32 /// Takes an argument and returns an equivalent [Matcher]. 34 /// Takes an argument and returns an equivalent [Matcher].
(...skipping 11 matching lines...) Expand all
44 } 46 }
45 } 47 }
46 48
47 /// Returns [str] with all whitespace characters represented as their escape 49 /// Returns [str] with all whitespace characters represented as their escape
48 /// sequences. 50 /// sequences.
49 /// 51 ///
50 /// Backslash characters are escaped as `\\` 52 /// Backslash characters are escaped as `\\`
51 String escape(String str) { 53 String escape(String str) {
52 str = str.replaceAll('\\', r'\\'); 54 str = str.replaceAll('\\', r'\\');
53 return str.replaceAllMapped(_escapeRegExp, (match) { 55 return str.replaceAllMapped(_escapeRegExp, (match) {
54 return _escapeMap[match[0]]; 56 var mapped = _escapeMap[match[0]];
57 if (mapped != null) return mapped;
58 return _getHexLiteral(match[0]);
55 }); 59 });
56 } 60 }
57 61
58 /// Given single-character string, return the hex-escaped equivalent. 62 /// Given single-character string, return the hex-escaped equivalent.
59 String _getHexLiteral(String input) { 63 String _getHexLiteral(String input) {
60 int rune = input.runes.single; 64 int rune = input.runes.single;
61 return r'\x' + rune.toRadixString(16).padLeft(2, '0'); 65 return r'\x' + rune.toRadixString(16).toUpperCase().padLeft(2, '0');
62 } 66 }
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