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 { |
11 | |
12 static String formatPercentNormalized(double x) { | 11 static String formatPercentNormalized(double x) { |
13 var percent = 100.0 * x; | 12 var percent = 100.0 * x; |
14 return '${percent.toStringAsFixed(2)}%'; | 13 return '${percent.toStringAsFixed(2)}%'; |
15 } | 14 } |
16 | 15 |
17 static String formatPercent(num a, num total) { | 16 static String formatPercent(num a, num total) { |
18 return formatPercentNormalized(a / total); | 17 return formatPercentNormalized(a / total); |
19 } | 18 } |
20 | 19 |
21 static String zeroPad(int value, int pad) { | 20 static String zeroPad(int value, int pad) { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
68 millis = millis % millisPerHour; | 67 millis = millis % millisPerHour; |
69 | 68 |
70 var minutes = millis ~/ millisPerMinute; | 69 var minutes = millis ~/ millisPerMinute; |
71 millis = millis % millisPerMinute; | 70 millis = millis % millisPerMinute; |
72 | 71 |
73 var seconds = millis ~/ millisPerSecond; | 72 var seconds = millis ~/ millisPerSecond; |
74 millis = millis % millisPerSecond; | 73 millis = millis % millisPerSecond; |
75 | 74 |
76 if (hours > 0) { | 75 if (hours > 0) { |
77 return ("${zeroPad(hours,2)}" | 76 return ("${zeroPad(hours,2)}" |
78 ":${zeroPad(minutes,2)}" | 77 ":${zeroPad(minutes,2)}" |
79 ":${zeroPad(seconds,2)}" | 78 ":${zeroPad(seconds,2)}" |
80 ".${zeroPad(millis,3)}"); | 79 ".${zeroPad(millis,3)}"); |
81 } else if (minutes > 0) { | 80 } else if (minutes > 0) { |
82 return ("${zeroPad(minutes,2)}" | 81 return ("${zeroPad(minutes,2)}" |
83 ":${zeroPad(seconds,2)}" | 82 ":${zeroPad(seconds,2)}" |
84 ".${zeroPad(millis,3)}"); | 83 ".${zeroPad(millis,3)}"); |
85 } else { | 84 } else { |
86 return ("${zeroPad(seconds,2)}" | 85 return ("${zeroPad(seconds,2)}" |
87 ".${zeroPad(millis,3)}"); | 86 ".${zeroPad(millis,3)}"); |
88 } | 87 } |
89 } | 88 } |
90 | 89 |
91 static String formatSize(int bytes) { | 90 static String formatSize(int bytes) { |
92 const int digits = 1; | 91 const int digits = 1; |
93 const int bytesPerKB = 1024; | 92 const int bytesPerKB = 1024; |
94 const int bytesPerMB = 1024 * bytesPerKB; | 93 const int bytesPerMB = 1024 * bytesPerKB; |
95 const int bytesPerGB = 1024 * bytesPerMB; | 94 const int bytesPerGB = 1024 * bytesPerMB; |
96 const int bytesPerTB = 1024 * bytesPerGB; | 95 const int bytesPerTB = 1024 * bytesPerGB; |
97 | 96 |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
130 return '${hours}h ${minutes}m ${seconds}s'; | 129 return '${hours}h ${minutes}m ${seconds}s'; |
131 } | 130 } |
132 if (minutes != 0) { | 131 if (minutes != 0) { |
133 return '${minutes}m ${seconds}s'; | 132 return '${minutes}m ${seconds}s'; |
134 } | 133 } |
135 return '${seconds}s'; | 134 return '${seconds}s'; |
136 } | 135 } |
137 | 136 |
138 static String formatDateTime(DateTime now) { | 137 static String formatDateTime(DateTime now) { |
139 return '${now.year}-${now.month}-${now.day} ' | 138 return '${now.year}-${now.month}-${now.day} ' |
140 '${now.hour.toString().padLeft(2)}:' | 139 '${now.hour.toString().padLeft(2)}:' |
141 '${now.minute.toString().padLeft(2)}:' | 140 '${now.minute.toString().padLeft(2)}:' |
142 '${now.second.toString().padLeft(2)}'; | 141 '${now.second.toString().padLeft(2)}'; |
143 } | 142 } |
144 | 143 |
145 static String formatSeconds(double x) { | 144 static String formatSeconds(double x) { |
146 return x.toStringAsFixed(2); | 145 return x.toStringAsFixed(2); |
147 } | 146 } |
148 | 147 |
149 static String formatMillis(double x) { | 148 static String formatMillis(double x) { |
150 return x.toStringAsFixed(2); | 149 return x.toStringAsFixed(2); |
151 } | 150 } |
152 | 151 |
153 static String formatDurationInSeconds(Duration x) => | 152 static String formatDurationInSeconds(Duration x) => |
154 formatSeconds(x.inMicroseconds / Duration.MICROSECONDS_PER_SECOND); | 153 formatSeconds(x.inMicroseconds / Duration.MICROSECONDS_PER_SECOND); |
155 | 154 |
156 static String formatDurationInMilliseconds(Duration x) => | 155 static String formatDurationInMilliseconds(Duration x) => |
157 formatMillis(x.inMicroseconds / Duration.MICROSECONDS_PER_MILLISECOND); | 156 formatMillis(x.inMicroseconds / Duration.MICROSECONDS_PER_MILLISECOND); |
158 | 157 |
159 static bool runningInJavaScript() => identical(1.0, 1); | 158 static bool runningInJavaScript() => identical(1.0, 1); |
160 | 159 |
161 static formatStringAsLiteral(String value, [bool wasTruncated=false]) { | 160 static formatStringAsLiteral(String value, [bool wasTruncated = false]) { |
162 var result = new List(); | 161 var result = new List(); |
163 result.add("'".codeUnitAt(0)); | 162 result.add("'".codeUnitAt(0)); |
164 for (int codeUnit in value.codeUnits) { | 163 for (int codeUnit in value.codeUnits) { |
165 if (codeUnit == '\n'.codeUnitAt(0)) result.addAll('\\n'.codeUnits); | 164 if (codeUnit == '\n'.codeUnitAt(0)) |
166 else if (codeUnit == '\r'.codeUnitAt(0)) result.addAll('\\r'.codeUnits); | 165 result.addAll('\\n'.codeUnits); |
167 else if (codeUnit == '\f'.codeUnitAt(0)) result.addAll('\\f'.codeUnits); | 166 else if (codeUnit == '\r'.codeUnitAt(0)) |
168 else if (codeUnit == '\b'.codeUnitAt(0)) result.addAll('\\b'.codeUnits); | 167 result.addAll('\\r'.codeUnits); |
169 else if (codeUnit == '\t'.codeUnitAt(0)) result.addAll('\\t'.codeUnits); | 168 else if (codeUnit == '\f'.codeUnitAt(0)) |
170 else if (codeUnit == '\v'.codeUnitAt(0)) result.addAll('\\v'.codeUnits); | 169 result.addAll('\\f'.codeUnits); |
171 else if (codeUnit == '\$'.codeUnitAt(0)) result.addAll('\\\$'.codeUnits); | 170 else if (codeUnit == '\b'.codeUnitAt(0)) |
172 else if (codeUnit == '\\'.codeUnitAt(0)) result.addAll('\\\\'.codeUnits); | 171 result.addAll('\\b'.codeUnits); |
173 else if (codeUnit == "'".codeUnitAt(0)) result.addAll("'".codeUnits); | 172 else if (codeUnit == '\t'.codeUnitAt(0)) |
| 173 result.addAll('\\t'.codeUnits); |
| 174 else if (codeUnit == '\v'.codeUnitAt(0)) |
| 175 result.addAll('\\v'.codeUnits); |
| 176 else if (codeUnit == '\$'.codeUnitAt(0)) |
| 177 result.addAll('\\\$'.codeUnits); |
| 178 else if (codeUnit == '\\'.codeUnitAt(0)) |
| 179 result.addAll('\\\\'.codeUnits); |
| 180 else if (codeUnit == "'".codeUnitAt(0)) |
| 181 result.addAll("'".codeUnits); |
174 else if (codeUnit < 32) { | 182 else if (codeUnit < 32) { |
175 var escapeSequence = "\\u" + codeUnit.toRadixString(16).padLeft(4, "0")
; | 183 var escapeSequence = "\\u" + codeUnit.toRadixString(16).padLeft(4, "0"); |
176 result.addAll(escapeSequence.codeUnits); | 184 result.addAll(escapeSequence.codeUnits); |
177 } else result.add(codeUnit); | 185 } else |
| 186 result.add(codeUnit); |
178 } | 187 } |
179 if (wasTruncated) { | 188 if (wasTruncated) { |
180 result.addAll("...".codeUnits); | 189 result.addAll("...".codeUnits); |
181 } else { | 190 } else { |
182 result.add("'".codeUnitAt(0)); | 191 result.add("'".codeUnitAt(0)); |
183 } | 192 } |
184 return new String.fromCharCodes(result); | 193 return new String.fromCharCodes(result); |
185 } | 194 } |
186 } | 195 } |
187 | 196 |
188 /// A [Task] that can be scheduled on the Dart event queue. | 197 /// A [Task] that can be scheduled on the Dart event queue. |
189 class Task { | 198 class Task { |
190 Timer _timer; | 199 Timer _timer; |
191 final Function callback; | 200 final Function callback; |
192 | 201 |
193 Task(this.callback); | 202 Task(this.callback); |
194 | 203 |
195 /// Queue [this] to run on the next Dart event queue pump. Does nothing | 204 /// Queue [this] to run on the next Dart event queue pump. Does nothing |
196 /// if [this] is already queued. | 205 /// if [this] is already queued. |
197 queue() { | 206 queue() { |
198 if (_timer != null) { | 207 if (_timer != null) { |
199 // Already scheduled. | 208 // Already scheduled. |
200 return; | 209 return; |
201 } | 210 } |
202 _timer = new Timer(Duration.ZERO, () { | 211 _timer = new Timer(Duration.ZERO, () { |
203 _timer = null; | 212 _timer = null; |
204 callback(); | 213 callback(); |
205 }); | 214 }); |
206 } | 215 } |
207 } | 216 } |
OLD | NEW |