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. Материалы по ссылкам сохраняют собственные права.

Машинный доступ