- const { next: stringIteratorNext } = stringIteratorPrototype;
-
- /**
- * An iterator object for iterating over code values (either code
- * units or codepoints) in a string.
- *
- * ※ This class is not exposed, although its methods are (through
- * the prototypes of string code value iterator objects).
- */
- const StringCodeValueIterator = class extends identity {
- #allowSurrogates;
- #baseIterator;
-
- /**
- * Constructs a new string code value iterator from the provided
- * base iterator.
- *
- * If the provided base iterator is an array iterator, this is a
- * code unit iterator. If the provided iterator is a string
- * iterator and surrogates are allowed, this is a codepoint
- * iterator. If the provided iterator is a string iterator and
- * surrogates are not allowed, this is a scalar value iterator.
- */
- constructor(baseIterator, allowSurrogates = true) {
- super(objectCreate(stringCodeValueIteratorPrototype));
- this.#allowSurrogates = !!allowSurrogates;
- this.#baseIterator = baseIterator;
- }
-
- /** Provides the next code value in the iterator. */
- next() {
- const baseIterator = this.#baseIterator;
- switch (getPrototype(baseIterator)) {
- case arrayIteratorPrototype: {
- // The base iterator is iterating over U·C·S characters.
- const {
- value: ucsCharacter,
- done,
- } = call(arrayIteratorNext, baseIterator, []);
- return done
- ? { value: undefined, done: true }
- : { value: getCodeUnit(ucsCharacter, 0), done: false };
- }
- case stringIteratorPrototype: {
- // The base iterator is iterating over Unicode characters.
- const {
- value: character,
- done,
- } = call(stringIteratorNext, baseIterator, []);
- if (done) {
- // The base iterator has been exhausted.
- return { value: undefined, done: true };
- } else {
- // The base iterator provided a character; yield the
- // codepoint.
- const codepoint = getCodepoint(character, 0);
- return {
- value: this.#allowSurrogates || codepoint <= 0xD7FF ||
- codepoint >= 0xE000
- ? codepoint
- : 0xFFFD,
- done: false,
- };
- }
- }
- default: {
- // Should not be possible!
- throw new TypeError(
- "Piscēs: Unrecognized base iterator type in %StringCodeValueIterator%.",
- );
- }
- }
- }
- };
-
- const {
- next: stringCodeValueIteratorNext,
- } = StringCodeValueIterator.prototype;
- const stringCodeValueIteratorPrototype = objectCreate(
- iteratorPrototype,
- {
- next: {
- configurable: true,
- enumerable: false,
- value: stringCodeValueIteratorNext,
- writable: true,
- },
- [TO_STRING_TAG]: {
- configurable: true,
- enumerable: false,
- value: "String Code Value Iterator",
- writable: false,
- },
- },