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

Unified Diff: lib/src/escape.dart

Issue 891463004: Correctly match and print strings with escaped values (Closed) Base URL: https://github.com/dart-lang/matcher.git@master
Patch Set: cleanup Created 5 years, 11 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 side-by-side diff with in-line comments
Download patch
Index: lib/src/escape.dart
diff --git a/lib/src/escape.dart b/lib/src/escape.dart
new file mode 100644
index 0000000000000000000000000000000000000000..eda1047d767c7f1ddd0de4cc776f6358f774e4dc
--- /dev/null
+++ b/lib/src/escape.dart
@@ -0,0 +1,30 @@
+// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+
+library matcher.escape;
+
+const _escapMap = const {
nweiz 2015/02/04 01:34:45 "escapMap" -> "escapeMap"
kevmoo 2015/02/04 05:28:21 Done.
+ '\n': r'\n',
+ '\r': r'\r',
+ '\f': r'\f',
+ '\b': r'\b',
+ '\t': r'\t',
+ '\v': r'\v',
+};
+
+final _escapeStr = "[" + _escapMap.keys.map(_getHexLiteral).join() + "]";
nweiz 2015/02/04 01:34:45 Just inline this in the regexp definition.
kevmoo 2015/02/04 05:28:21 Done.
+
+final _escapeRegExp = new RegExp(_escapeStr);
+
+String escape(String s) {
nweiz 2015/02/04 01:34:45 Put this in utils. Also, document everything in he
kevmoo 2015/02/04 05:28:21 Done.
kevmoo 2015/02/04 05:28:21 Done.
+ s = s.replaceAll('\\', r'\\');
+ return s.replaceAllMapped(_escapeRegExp, (m) {
+ return _escapMap[m.input];
+ });
+}
+
+String _getHexLiteral(String input) {
+ int rune = input.runes.single;
+ return r'\x' + rune.toRadixString(16).padLeft(2, '0');
+}

Powered by Google App Engine
This is Rietveld 408576698