Stage 1 Draft / May 19, 2018

String.prototype.codePoints

1Properties of the String Prototype Object

1.1String.prototype [ @@iterator ] ( )

When the @@iterator method is called it returns an Iterator object (25.1.1.2) that iterates over the code points of a String value, returning each code point as a String value. The following steps are taken:

  1. Let O be ? RequireObjectCoercible(this value).
  2. Let S be ? ToString(O).
  3. Return CreateStringIterator(S, "string").

The value of the name property of this function is "[Symbol.iterator]".

1.2String.prototype.codePoints ( )

When the codePoints method is called it returns an Iterator object (25.1.1.2) that iterates over the code points of a String value, returning a descriptor consisting of position within a string and code point itself as a Number value. The following steps are taken:

  1. Let O be ? RequireObjectCoercible(this value).
  2. Let S be ? ToString(O).
  3. Return CreateStringIterator(S, "descriptor").

2String Iterator Objects

2.1CreateStringIterator ( string, kind )

Several methods of String objects return Iterator objects. The abstract operation CreateStringIterator with argument string is used to create such iterator objects. It performs the following steps:

  1. Assert: Type(string) is String.
  2. Let iterator be ObjectCreate(%StringIteratorPrototype%, « [[IteratedString]], [[StringIteratorNextIndex]] »).
  3. Set iterator.[[IteratedString]] to string.
  4. Set iterator.[[StringIteratorNextIndex]] to 0.
  5. Set iterator.[[StringIterationKind]] to kind.
  6. Return iterator.

2.2The %StringIteratorPrototype% Object

2.2.1%StringIteratorPrototype%.next ( )

  1. Let O be the this value.
  2. If Type(O) is not Object, throw a TypeError exception.
  3. If O does not have all of the internal slots of a String Iterator Instance (2.3), throw a TypeError exception.
  4. Let s be O.[[IteratedString]].
  5. If s is undefined, return CreateIterResultObject(undefined, true).
  6. Let position be O.[[StringIteratorNextIndex]].
  7. Let len be the length of s.
  8. If position == len, then
    1. Set O.[[IteratedString]] to undefined.
    2. If itemKind is "string", let result be undefined.
    3. Else,
      1. Assert: itemKind is "descriptor".
      2. Let result be ObjectCreate(%ObjectPrototype%).
      3. Perform CreateDataProperty(result, "position", len).
      4. Perform CreateDataProperty(result, "codePoint", undefined).
    4. Return CreateIterResultObject(undefined, true).
    5. Return CreateIterResultObject(result, true).
  9. Let itemKind be O.[[StringIterationKind]].
  10. Let first be the numeric value of the code unit at index position within s.
  11. If first < 0xD800 or first > 0xDBFF or position+1 = len, let resultString be the String value consisting of the single code unit first.
  12. If first ≥ 0xD800 and first ≤ 0xDBFF and position+1 < len, then
    1. Let second be the numeric value of the code unit at index position+1 within the String S.
    2. If second < 0xDC00 or second > 0xDFFF, let resultString be the String value consisting of the single code unit first.
    3. Else, let resultString be the string-concatenation of the code unit first and the code unit second.
    4. If second ≥ 0xDC00 and second ≤ 0xDFFF, then
      1. Set O.[[StringIteratorNextIndex]] to position + 2.
      2. If itemKind is "string", let result be the string-concatenation of the code unit first and the code unit second.
      3. Else,
        1. Assert: itemKind is "descriptor".
        2. Let resultCp be UTF16Decode(first, second).
        3. Let result be ObjectCreate(%ObjectPrototype%).
        4. Perform CreateDataProperty(result, "position", position).
        5. Perform CreateDataProperty(result, "codePoint", resultCp).
      4. Return CreateIterResultObject(result, false).
  13. Let resultSize be the number of code units in resultString.
  14. Set O.[[StringIteratorNextIndex]] to position + resultSize.
  15. Set O.[[StringIteratorNextIndex]] to position + 1.
  16. If itemKind is "string", let result be the String value consisting of the single code unit first.
  17. Else,
    1. Assert: itemKind is "descriptor".
    2. Let result be ObjectCreate(%ObjectPrototype%).
    3. Perform CreateDataProperty(result, "position", position).
    4. Perform CreateDataProperty(result, "codePoint", first).
  18. Return CreateIterResultObject(resultStringresult, false).

2.3Properties of String Iterator Instances

String Iterator instances are ordinary objects that inherit properties from the %StringIteratorPrototype% intrinsic object. String Iterator instances are initially created with the internal slots listed in Table 1.

Table 1: Internal Slots of String Iterator Instances
Internal Slot Description
[[IteratedString]] The String value whose elements are being iterated.
[[StringIteratorNextIndex]] The integer index of the next string index to be examined by this iteration.
[[StringIterationKind]] A String value that identifies what is to be returned for each element of the iteration. The possible values are: "string", "descriptor".

ACopyright & Software License

Copyright Notice

© 2018 Ingvar Stepanyan

Software License

All Software contained in this document ("Software") is protected by copyright and is being made available under the "BSD License", included below. This Software may be subject to third party rights (rights from parties other than Ecma International), including patent rights, and no licenses under such third party rights are granted under this license even if the third party concerned is a member of Ecma International. SEE THE ECMA CODE OF CONDUCT IN PATENT MATTERS AVAILABLE AT https://ecma-international.org/memento/codeofconduct.htm FOR INFORMATION REGARDING THE LICENSING OF PATENT CLAIMS THAT ARE REQUIRED TO IMPLEMENT ECMA INTERNATIONAL STANDARDS.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

  1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  3. Neither the name of the authors nor Ecma International may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE ECMA INTERNATIONAL "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ECMA INTERNATIONAL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.