Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of dart.core; | 5 part of dart.core; |
| 6 | 6 |
| 7 class Error { | 7 class Error { |
| 8 /** | 8 /** |
| 9 * Safely convert a value to a [String] description. | 9 * Safely convert a value to a [String] description. |
| 10 * | 10 * |
| 11 * The conversion is guaranteed to not throw, so it won't use the object's | 11 * The conversion is guaranteed to not throw, so it won't use the object's |
| 12 * toString method. | 12 * toString method. |
| 13 */ | 13 */ |
| 14 static String safeToString(Object object) { | 14 static String safeToString(Object object) { |
| 15 if (object is int || object is double || object is bool || null == object) { | 15 if (object is int || object is double || object is bool || null == object) { |
| 16 return object.toString(); | 16 return object.toString(); |
| 17 } | 17 } |
| 18 if (object is String) { | 18 if (object is String) { |
| 19 // TODO(ahe): Remove backslash when http://dartbug.com/4995 is fixed. | |
| 20 String string = object; | 19 String string = object; |
| 21 const backslash = '\\'; | 20 StringBuffer buffer = new StringBuffer('"'); |
| 22 String escaped = string | 21 const int TAB = 0x09; |
| 23 .replaceAll('$backslash', '$backslash$backslash') | 22 const int NEWLINE = 0x0a; |
| 24 .replaceAll('\n', '${backslash}n') | 23 const int CARRIGE_RETURN = 0x0d; |
| 25 .replaceAll('\r', '${backslash}r') | 24 const int BACKSLASH = 0x5c; |
| 26 .replaceAll('"', '$backslash"'); | 25 const int DOUBLE_QUOTE = 0x22; |
| 27 return '"$escaped"'; | 26 const int DIGIT_ZERO = 0x30; |
| 27 const int LOWERCASE_A = 0x61; | |
| 28 const int MAX_CONTROL = 0x1f; | |
| 29 for (int i = 0; i < string.length; i++) { | |
| 30 int codeUnit = string.codeUnitAt(i); | |
| 31 if (codeUnit <= MAX_CONTROL) { | |
| 32 if (codeUnit == NEWLINE) { | |
| 33 buffer.write(r"\n"); | |
| 34 } else if (codeUnit == CARRIGE_RETURN) { | |
| 35 buffer.write(r"\r"); | |
| 36 } else if (codeUnit == TAB) { | |
| 37 buffer.write(r"\t"); | |
| 38 } else { | |
| 39 buffer.write(r"\x"); | |
| 40 // Convert code in range 0x00 .. 0x1f to hex a two-digit hex string. | |
| 41 if (codeUnit < 0x10) { | |
| 42 buffer.write("0"); | |
| 43 } else { | |
| 44 buffer.write("1"); | |
| 45 codeUnit -= 0x10; | |
| 46 } | |
| 47 // Single digit to hex. | |
| 48 buffer.writeCharCode(codeUnit < 10 ? DIGIT_ZERO + codeUnit | |
|
Søren Gjesse
2013/08/16 11:24:57
Align : with ?
| |
| 49 : LOWERCASE_A - 10 + codeUnit); | |
| 50 } | |
| 51 } else if (codeUnit == BACKSLASH) { | |
| 52 buffer.write(r"\\"); | |
| 53 } 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
| |
| 54 buffer.write(r'\"'); | |
| 55 } else { | |
| 56 buffer.writeCharCode(codeUnit); | |
| 57 } | |
| 58 } | |
| 59 buffer.write('"'); | |
| 60 return buffer.toString(); | |
| 28 } | 61 } |
| 29 return _objectToString(object); | 62 return _objectToString(object); |
| 30 } | 63 } |
| 31 | 64 |
| 32 external static String _objectToString(Object object); | 65 external static String _objectToString(Object object); |
| 33 | 66 |
| 34 external StackTrace get stackTrace; | 67 external StackTrace get stackTrace; |
| 35 } | 68 } |
| 36 | 69 |
| 37 /** | 70 /** |
| (...skipping 208 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 246 * the first time it is read. If evaluating the initializer expression causes | 279 * the first time it is read. If evaluating the initializer expression causes |
| 247 * another read of the variable, this error is thrown. | 280 * another read of the variable, this error is thrown. |
| 248 */ | 281 */ |
| 249 class CyclicInitializationError extends Error { | 282 class CyclicInitializationError extends Error { |
| 250 final String variableName; | 283 final String variableName; |
| 251 CyclicInitializationError([this.variableName]); | 284 CyclicInitializationError([this.variableName]); |
| 252 String toString() => variableName == null | 285 String toString() => variableName == null |
| 253 ? "Reading static variable during its initialization" | 286 ? "Reading static variable during its initialization" |
| 254 : "Reading static variable '$variableName' during its initialization"; | 287 : "Reading static variable '$variableName' during its initialization"; |
| 255 } | 288 } |
| OLD | NEW |