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'); |
+} |