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

Side by Side Diff: third_party/dart-packages/matcher/matcher/src/util.dart

Issue 1063233004: Teach dart_package to understand the packages/ subdirectory (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Rebase Created 5 years, 8 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
OLDNEW
(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 }
OLDNEW
« no previous file with comments | « third_party/dart-packages/matcher/matcher/src/throws_matchers.dart ('k') | third_party/dart-packages/path/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698