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

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

Issue 23116007: Make Error.safeToString convert control codes in strings to escapes. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Addressed review comments. Created 7 years, 4 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 | « no previous file | tests/corelib/safe_to_string_test.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sdk/lib/core/errors.dart
diff --git a/sdk/lib/core/errors.dart b/sdk/lib/core/errors.dart
index f61cdfe07f815bcdc596f8851d782cd77d486edc..1ce7193a7d8a503baa6bcdcd6d90158a2df778da 100644
--- a/sdk/lib/core/errors.dart
+++ b/sdk/lib/core/errors.dart
@@ -16,15 +16,48 @@ class Error {
return object.toString();
}
if (object is String) {
- // TODO(ahe): Remove backslash when http://dartbug.com/4995 is fixed.
String string = object;
- const backslash = '\\';
- String escaped = string
- .replaceAll('$backslash', '$backslash$backslash')
- .replaceAll('\n', '${backslash}n')
- .replaceAll('\r', '${backslash}r')
- .replaceAll('"', '$backslash"');
- return '"$escaped"';
+ StringBuffer buffer = new StringBuffer('"');
+ const int TAB = 0x09;
+ const int NEWLINE = 0x0a;
+ const int CARRIGE_RETURN = 0x0d;
srdjan 2013/12/03 00:18:36 Any reason why CARRIAGE_RETURN is misspelled?
Lasse Reichstein Nielsen 2013/12/03 08:50:28 It's ... shorter? It's spelled like it's pronounce
+ const int BACKSLASH = 0x5c;
+ const int DOUBLE_QUOTE = 0x22;
+ const int DIGIT_ZERO = 0x30;
+ const int LOWERCASE_A = 0x61;
+ const int MAX_CONTROL = 0x1f;
+ for (int i = 0; i < string.length; i++) {
+ int codeUnit = string.codeUnitAt(i);
+ if (codeUnit <= MAX_CONTROL) {
+ if (codeUnit == NEWLINE) {
+ buffer.write(r"\n");
+ } else if (codeUnit == CARRIGE_RETURN) {
+ buffer.write(r"\r");
+ } else if (codeUnit == TAB) {
+ buffer.write(r"\t");
+ } else {
+ buffer.write(r"\x");
+ // Convert code in range 0x00 .. 0x1f to hex a two-digit hex string.
+ if (codeUnit < 0x10) {
+ buffer.write("0");
+ } else {
+ buffer.write("1");
+ codeUnit -= 0x10;
+ }
+ // Single digit to hex.
+ buffer.writeCharCode(codeUnit < 10 ? DIGIT_ZERO + codeUnit
+ : LOWERCASE_A - 10 + codeUnit);
+ }
+ } else if (codeUnit == BACKSLASH) {
+ buffer.write(r"\\");
+ } else if (codeUnit == DOUBLE_QUOTE) {
+ buffer.write(r'\"');
+ } else {
+ buffer.writeCharCode(codeUnit);
+ }
+ }
+ buffer.write('"');
+ return buffer.toString();
}
return _objectToString(object);
}
« no previous file with comments | « no previous file | tests/corelib/safe_to_string_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698