Chromium Code Reviews| Index: sdk/lib/core/errors.dart |
| diff --git a/sdk/lib/core/errors.dart b/sdk/lib/core/errors.dart |
| index f61cdfe07f815bcdc596f8851d782cd77d486edc..1fb0b3546b1d51b29b0aaff434cef7caa9b3698f 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; |
| + 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 |
|
Søren Gjesse
2013/08/16 11:24:57
Align : with ?
|
| + : LOWERCASE_A - 10 + codeUnit); |
| + } |
| + } else if (codeUnit == BACKSLASH) { |
| + buffer.write(r"\\"); |
| + } else if (codeUnit == DOUBLE_QUOTE) { |
|
Søren Gjesse
2013/08/16 11:24:57
Why is it that we want to escape double quote?
Lasse Reichstein Nielsen
2013/08/16 11:27:41
Readability - you can read the string and see wher
|
| + buffer.write(r'\"'); |
| + } else { |
| + buffer.writeCharCode(codeUnit); |
| + } |
| + } |
| + buffer.write('"'); |
| + return buffer.toString(); |
| } |
| return _objectToString(object); |
| } |