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.core; | 5 part of dart.core; |
6 | 6 |
7 /** | 7 /** |
8 * A sequence of characters. | 8 * A sequence of characters. |
9 * | 9 * |
10 * A string can be either single or multiline. Single line strings are | 10 * A string can be either single or multiline. Single line strings are |
(...skipping 622 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
633 : this.string = string, _position = 0, _nextPosition = 0; | 633 : this.string = string, _position = 0, _nextPosition = 0; |
634 | 634 |
635 /** | 635 /** |
636 * Create an iterator positioned before the [index]th code unit of the string. | 636 * Create an iterator positioned before the [index]th code unit of the string. |
637 * | 637 * |
638 * When created, there is no [current] value. | 638 * When created, there is no [current] value. |
639 * A [moveNext] will use the rune starting at [index] the current value, | 639 * A [moveNext] will use the rune starting at [index] the current value, |
640 * and a [movePrevious] will use the rune ending just before [index] as the | 640 * and a [movePrevious] will use the rune ending just before [index] as the |
641 * the current value. | 641 * the current value. |
642 * | 642 * |
643 * It is an error if the [index] position is in the middle of a surrogate | 643 * The [index] position must not be in the middle of a surrogate pair. |
644 * pair. | |
645 */ | 644 */ |
646 RuneIterator.at(String string, int index) | 645 RuneIterator.at(String string, int index) |
647 : string = string, _position = index, _nextPosition = index { | 646 : string = string, _position = index, _nextPosition = index { |
648 if (index < 0 || index > string.length) { | 647 RangeError.checkValueInInterval(index, 0, string.length); |
649 throw new RangeError.range(index, 0, string.length); | |
650 } | |
651 _checkSplitSurrogate(index); | 648 _checkSplitSurrogate(index); |
652 } | 649 } |
653 | 650 |
654 /** Throw an error if the index is in the middle of a surrogate pair. */ | 651 /** Throw an error if the index is in the middle of a surrogate pair. */ |
655 void _checkSplitSurrogate(int index) { | 652 void _checkSplitSurrogate(int index) { |
656 if (index > 0 && index < string.length && | 653 if (index > 0 && index < string.length && |
657 _isLeadSurrogate(string.codeUnitAt(index - 1)) && | 654 _isLeadSurrogate(string.codeUnitAt(index - 1)) && |
658 _isTrailSurrogate(string.codeUnitAt(index))) { | 655 _isTrailSurrogate(string.codeUnitAt(index))) { |
659 throw new ArgumentError('Index inside surrogate pair: $index'); | 656 throw new ArgumentError('Index inside surrogate pair: $index'); |
660 } | 657 } |
661 } | 658 } |
662 | 659 |
663 /** | 660 /** |
664 * Returns the starting position of the current rune in the string. | 661 * Returns the starting position of the current rune in the string. |
665 * | 662 * |
666 * Returns null if the [current] rune is null. | 663 * Returns null if the [current] rune is null. |
667 */ | 664 */ |
668 int get rawIndex => (_position != _nextPosition) ? _position : null; | 665 int get rawIndex => (_position != _nextPosition) ? _position : null; |
669 | 666 |
670 /** | 667 /** |
671 * Resets the iterator to the rune at the specified index of the string. | 668 * Resets the iterator to the rune at the specified index of the string. |
672 * | 669 * |
673 * Setting a negative [rawIndex], or one greater than or equal to | 670 * Setting a negative [rawIndex], or one greater than or equal to |
674 * [:string.length:], | 671 * [:string.length:], |
675 * is an error. So is setting it in the middle of a surrogate pair. | 672 * is an error. So is setting it in the middle of a surrogate pair. |
676 * | 673 * |
677 * Setting the position to the end of then string will set [current] to null. | 674 * Setting the position to the end of then string will set [current] to null. |
678 */ | 675 */ |
679 void set rawIndex(int rawIndex) { | 676 void set rawIndex(int rawIndex) { |
680 if (rawIndex >= string.length) { | 677 RangeError.checkValidIndex(rawIndex, string, "rawIndex"); |
681 throw new RangeError.index(rawIndex, string); | |
682 } | |
683 reset(rawIndex); | 678 reset(rawIndex); |
684 moveNext(); | 679 moveNext(); |
685 } | 680 } |
686 | 681 |
687 /** | 682 /** |
688 * Resets the iterator to the given index into the string. | 683 * Resets the iterator to the given index into the string. |
689 * | 684 * |
690 * After this the [current] value is unset. | 685 * After this the [current] value is unset. |
691 * You must call [moveNext] make the rune at the position current, | 686 * You must call [moveNext] make the rune at the position current, |
692 * or [movePrevious] for the last rune before the position. | 687 * or [movePrevious] for the last rune before the position. |
693 * | 688 * |
694 * Setting a negative [rawIndex], or one greater than [:string.length:], | 689 * Setting a negative [rawIndex], or one greater than [:string.length:], |
695 * is an error. So is setting it in the middle of a surrogate pair. | 690 * is an error. So is setting it in the middle of a surrogate pair. |
696 */ | 691 */ |
697 void reset([int rawIndex = 0]) { | 692 void reset([int rawIndex = 0]) { |
698 if (rawIndex < 0 || rawIndex > string.length) { | 693 RangeError.checkValueInInterval(rawIndex, 0, string.length, "rawIndex"); |
699 throw new RangeError.range(rawIndex, 0, string.length); | |
700 } | |
701 _checkSplitSurrogate(rawIndex); | 694 _checkSplitSurrogate(rawIndex); |
702 _position = _nextPosition = rawIndex; | 695 _position = _nextPosition = rawIndex; |
703 _currentCodePoint = null; | 696 _currentCodePoint = null; |
704 } | 697 } |
705 | 698 |
706 /** The rune (integer Unicode code point) starting at the current position in | 699 /** The rune (integer Unicode code point) starting at the current position in |
707 * the string. | 700 * the string. |
708 */ | 701 */ |
709 int get current => _currentCodePoint; | 702 int get current => _currentCodePoint; |
710 | 703 |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
764 _position = position - 1; | 757 _position = position - 1; |
765 _currentCodePoint = _combineSurrogatePair(prevCodeUnit, codeUnit); | 758 _currentCodePoint = _combineSurrogatePair(prevCodeUnit, codeUnit); |
766 return true; | 759 return true; |
767 } | 760 } |
768 } | 761 } |
769 _position = position; | 762 _position = position; |
770 _currentCodePoint = codeUnit; | 763 _currentCodePoint = codeUnit; |
771 return true; | 764 return true; |
772 } | 765 } |
773 } | 766 } |
OLD | NEW |