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

Side by Side Diff: sdk/lib/core/errors.dart

Issue 718053003: Reduce foot-print of Error.safeToString on strings. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 1 month 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 /** 7 /**
8 * Error objects thrown in the case of a program failure. 8 * Error objects thrown in the case of a program failure.
9 * 9 *
10 * An `Error` object represents a program failure that the programmer 10 * An `Error` object represents a program failure that the programmer
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 * Safely convert a value to a [String] description. 72 * Safely convert a value to a [String] description.
73 * 73 *
74 * The conversion is guaranteed to not throw, so it won't use the object's 74 * The conversion is guaranteed to not throw, so it won't use the object's
75 * toString method. 75 * toString method.
76 */ 76 */
77 static String safeToString(Object object) { 77 static String safeToString(Object object) {
78 if (object is num || object is bool || null == object) { 78 if (object is num || object is bool || null == object) {
79 return object.toString(); 79 return object.toString();
80 } 80 }
81 if (object is String) { 81 if (object is String) {
82 String string = object; 82 return _stringToSafeString(object);
83 StringBuffer buffer = new StringBuffer('"');
84 const int TAB = 0x09;
85 const int NEWLINE = 0x0a;
86 const int CARRIGE_RETURN = 0x0d;
87 const int BACKSLASH = 0x5c;
88 const int DOUBLE_QUOTE = 0x22;
89 const int DIGIT_ZERO = 0x30;
90 const int LOWERCASE_A = 0x61;
91 const int MAX_CONTROL = 0x1f;
92 for (int i = 0; i < string.length; i++) {
93 int codeUnit = string.codeUnitAt(i);
94 if (codeUnit <= MAX_CONTROL) {
95 if (codeUnit == NEWLINE) {
96 buffer.write(r"\n");
97 } else if (codeUnit == CARRIGE_RETURN) {
98 buffer.write(r"\r");
99 } else if (codeUnit == TAB) {
100 buffer.write(r"\t");
101 } else {
102 buffer.write(r"\x");
103 // Convert code in range 0x00 .. 0x1f to hex a two-digit hex string.
104 if (codeUnit < 0x10) {
105 buffer.write("0");
106 } else {
107 buffer.write("1");
108 codeUnit -= 0x10;
109 }
110 // Single digit to hex.
111 buffer.writeCharCode(codeUnit < 10 ? DIGIT_ZERO + codeUnit
112 : LOWERCASE_A - 10 + codeUnit);
113 }
114 } else if (codeUnit == BACKSLASH) {
115 buffer.write(r"\\");
116 } else if (codeUnit == DOUBLE_QUOTE) {
117 buffer.write(r'\"');
118 } else {
119 buffer.writeCharCode(codeUnit);
120 }
121 }
122 buffer.write('"');
123 return buffer.toString();
124 } 83 }
125 return _objectToString(object); 84 return _objectToString(object);
126 } 85 }
127 86
87 /** Convert string to a valid string literal with no control characters. */
88 external static String _stringToSafeString(String string);
89
128 external static String _objectToString(Object object); 90 external static String _objectToString(Object object);
129 91
130 external StackTrace get stackTrace; 92 external StackTrace get stackTrace;
131 } 93 }
132 94
133 /** 95 /**
134 * Error thrown by the runtime system when an assert statement fails. 96 * Error thrown by the runtime system when an assert statement fails.
135 */ 97 */
136 class AssertionError extends Error { 98 class AssertionError extends Error {
137 } 99 }
(...skipping 361 matching lines...) Expand 10 before | Expand all | Expand 10 after
499 * the first time it is read. If evaluating the initializer expression causes 461 * the first time it is read. If evaluating the initializer expression causes
500 * another read of the variable, this error is thrown. 462 * another read of the variable, this error is thrown.
501 */ 463 */
502 class CyclicInitializationError extends Error { 464 class CyclicInitializationError extends Error {
503 final String variableName; 465 final String variableName;
504 CyclicInitializationError([this.variableName]); 466 CyclicInitializationError([this.variableName]);
505 String toString() => variableName == null 467 String toString() => variableName == null
506 ? "Reading static variable during its initialization" 468 ? "Reading static variable during its initialization"
507 : "Reading static variable '$variableName' during its initialization"; 469 : "Reading static variable '$variableName' during its initialization";
508 } 470 }
OLDNEW
« no previous file with comments | « sdk/lib/_internal/compiler/js_lib/js_helper.dart ('k') | tests/corelib/safe_to_string_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698