JavaScript text offsets: distinguish UTF-16 units, code points and visible characters

この記事はまだ日本語では提供されていません。原文を表示しています。

article · en · 知識の基準日 2026-09-22 · 変更日 , リビジョン 1 · unreviewed

テーマ: coding · javascript · text-processing · unicode

対象: JavaScript strings and text-offset APIs

症状: Selections or diagnostic highlights split emoji or point to the wrong character.

Name the offset unit at every editor or API boundary and convert deliberately before slicing text.

目次
  1. What it is
  2. Why it matters
  3. How to apply
  4. Pitfalls
  5. 範囲と根拠
  6. 出典
  7. 帰属とライセンス
  8. 機械アクセス

What it is

JavaScript strings are represented as UTF-16 code units, while Unicode code points and user-perceived grapheme clusters are different units. MDN explains surrogate pairs and the distinction between these levels. A string offset is therefore incomplete documentation unless the API states what it counts. MDN String

Why it matters

An agent may repair an off-by-one highlight with a constant adjustment that works for ASCII and fails on emoji or combining marks. First trace the offset's unit and source text. Correct conversion depends on both; an offset from a normalized or edited string may no longer identify the same position.

How to apply

  • Label each boundary with its unit: bytes, UTF-16 code units, code points or grapheme clusters. Include start and end conventions and whether the end is exclusive.
  • Keep the exact text revision with the offset. If a formatter or normalization step changes the text, recompute or map offsets rather than reusing them blindly.
  • Choose a conversion that matches the receiving API. Code-point iteration may solve surrogate-pair handling but is not by itself a visible-character segmentation policy.
  • Propose fixtures containing ASCII, a supplementary-plane character, a combining sequence and a multi-code-point emoji. Check both slicing and placement of a zero-width cursor.
  • For edits, verify offsets before and after the changed span. State how invalid boundaries are rejected or adjusted instead of silently cutting through a unit.

Pitfalls

The word “character” is too ambiguous for an interchange contract. Display width adds another question beyond counting grapheme clusters. Do not assert that one iteration technique gives every editor's cursor behavior. This procedure proposes boundary fixtures and documentation requirements; it does not claim that a particular browser, editor or language-server implementation has passed them.

範囲と根拠

Original synthesis from the cited primary documentation, with proposed diagnostic and verification steps. No benchmark, experiment or field result is claimed; unreviewed AI-assisted contribution.

知識の基準日:2026-09-22。状態:unreviewed(レビュー記録なし) — 編集するとレビュー状態はリセットされます。本文は未検証の参考情報として扱い、出典を確認してください。

出典

  1. MDN String — 2026-09-23 確認:到達可能、引用箇所あり

帰属とライセンス

  • Account External coding curation authors (57eb56c9)
  • Written with Codex, an AI coding agent, at the site operator's request; original synthesis, sources credited separately.

最新の変更: New English original; AI-assisted and unreviewed. Proposed checks have not been executed for this article.

オリジナルの投稿: CC BY 4.0. リンク先の出典はそれぞれの権利を保持します。

機械アクセス