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.collection; | 5 part of dart.collection; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * This [Iterable] mixin implements all [Iterable] members except `iterator`. | 8 * This [Iterable] mixin implements all [Iterable] members except `iterator`. |
| 9 * | 9 * |
| 10 * All other methods are implemented in terms of `iterator`. | 10 * All other methods are implemented in terms of `iterator`. |
| (...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 401 * If the resulting string isn't above 80 characters, more elements are | 401 * If the resulting string isn't above 80 characters, more elements are |
| 402 * included from the start of the iterable. | 402 * included from the start of the iterable. |
| 403 * | 403 * |
| 404 * The conversion may omit calling `toString` on some elements if they | 404 * The conversion may omit calling `toString` on some elements if they |
| 405 * are known to now occur in the output, and it may stop iterating after | 405 * are known to now occur in the output, and it may stop iterating after |
| 406 * a hundred elements. | 406 * a hundred elements. |
| 407 */ | 407 */ |
| 408 String toString() => _iterableToString(this); | 408 String toString() => _iterableToString(this); |
| 409 } | 409 } |
| 410 | 410 |
| 411 String _iterableToString(Iterable iterable) { | 411 String _setToString(Set set) => _collectionToString(set, "{" , "}"); |
|
floitsch
2014/05/23 10:24:55
This is not the same as `IterableMixinWorkaround.t
Lasse Reichstein Nielsen
2014/05/23 11:46:48
I know, and I think it should be shortened if it i
floitsch
2014/05/23 11:53:57
That's a breaking change. I can easily imagine doi
| |
| 412 if (_toStringVisiting.contains(iterable)) return "(...)"; | 412 |
| 413 String _iterableToString(Iterable iterable) => | |
| 414 _collectionToString(iterable, "(", ")"); | |
| 415 | |
| 416 String _collectionToString(Iterable iterable, String before, String after) { | |
| 417 if (_toStringVisiting.contains(iterable)) return "$before...$after"; | |
| 413 _toStringVisiting.add(iterable); | 418 _toStringVisiting.add(iterable); |
| 414 List parts = []; | 419 List parts = []; |
| 415 try { | 420 try { |
| 416 _iterablePartsToStrings(iterable, parts); | 421 _collectionPartsToStrings(iterable, parts); |
| 417 } finally { | 422 } finally { |
| 418 _toStringVisiting.remove(iterable); | 423 _toStringVisiting.remove(iterable); |
| 419 } | 424 } |
| 420 return (new StringBuffer("(")..writeAll(parts, ", ")..write(")")).toString(); | 425 return (new StringBuffer(before) |
| 426 ..writeAll(parts, ", ") | |
| 427 ..write(after)).toString(); | |
| 421 } | 428 } |
| 422 | 429 |
| 423 /** Convert elments of [iterable] to strings and store them in [parts]. */ | 430 /** Convert elments of [iterable] to strings and store them in [parts]. */ |
| 424 void _iterablePartsToStrings(Iterable iterable, List parts) { | 431 void _collectionPartsToStrings(Iterable iterable, List parts) { |
| 425 /// Try to stay below this many characters. | 432 /// Try to stay below this many characters. |
| 426 const int LENGTH_LIMIT = 80; | 433 const int LENGTH_LIMIT = 80; |
| 427 /// Always at least this many elements at the start. | 434 /// Always at least this many elements at the start. |
| 428 const int HEAD_COUNT = 3; | 435 const int HEAD_COUNT = 3; |
| 429 /// Always at least this many elements at the end. | 436 /// Always at least this many elements at the end. |
| 430 const int TAIL_COUNT = 2; | 437 const int TAIL_COUNT = 2; |
| 431 /// Stop iterating after this many elements. Iterables can be infinite. | 438 /// Stop iterating after this many elements. Iterables can be infinite. |
| 432 const int MAX_COUNT = 100; | 439 const int MAX_COUNT = 100; |
| 433 // Per entry length overhead. It's for ", " for all after the first entry, | 440 // Per entry length overhead. It's for ", " for all after the first entry, |
| 434 // and for "(" and ")" for the initial entry. By pure luck, that's the same | 441 // and for "(" and ")" for the initial entry. By pure luck, that's the same |
| 435 // number. | 442 // number. |
| 436 const int OVERHEAD = 2; | 443 const int OVERHEAD = 2; |
| 437 const int ELLIPSIS_SIZE = 3; // "...".length. | 444 const int ELLIPSIS_SIZE = 3; // "...".length. |
| 438 | |
| 439 int length = 0; | 445 int length = 0; |
| 440 int count = 0; | 446 int count = 0; |
| 441 Iterator it = iterable.iterator; | 447 Iterator it = iterable.iterator; |
| 442 // Initial run of elements, at least HEAD_COUNT, and then continue until | 448 // Initial run of elements, at least HEAD_COUNT, and then continue until |
| 443 // passing at most LENGTH_LIMIT characters. | 449 // passing at most LENGTH_LIMIT characters. |
| 444 while (length < LENGTH_LIMIT || count < HEAD_COUNT) { | 450 while (length < LENGTH_LIMIT || count < HEAD_COUNT) { |
| 445 if (!it.moveNext()) return; | 451 if (!it.moveNext()) { |
| 452 return; | |
| 453 } | |
| 446 String next = "${it.current}"; | 454 String next = "${it.current}"; |
| 447 parts.add(next); | 455 parts.add(next); |
| 448 length += next.length + OVERHEAD; | 456 length += next.length + OVERHEAD; |
| 449 count++; | 457 count++; |
| 450 } | 458 } |
| 451 | 459 |
| 452 String penultimateString; | 460 String penultimateString; |
| 453 String ultimateString; | 461 String ultimateString; |
| 454 | 462 |
| 455 // Find last two elements. One or more of them may already be in the | 463 // Find last two elements. One or more of them may already be in the |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 509 String elision = null; | 517 String elision = null; |
| 510 if (count > parts.length + TAIL_COUNT) { | 518 if (count > parts.length + TAIL_COUNT) { |
| 511 elision = "..."; | 519 elision = "..."; |
| 512 length += ELLIPSIS_SIZE + OVERHEAD; | 520 length += ELLIPSIS_SIZE + OVERHEAD; |
| 513 } | 521 } |
| 514 | 522 |
| 515 // If the last two elements were very long, and we have more than | 523 // If the last two elements were very long, and we have more than |
| 516 // HEAD_COUNT elements in the initial run, drop some to make room for | 524 // HEAD_COUNT elements in the initial run, drop some to make room for |
| 517 // the last two. | 525 // the last two. |
| 518 while (length > LENGTH_LIMIT && parts.length > HEAD_COUNT) { | 526 while (length > LENGTH_LIMIT && parts.length > HEAD_COUNT) { |
| 519 length -= parts.removeLast().length + OVERHEAD; | 527 String lastPart = parts.removeLast(); |
| 528 length -= lastPart.length + OVERHEAD; | |
| 520 if (elision == null) { | 529 if (elision == null) { |
| 521 elision = "..."; | 530 elision = "..."; |
| 522 length += ELLIPSIS_SIZE + OVERHEAD; | 531 length += ELLIPSIS_SIZE + OVERHEAD; |
| 523 } | 532 } |
| 524 } | 533 } |
| 525 if (elision != null) { | 534 if (elision != null) { |
| 526 parts.add(elision); | 535 parts.add(elision); |
| 527 } | 536 } |
| 528 parts.add(penultimateString); | 537 parts.add(penultimateString); |
| 529 parts.add(ultimateString); | 538 parts.add(ultimateString); |
| 530 } | 539 } |
| OLD | NEW |