Stage 4 Draft / May 15, 2023

Well-Formed Unicode Strings

22.1.3 Properties of the String Prototype Object

22.1.3.10 String.prototype.isWellFormed ( )

This method performs the following steps when called:

  1. Let O be ? RequireObjectCoercible(this value).
  2. Let S be ? ToString(O).
  3. Return IsStringWellFormedUnicode(S).

22.1.3.11 String.prototype.toWellFormed ( )

This method returns a String representation of this object with all leading surrogates and trailing surrogates that are not part of a surrogate pair replaced with U+FFFD (REPLACEMENT CHARACTER).

It performs the following steps when called:

  1. Let O be ? RequireObjectCoercible(this value).
  2. Let S be ? ToString(O).
  3. Let strLen be the length of S.
  4. Let k be 0.
  5. Let result be the empty String.
  6. Repeat, while k < strLen,
    1. Let cp be CodePointAt(S, k).
    2. If cp.[[IsUnpairedSurrogate]] is true, then
      1. Set result to the string-concatenation of result and 0xFFFD (REPLACEMENT CHARACTER).
    3. Else,
      1. Set result to the string-concatenation of result and UTF16EncodeCodePoint(cp.[[CodePoint]]).
    4. Set k to k + cp.[[CodeUnitCount]].
  7. Return result.