サロゲートペアの理解
UTF-16 がサロゲートペアを使って BMP 外の文字をどう表現するか、そしてそれが重要な理由を学びます。
问题
1 つの絵文字や希少文字が 2 つのコード単位として数えられ、文字列長やインデックス操作が壊れる現象に直面しています。
前提条件
- Unicode コードポイントの理解
- UTF-16 への習熟
ステップ
-
1. Understand the BMP and beyond
The Basic Multilingual Plane (U+0000 to U+FFFF) covers most common characters. Characters above U+FFFF (like emoji) cannot fit in a single 16-bit code unit and require a surrogate pair in UTF-16.
入力😀 = U+1F600输出above BMP, needs surrogate pair -
2. Compute the surrogate pair
Subtract 0x10000 from the code point, giving a 20-bit value. The high 10 bits plus 0xD800 form the high surrogate. The low 10 bits plus 0xDC00 form the low surrogate. For U+1F600, the pair is D83D DE00.
入力U+1F600输出high: D83D, low: DE00 -
3. Handle pairs in code
When iterating or indexing strings, account for surrogate pairs. A string's .length counts code units, not characters. Use code-point-aware APIs (like String.fromCodePoint and [...str]) to avoid splitting pairs.
入力'😀'.length输出2 (code units), not 1
期待される結果
BMP 外の文字を正しく処理でき、サロゲートペアの分割や誤った長さカウントが起こらないこと。
よくあるエラー
- Splitting a string at an index that falls inside a surrogate pair
- Use code-point-aware iteration or check whether the code unit at the index is a low or high surrogate before splitting.
- String length is double the expected character count
- The string contains characters outside the BMP. Count code points, not code units, using a spread operator or iterator.
トラブルシューティング指引
- If a character shows as two boxes, a surrogate pair was split. Rejoin them or use a UTF-8 representation.
- JavaScript strings are UTF-16; use Array.from(str) or [...str] to iterate by code point.
セキュリティ上の制限
- Surrogate pair bugs can cause truncation or misinterpretation of input, potentially bypassing length-based security checks.