OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library utils; | 5 library utils; |
6 | 6 |
7 import 'dart:async'; | 7 import 'dart:async'; |
8 import 'dart:math'; | 8 import 'dart:math'; |
9 | 9 |
10 class Utils { | 10 class Utils { |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
150 return x.toStringAsFixed(2); | 150 return x.toStringAsFixed(2); |
151 } | 151 } |
152 | 152 |
153 static String formatDurationInSeconds(Duration x) => | 153 static String formatDurationInSeconds(Duration x) => |
154 formatSeconds(x.inMicroseconds / Duration.MICROSECONDS_PER_SECOND); | 154 formatSeconds(x.inMicroseconds / Duration.MICROSECONDS_PER_SECOND); |
155 | 155 |
156 static String formatDurationInMilliseconds(Duration x) => | 156 static String formatDurationInMilliseconds(Duration x) => |
157 formatMillis(x.inMicroseconds / Duration.MICROSECONDS_PER_MILLISECOND); | 157 formatMillis(x.inMicroseconds / Duration.MICROSECONDS_PER_MILLISECOND); |
158 | 158 |
159 static bool runningInJavaScript() => identical(1.0, 1); | 159 static bool runningInJavaScript() => identical(1.0, 1); |
| 160 |
| 161 static formatStringAsLiteral(String value, [bool wasTruncated=false]) { |
| 162 var result = new List(); |
| 163 result.add("'".codeUnitAt(0)); |
| 164 for (int codeUnit in value.codeUnits) { |
| 165 if (codeUnit == '\n'.codeUnitAt(0)) result.addAll('\\n'.codeUnits); |
| 166 else if (codeUnit == '\r'.codeUnitAt(0)) result.addAll('\\r'.codeUnits); |
| 167 else if (codeUnit == '\f'.codeUnitAt(0)) result.addAll('\\f'.codeUnits); |
| 168 else if (codeUnit == '\b'.codeUnitAt(0)) result.addAll('\\b'.codeUnits); |
| 169 else if (codeUnit == '\t'.codeUnitAt(0)) result.addAll('\\t'.codeUnits); |
| 170 else if (codeUnit == '\v'.codeUnitAt(0)) result.addAll('\\v'.codeUnits); |
| 171 else if (codeUnit == '\$'.codeUnitAt(0)) result.addAll('\\\$'.codeUnits); |
| 172 else if (codeUnit == '\\'.codeUnitAt(0)) result.addAll('\\\\'.codeUnits); |
| 173 else if (codeUnit == "'".codeUnitAt(0)) result.addAll("'".codeUnits); |
| 174 else if (codeUnit < 32) { |
| 175 var escapeSequence = "\\u" + codeUnit.toRadixString(16).padLeft(4, "0")
; |
| 176 result.addAll(escapeSequence.codeUnits); |
| 177 } else result.add(codeUnit); |
| 178 } |
| 179 if (wasTruncated) { |
| 180 result.addAll("...".codeUnits); |
| 181 } else { |
| 182 result.add("'".codeUnitAt(0)); |
| 183 } |
| 184 return new String.fromCharCodes(result); |
| 185 } |
160 } | 186 } |
161 | 187 |
162 /// A [Task] that can be scheduled on the Dart event queue. | 188 /// A [Task] that can be scheduled on the Dart event queue. |
163 class Task { | 189 class Task { |
164 Timer _timer; | 190 Timer _timer; |
165 final Function callback; | 191 final Function callback; |
166 | 192 |
167 Task(this.callback); | 193 Task(this.callback); |
168 | 194 |
169 /// Queue [this] to run on the next Dart event queue pump. Does nothing | 195 /// Queue [this] to run on the next Dart event queue pump. Does nothing |
170 /// if [this] is already queued. | 196 /// if [this] is already queued. |
171 queue() { | 197 queue() { |
172 if (_timer != null) { | 198 if (_timer != null) { |
173 // Already scheduled. | 199 // Already scheduled. |
174 return; | 200 return; |
175 } | 201 } |
176 _timer = new Timer(Duration.ZERO, () { | 202 _timer = new Timer(Duration.ZERO, () { |
177 _timer = null; | 203 _timer = null; |
178 callback(); | 204 callback(); |
179 }); | 205 }); |
180 } | 206 } |
181 } | 207 } |
OLD | NEW |