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 import '../common.dart'; | 5 import '../common.dart'; |
6 import '../util/util.dart'; | 6 import '../util/util.dart'; |
7 | 7 |
8 /// Function signature for [trace]. | 8 /// Function signature for [trace]. |
9 typedef void Trace(String message, | 9 typedef void Trace(String message, |
10 {bool condition(String stackTrace), | 10 {bool condition(String stackTrace), int limit, bool throwOnPrint}); |
11 int limit, | |
12 bool throwOnPrint}); | |
13 | 11 |
14 /** | 12 /** |
15 * Helper method for printing stack traces for debugging. | 13 * Helper method for printing stack traces for debugging. |
16 * | 14 * |
17 * [message] is printed as the header of the stack trace. | 15 * [message] is printed as the header of the stack trace. |
18 * | 16 * |
19 * If [condition] is provided, the stack trace is only printed if [condition] | 17 * If [condition] is provided, the stack trace is only printed if [condition] |
20 * returns [:true:] on the stack trace text. This can be used to filter the | 18 * returns [:true:] on the stack trace text. This can be used to filter the |
21 * printed stack traces based on their content. For instance only print stack | 19 * printed stack traces based on their content. For instance only print stack |
22 * traces that contain specific paths. | 20 * traces that contain specific paths. |
23 * | 21 * |
24 * If [limit] is provided, the stack trace is limited to [limit] entries. | 22 * If [limit] is provided, the stack trace is limited to [limit] entries. |
25 * | 23 * |
26 * If [throwOnPrint] is `true`, [message] will be thrown after the stack trace | 24 * If [throwOnPrint] is `true`, [message] will be thrown after the stack trace |
27 * has been printed. Together with [condition] this can be used to discover | 25 * has been printed. Together with [condition] this can be used to discover |
28 * unknown call-sites in tests by filtering known call-sites and throwning | 26 * unknown call-sites in tests by filtering known call-sites and throwning |
29 * otherwise. | 27 * otherwise. |
30 */ | 28 */ |
31 Trace get trace { | 29 Trace get trace { |
32 enableDebugMode(); | 30 enableDebugMode(); |
33 return _trace; | 31 return _trace; |
34 } | 32 } |
35 | 33 |
36 void _trace(String message, {bool condition(String stackTrace), int limit, | 34 void _trace(String message, |
37 bool throwOnPrint: false}) { | 35 {bool condition(String stackTrace), int limit, bool throwOnPrint: false}) { |
38 try { | 36 try { |
39 throw ''; | 37 throw ''; |
40 } catch (e, s) { | 38 } catch (e, s) { |
41 String stackTrace; | 39 String stackTrace; |
42 try { | 40 try { |
43 stackTrace = prettifyStackTrace( | 41 stackTrace = prettifyStackTrace(s, |
44 s, rangeStart: 1, rangeEnd: limit, filePrefix: stackTraceFilePrefix); | 42 rangeStart: 1, rangeEnd: limit, filePrefix: stackTraceFilePrefix); |
45 } catch (e) { | 43 } catch (e) { |
46 print(e); | 44 print(e); |
47 stackTrace = '$s'; | 45 stackTrace = '$s'; |
48 } | 46 } |
49 if (condition != null) { | 47 if (condition != null) { |
50 if (!condition(stackTrace)) return; | 48 if (!condition(stackTrace)) return; |
51 } | 49 } |
52 print('$message\n$stackTrace'); | 50 print('$message\n$stackTrace'); |
53 if (throwOnPrint) throw message; | 51 if (throwOnPrint) throw message; |
54 } | 52 } |
55 } | 53 } |
56 | 54 |
57 /// Creates a function to use as an `condition` argument in [trace] that filters | 55 /// Creates a function to use as an `condition` argument in [trace] that filters |
58 /// stack traces that contains any of the [exceptions]. | 56 /// stack traces that contains any of the [exceptions]. |
59 traceExceptions(List<String> exceptions) { | 57 traceExceptions(List<String> exceptions) { |
60 return (String stackTrace) => !exceptions.any(stackTrace.contains); | 58 return (String stackTrace) => !exceptions.any(stackTrace.contains); |
61 } | 59 } |
62 | 60 |
63 /// Function signature of [traceAndReport]. | 61 /// Function signature of [traceAndReport]. |
64 typedef void TraceAndReport(DiagnosticReporter reporter, | 62 typedef void TraceAndReport( |
65 Spannable node, | 63 DiagnosticReporter reporter, Spannable node, String message, |
66 String message, | 64 {bool condition(String stackTrace), int limit, bool throwOnPrint}); |
67 {bool condition(String stackTrace), | |
68 int limit, | |
69 bool throwOnPrint}); | |
70 | 65 |
71 /// Calls [reportHere] and [trace] with the same message. | 66 /// Calls [reportHere] and [trace] with the same message. |
72 TraceAndReport get traceAndReport { | 67 TraceAndReport get traceAndReport { |
73 enableDebugMode(); | 68 enableDebugMode(); |
74 return _traceAndReport; | 69 return _traceAndReport; |
75 } | 70 } |
76 | 71 |
77 /// Calls [reportHere] and [trace] with the same message. | 72 /// Calls [reportHere] and [trace] with the same message. |
78 TraceAndReport get reportAndTrace => traceAndReport; | 73 TraceAndReport get reportAndTrace => traceAndReport; |
79 | 74 |
80 /// Implementation of [traceAndReport]. | 75 /// Implementation of [traceAndReport]. |
81 void _traceAndReport(DiagnosticReporter reporter, | 76 void _traceAndReport( |
82 Spannable node, | 77 DiagnosticReporter reporter, Spannable node, String message, |
83 String message, | 78 {bool condition(String stackTrace), int limit, bool throwOnPrint: false}) { |
84 {bool condition(String stackTrace), int limit, | |
85 bool throwOnPrint: false}) { | |
86 | |
87 trace(message, limit: limit, throwOnPrint: throwOnPrint, | 79 trace(message, limit: limit, throwOnPrint: throwOnPrint, |
88 condition: (String stackTrace) { | 80 condition: (String stackTrace) { |
89 bool result = condition != null ? condition(stackTrace) : true; | 81 bool result = condition != null ? condition(stackTrace) : true; |
90 if (result) { | 82 if (result) { |
91 reportHere(reporter, node, message); | 83 reportHere(reporter, node, message); |
92 } | 84 } |
93 return result; | 85 return result; |
94 }); | 86 }); |
95 } | 87 } |
96 | 88 |
97 /// Returns the [StackTraceLines] for the current call stack. | 89 /// Returns the [StackTraceLines] for the current call stack. |
98 /// | 90 /// |
99 /// Use [offset] to discard the first [offset] calls of the call stack. Defaults | 91 /// Use [offset] to discard the first [offset] calls of the call stack. Defaults |
100 /// to `1`, that is, discard the call to [stackTrace] itself. Use [limit] to | 92 /// to `1`, that is, discard the call to [stackTrace] itself. Use [limit] to |
101 /// limit the length of the stack trace lines. | 93 /// limit the length of the stack trace lines. |
102 StackTraceLines stackTrace({int offset: 1, | 94 StackTraceLines stackTrace({int offset: 1, int limit: null}) { |
103 int limit: null}) { | |
104 int rangeStart = offset; | 95 int rangeStart = offset; |
105 int rangeEnd = limit == null ? null : rangeStart + limit; | 96 int rangeEnd = limit == null ? null : rangeStart + limit; |
106 try { | 97 try { |
107 throw ''; | 98 throw ''; |
108 } catch (_, stackTrace) { | 99 } catch (_, stackTrace) { |
109 return new StackTraceLines.fromTrace(stackTrace, | 100 return new StackTraceLines.fromTrace(stackTrace, |
110 rangeStart: offset, rangeEnd: rangeEnd, | 101 rangeStart: offset, |
| 102 rangeEnd: rangeEnd, |
111 filePrefix: stackTraceFilePrefix); | 103 filePrefix: stackTraceFilePrefix); |
112 } | 104 } |
113 return null; | 105 return null; |
114 } | 106 } |
115 | 107 |
116 /// A stack trace as a sequence of [StackTraceLine]s. | 108 /// A stack trace as a sequence of [StackTraceLine]s. |
117 class StackTraceLines { | 109 class StackTraceLines { |
118 final List<StackTraceLine> lines; | 110 final List<StackTraceLine> lines; |
119 final int maxFileLength; | 111 final int maxFileLength; |
120 final int maxLineNoLength; | 112 final int maxLineNoLength; |
121 final int maxColumnNoLength; | 113 final int maxColumnNoLength; |
122 | 114 |
123 factory StackTraceLines.fromTrace(StackTrace s, | 115 factory StackTraceLines.fromTrace(StackTrace s, |
124 {int rangeStart, | 116 {int rangeStart, int rangeEnd, String filePrefix, String lambda: r'?'}) { |
125 int rangeEnd, | |
126 String filePrefix, | |
127 String lambda: r'?'}) { | |
128 final RegExp indexPattern = new RegExp(r'#\d+\s*'); | 117 final RegExp indexPattern = new RegExp(r'#\d+\s*'); |
129 int index = -1; | 118 int index = -1; |
130 int maxFileLength = 0; | 119 int maxFileLength = 0; |
131 int maxLineNoLength = 0; | 120 int maxLineNoLength = 0; |
132 int maxColumnNoLength = 0; | 121 int maxColumnNoLength = 0; |
133 | 122 |
134 String stackTrace = '$s'; | 123 String stackTrace = '$s'; |
135 List<StackTraceLine> lines = <StackTraceLine>[]; | 124 List<StackTraceLine> lines = <StackTraceLine>[]; |
136 // Parse each line in the stack trace. The supported line formats from the | 125 // Parse each line in the stack trace. The supported line formats from the |
137 // Dart VM are: | 126 // Dart VM are: |
138 // #n <method-name> (<uri>:<line-no>:<column-no>) | 127 // #n <method-name> (<uri>:<line-no>:<column-no>) |
139 // #n <method-name> (<uri>:<line-no>) | 128 // #n <method-name> (<uri>:<line-no>) |
140 // #n <method-name> (<uri>) | 129 // #n <method-name> (<uri>) |
141 // in which '<anonymous closure>' is the name used for an (unnamed) function | 130 // in which '<anonymous closure>' is the name used for an (unnamed) function |
142 // expression. The last case is used for async bodies. | 131 // expression. The last case is used for async bodies. |
143 for (String line in stackTrace.split('\n')) { | 132 for (String line in stackTrace.split('\n')) { |
144 try { | 133 try { |
145 index++; | 134 index++; |
146 if (rangeStart != null && index < rangeStart) continue; | 135 if (rangeStart != null && index < rangeStart) continue; |
147 if (rangeEnd != null && index > rangeEnd) break; | 136 if (rangeEnd != null && index > rangeEnd) break; |
148 if (line.isEmpty) continue; | 137 if (line.isEmpty) continue; |
149 | 138 |
150 // Strip index. | 139 // Strip index. |
151 line = line.replaceFirst(indexPattern, ''); | 140 line = line.replaceFirst(indexPattern, ''); |
152 | 141 |
153 int leftParenPos = line.indexOf('('); | 142 int leftParenPos = line.indexOf('('); |
154 int rightParenPos = line.indexOf(')', leftParenPos); | 143 int rightParenPos = line.indexOf(')', leftParenPos); |
155 int lastColon = line.lastIndexOf(':', rightParenPos); | 144 int lastColon = line.lastIndexOf(':', rightParenPos); |
156 int nextToLastColon = line.lastIndexOf(':', lastColon-1); | 145 int nextToLastColon = line.lastIndexOf(':', lastColon - 1); |
157 | 146 |
158 String lineNo; | 147 String lineNo; |
159 String columnNo; | 148 String columnNo; |
160 if (nextToLastColon != -1) { | 149 if (nextToLastColon != -1) { |
161 lineNo = line.substring(nextToLastColon+1, lastColon); | 150 lineNo = line.substring(nextToLastColon + 1, lastColon); |
162 columnNo = line.substring(lastColon+1, rightParenPos); | 151 columnNo = line.substring(lastColon + 1, rightParenPos); |
163 try { | 152 try { |
164 int.parse(columnNo); | 153 int.parse(columnNo); |
165 try { | 154 try { |
166 int.parse(lineNo); | 155 int.parse(lineNo); |
167 } on FormatException { | 156 } on FormatException { |
168 // Only line number. | 157 // Only line number. |
169 lineNo = columnNo; | 158 lineNo = columnNo; |
170 columnNo = ''; | 159 columnNo = ''; |
171 nextToLastColon = lastColon; | 160 nextToLastColon = lastColon; |
172 } | 161 } |
173 } on FormatException { | 162 } on FormatException { |
174 // No column number nor line number. | 163 // No column number nor line number. |
175 lineNo = ''; | 164 lineNo = ''; |
176 columnNo = ''; | 165 columnNo = ''; |
177 nextToLastColon = rightParenPos; | 166 nextToLastColon = rightParenPos; |
178 } | 167 } |
179 } else { | 168 } else { |
180 lineNo = line.substring(lastColon+1, rightParenPos); | 169 lineNo = line.substring(lastColon + 1, rightParenPos); |
181 columnNo = ''; | 170 columnNo = ''; |
182 try { | 171 try { |
183 int.parse(lineNo); | 172 int.parse(lineNo); |
184 nextToLastColon = lastColon; | 173 nextToLastColon = lastColon; |
185 } on FormatException { | 174 } on FormatException { |
186 // No column number nor line number. | 175 // No column number nor line number. |
187 lineNo = columnNo; | 176 lineNo = columnNo; |
188 columnNo = ''; | 177 columnNo = ''; |
189 nextToLastColon = rightParenPos; | 178 nextToLastColon = rightParenPos; |
190 } | 179 } |
191 } | 180 } |
192 | 181 |
193 if (lineNo.length > maxLineNoLength) { | 182 if (lineNo.length > maxLineNoLength) { |
194 maxLineNoLength = lineNo.length; | 183 maxLineNoLength = lineNo.length; |
195 } | 184 } |
196 if (columnNo.length > maxColumnNoLength) { | 185 if (columnNo.length > maxColumnNoLength) { |
197 maxColumnNoLength = columnNo.length; | 186 maxColumnNoLength = columnNo.length; |
198 } | 187 } |
199 | 188 |
200 String file = line.substring(leftParenPos+1, nextToLastColon); | 189 String file = line.substring(leftParenPos + 1, nextToLastColon); |
201 if (filePrefix != null && file.startsWith(filePrefix)) { | 190 if (filePrefix != null && file.startsWith(filePrefix)) { |
202 file = file.substring(filePrefix.length); | 191 file = file.substring(filePrefix.length); |
203 } | 192 } |
204 if (file.length > maxFileLength) { | 193 if (file.length > maxFileLength) { |
205 maxFileLength = file.length; | 194 maxFileLength = file.length; |
206 } | 195 } |
207 String method = line.substring(0, leftParenPos-1); | 196 String method = line.substring(0, leftParenPos - 1); |
208 if (lambda != null) { | 197 if (lambda != null) { |
209 method = method.replaceAll('<anonymous closure>', lambda); | 198 method = method.replaceAll('<anonymous closure>', lambda); |
210 } | 199 } |
211 lines.add(new StackTraceLine(index, file, lineNo, columnNo, method)); | 200 lines.add(new StackTraceLine(index, file, lineNo, columnNo, method)); |
212 } catch (e) { | 201 } catch (e) { |
213 throw 'Error prettifying "$line": $e'; | 202 throw 'Error prettifying "$line": $e'; |
214 } | 203 } |
215 } | 204 } |
216 return new StackTraceLines.fromLines( | 205 return new StackTraceLines.fromLines( |
217 lines, maxFileLength, maxLineNoLength, maxColumnNoLength); | 206 lines, maxFileLength, maxLineNoLength, maxColumnNoLength); |
218 } | 207 } |
219 | 208 |
220 StackTraceLines.fromLines(this.lines, | 209 StackTraceLines.fromLines(this.lines, this.maxFileLength, |
221 this.maxFileLength, | 210 this.maxLineNoLength, this.maxColumnNoLength); |
222 this.maxLineNoLength, | |
223 this.maxColumnNoLength); | |
224 | 211 |
225 StackTraceLines subtrace(int offset) { | 212 StackTraceLines subtrace(int offset) { |
226 return new StackTraceLines.fromLines( | 213 return new StackTraceLines.fromLines(lines.sublist(offset), maxFileLength, |
227 lines.sublist(offset), | 214 maxLineNoLength, maxColumnNoLength); |
228 maxFileLength, | |
229 maxLineNoLength, | |
230 maxColumnNoLength); | |
231 } | 215 } |
232 | 216 |
233 String prettify({bool showColumnNo: false, | 217 String prettify({bool showColumnNo: false, bool showDots: true}) { |
234 bool showDots: true}) { | |
235 StringBuffer sb = new StringBuffer(); | 218 StringBuffer sb = new StringBuffer(); |
236 bool dots = true; | 219 bool dots = true; |
237 for (StackTraceLine line in lines) { | 220 for (StackTraceLine line in lines) { |
238 sb.write(' '); | 221 sb.write(' '); |
239 line.printOn(sb, | 222 line.printOn(sb, |
240 fileLength: maxFileLength, | 223 fileLength: maxFileLength, |
241 padding: showDots && dots ? ' .' : ' ', | 224 padding: showDots && dots ? ' .' : ' ', |
242 lineNoLength: maxLineNoLength, | 225 lineNoLength: maxLineNoLength, |
243 showColumnNo: showColumnNo, | 226 showColumnNo: showColumnNo, |
244 columnNoLength: maxColumnNoLength); | 227 columnNoLength: maxColumnNoLength); |
245 | 228 |
246 dots = !dots; | 229 dots = !dots; |
247 } | 230 } |
248 return sb.toString(); | 231 return sb.toString(); |
249 } | 232 } |
250 | 233 |
251 String toString() { | 234 String toString() { |
252 return prettify(); | 235 return prettify(); |
253 } | 236 } |
254 } | 237 } |
255 | 238 |
256 /// A parsed line from a stack trace. | 239 /// A parsed line from a stack trace. |
257 class StackTraceLine { | 240 class StackTraceLine { |
258 final int index; | 241 final int index; |
259 final String file; | 242 final String file; |
260 final String lineNo; | 243 final String lineNo; |
261 final String columnNo; | 244 final String columnNo; |
262 final String method; | 245 final String method; |
263 | 246 |
264 StackTraceLine(this.index, this.file, this.lineNo, | 247 StackTraceLine( |
265 this.columnNo, this.method); | 248 this.index, this.file, this.lineNo, this.columnNo, this.method); |
266 | 249 |
267 void printOn(StringBuffer sb, | 250 void printOn(StringBuffer sb, |
268 {String padding: ' ', | 251 {String padding: ' ', |
269 int fileLength, | 252 int fileLength, |
270 int lineNoLength, | 253 int lineNoLength, |
271 int columnNoLength, | 254 int columnNoLength, |
272 bool showColumnNo: false}) { | 255 bool showColumnNo: false}) { |
273 String fileText = '${file} '; | 256 String fileText = '${file} '; |
274 if (fileLength != null) { | 257 if (fileLength != null) { |
275 fileText = pad(fileText, fileLength, dots: padding); | 258 fileText = pad(fileText, fileLength, dots: padding); |
276 } | 259 } |
277 String lineNoText = lineNo; | 260 String lineNoText = lineNo; |
278 if (lineNoLength != null) { | 261 if (lineNoLength != null) { |
279 lineNoText = pad(lineNoText, lineNoLength, padLeft: true); | 262 lineNoText = pad(lineNoText, lineNoLength, padLeft: true); |
280 } | 263 } |
281 String columnNoText = showColumnNo ? '': columnNo; | 264 String columnNoText = showColumnNo ? '' : columnNo; |
282 if (columnNoLength != null) { | 265 if (columnNoLength != null) { |
283 columnNoText = ':${pad(columnNoText, columnNoLength)}'; | 266 columnNoText = ':${pad(columnNoText, columnNoLength)}'; |
284 } | 267 } |
285 sb.write('$fileText $lineNoText$columnNoText $method\n'); | 268 sb.write('$fileText $lineNoText$columnNoText $method\n'); |
286 } | 269 } |
287 | 270 |
288 int get hashCode { | 271 int get hashCode { |
289 return 13 * index + | 272 return 13 * index + |
290 17 * file.hashCode + | 273 17 * file.hashCode + |
291 19 * lineNo.hashCode + | 274 19 * lineNo.hashCode + |
292 23 * columnNo.hashCode + | 275 23 * columnNo.hashCode + |
293 29 * method.hashCode; | 276 29 * method.hashCode; |
294 } | 277 } |
295 | 278 |
296 bool operator ==(other) { | 279 bool operator ==(other) { |
297 if (identical(this, other)) return true; | 280 if (identical(this, other)) return true; |
298 if (other is! StackTraceLine) return false; | 281 if (other is! StackTraceLine) return false; |
299 return index == other.index && | 282 return index == other.index && |
300 file == other.file && | 283 file == other.file && |
301 lineNo == other.lineNo && | 284 lineNo == other.lineNo && |
302 columnNo == other.columnNo && | 285 columnNo == other.columnNo && |
303 method == other.method; | 286 method == other.method; |
304 } | 287 } |
305 | 288 |
306 String toString() => "$method @ $file [$lineNo:$columnNo]"; | 289 String toString() => "$method @ $file [$lineNo:$columnNo]"; |
307 } | 290 } |
308 | 291 |
309 // TODO(johnniwinther): Use this format for --throw-on-error. | 292 // TODO(johnniwinther): Use this format for --throw-on-error. |
310 /** | 293 /** |
311 * Converts the normal VM stack trace into a more compact and readable format. | 294 * Converts the normal VM stack trace into a more compact and readable format. |
312 * | 295 * |
313 * The output format is [: <file> . . . <lineNo>:<columnNo> <method> :] where | 296 * The output format is [: <file> . . . <lineNo>:<columnNo> <method> :] where |
314 * [: <file> :] is file name, [: <lineNo> :] is the line number, | 297 * [: <file> :] is file name, [: <lineNo> :] is the line number, |
315 * [: <columnNo> :] is the column number, and [: <method> :] is the method name. | 298 * [: <columnNo> :] is the column number, and [: <method> :] is the method name. |
316 * | 299 * |
317 * If [rangeStart] and/or [rangeEnd] are provided, only the lines within the | 300 * If [rangeStart] and/or [rangeEnd] are provided, only the lines within the |
318 * range are included. | 301 * range are included. |
319 * If [showColumnNo] is [:false:], the [: :<columnNo> :] part is omitted. | 302 * If [showColumnNo] is [:false:], the [: :<columnNo> :] part is omitted. |
320 * If [showDots] is [:true:], the space between [: <file> :] and [: <lineNo> :] | 303 * If [showDots] is [:true:], the space between [: <file> :] and [: <lineNo> :] |
321 * is padded with dots on every other line. | 304 * is padded with dots on every other line. |
322 * If [filePrefix] is provided, then for every file name thats starts with | 305 * If [filePrefix] is provided, then for every file name thats starts with |
323 * [filePrefix] only the remainder is printed. | 306 * [filePrefix] only the remainder is printed. |
324 * If [lambda] is non-null, anonymous closures are printed as [lambda]. | 307 * If [lambda] is non-null, anonymous closures are printed as [lambda]. |
325 */ | 308 */ |
326 String prettifyStackTrace(StackTrace stackTrace, | 309 String prettifyStackTrace(StackTrace stackTrace, |
327 {int rangeStart, | 310 {int rangeStart, |
328 int rangeEnd, | 311 int rangeEnd, |
329 bool showColumnNo: false, | 312 bool showColumnNo: false, |
330 bool showDots: true, | 313 bool showDots: true, |
331 String filePrefix, | 314 String filePrefix, |
332 String lambda: r'?'}) { | 315 String lambda: r'?'}) { |
333 return new StackTraceLines.fromTrace(stackTrace, | 316 return new StackTraceLines.fromTrace(stackTrace, |
334 rangeStart: rangeStart, rangeEnd: rangeEnd, | 317 rangeStart: rangeStart, |
335 filePrefix: filePrefix, lambda: lambda) | 318 rangeEnd: rangeEnd, |
336 .prettify(showColumnNo: showColumnNo, showDots: showDots); | 319 filePrefix: filePrefix, |
| 320 lambda: lambda) |
| 321 .prettify(showColumnNo: showColumnNo, showDots: showDots); |
337 } | 322 } |
338 | 323 |
339 /** | 324 /** |
340 * Pads (or truncates) [text] to the [intendedLength]. | 325 * Pads (or truncates) [text] to the [intendedLength]. |
341 * | 326 * |
342 * If [padLeft] is [:true:] the text is padding inserted to the left of [text]. | 327 * If [padLeft] is [:true:] the text is padding inserted to the left of [text]. |
343 * A repetition of the [dots] text is used for padding. | 328 * A repetition of the [dots] text is used for padding. |
344 */ | 329 */ |
345 String pad(String text, int intendedLength, | 330 String pad(String text, int intendedLength, |
346 {bool padLeft: false, String dots: ' '}) { | 331 {bool padLeft: false, String dots: ' '}) { |
347 if (text.length == intendedLength) return text; | 332 if (text.length == intendedLength) return text; |
348 if (text.length > intendedLength) return text.substring(0, intendedLength); | 333 if (text.length > intendedLength) return text.substring(0, intendedLength); |
349 if (dots == null || dots.isEmpty) dots = ' '; | 334 if (dots == null || dots.isEmpty) dots = ' '; |
350 int dotsLength = dots.length; | 335 int dotsLength = dots.length; |
351 StringBuffer sb = new StringBuffer(); | 336 StringBuffer sb = new StringBuffer(); |
352 if (!padLeft) { | 337 if (!padLeft) { |
353 sb.write(text); | 338 sb.write(text); |
354 } | 339 } |
355 for (int index = text.length ; index < intendedLength ; index ++) { | 340 for (int index = text.length; index < intendedLength; index++) { |
356 int dotsIndex = index % dotsLength; | 341 int dotsIndex = index % dotsLength; |
357 sb.write(dots.substring(dotsIndex, dotsIndex + 1)); | 342 sb.write(dots.substring(dotsIndex, dotsIndex + 1)); |
358 } | 343 } |
359 if (padLeft) { | 344 if (padLeft) { |
360 sb.write(text); | 345 sb.write(text); |
361 } | 346 } |
362 return sb.toString(); | 347 return sb.toString(); |
363 } | 348 } |
OLD | NEW |