4 ms·
JS does require UTF-16, because the surrogate pairs of non-BMP characters are separable in JS strings. '<non-BMP character>'.length == 2 '<non-BMP character>'[
by khuey 15y ago
JS does require UTF-16, because the surrogate pairs of non-BMP characters are separable in JS strings.
'<non-BMP character>'.length == 2
'<non-BMP character>'[0] == first codepoint in the surrogate pair
'<non-BMP character>'[1] == second codepoint in the surrogate pair
Any JS implementation using UTF-8 would have to convert to UTF-16 for proper answers to .length and array indexing on strings.
- JoshTriplett 15y agoStill doesn't mean that it has to store the strings in UTF-16. And a bit of common-case optimizing would allow ignoring that case until a string actually ends up with a non-BMP character in it. Alternatively, if a JavaScript implementation chose to completely ignore that particular requirement, I'd guess that approximately zero pages (outside of test cases) would break, and a few currently broken pages (that assumed sane Unicode handling) would start working.
- soc88 15y agoIf stuff like length and indexing depend on the actual underlying representation, the implementation is broken regardless of the actual format used.
- btilly 15y agoChange "implementation" to spec and I agree with you. An implementation that fails to meet the spec can be argued as not broken, even if said spec is broken.
- gwillen 15y agoThis is horrifying. :-(