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

Unified Diff: dart/lib/core/errors.dart

Issue 10943027: Handle backslashes and newlines when escaping strings. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 8 years, 3 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
« no previous file with comments | « dart/lib/compiler/implementation/lib/core_patch.dart ('k') | dart/runtime/lib/errors_patch.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: dart/lib/core/errors.dart
diff --git a/dart/lib/core/errors.dart b/dart/lib/core/errors.dart
index 2c904587e225ccc233eccfeadb2c8597e19c2cf5..597d26a1bfdc97b927c110e1ccd185dd1a3e343a 100644
--- a/dart/lib/core/errors.dart
+++ b/dart/lib/core/errors.dart
@@ -81,5 +81,21 @@ class NoSuchMethodError implements Error {
}
}
- external static String safeToString(Object object);
+ static String safeToString(Object object) {
+ if (object is int || object is double || object is bool || null == object) {
Lasse Reichstein Nielsen 2012/09/19 12:14:53 The first two can be condensed to 'object is num'.
ahe 2012/09/19 15:59:45 No. I'll leave it as an exercise to the reader to
+ return object.toString();
+ }
+ if (object is String) {
+ // TODO(ahe): Remove backslash when http://dartbug.com/4995 is fixed.
+ const backslash = '\\';
+ String escaped = object
+ .replaceAll('$backslash', '$backslash$backslash')
+ .replaceAll('\n', '${backslash}n')
Lasse Reichstein Nielsen 2012/09/19 12:14:53 Ditto for "\r", which is also a "newline" in Dart.
ahe 2012/09/19 15:59:45 Done. Also extended the test.
+ .replaceAll('"', '$backslash"');
Lasse Reichstein Nielsen 2012/09/19 12:14:53 We SO need a computed replace to avoid these seque
+ return '"$escaped"';
+ }
+ return _objectToString(object);
+ }
+
+ external static _objectToString(Object object);
}
« no previous file with comments | « dart/lib/compiler/implementation/lib/core_patch.dart ('k') | dart/runtime/lib/errors_patch.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698