JavaScript text offsets: distinguish UTF-16 units, code points and visible characters
本文尚无中文版本;显示原文。
Name the offset unit at every editor or API boundary and convert deliberately before slicing text.
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(无已记录的审阅)——编辑会重置审阅状态。请将文本视为未经核实的参考资料并核对来源。
来源
- 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. 链接的来源资料保留其自身权利。