A quote reference can give a reader two numbers that mark where the quote starts and ends in a longer string. The numbers are useful only if the producer and the consumer agree on what each position counts. In a text helper we inspected, the range counts Unicode code points and the end is exclusive. A JavaScript string slice, however, counts UTF-16 code units. We compared the two using synthetic text, then checked what the helper did when ranges overlapped or reached beyond the end of the string. The results show why a range needs a unit as much as it needs two endpoints.
Put an emoji before the quote
Take the string “😀 ab cd” and ask for the range from 2 up to but excluding 4. In code points, the emoji is at position 0, the space is at 1, and the letters a and b occupy positions 2 and 3. The space after b is at position 4. The requested range is therefore just “ab”. That sounds simple until the same two numbers reach a consumer that slices a JavaScript string directly.
The emoji takes two UTF-16 code units. In that coordinate system, it occupies positions 0 and 1, the space moves to 2, and a moves to 3. A direct slice from 2 to 4 returns a space followed by a. The browser environment is incidental to this particular test. We ran the comparison in a local JavaScript process, where the extracted helper first split the string into code points and returned “ab” as the highlighted segment. The fixture in the repository also specifies the surrounding unhighlighted text. The two operations received exactly the same range, but their units differed.
An ordinary ASCII-only example would give the same result with either operation, because each ASCII character takes one code unit and is one code point. Simply replacing an ASCII letter with many other single-code-unit characters would also leave that test unchanged. To see this indexing difference, put a character represented by two UTF-16 code units before the target. Then verify the text selected, including any spaces, instead of checking only that the result has the expected length. Both slices in our example contain two positions under their respective rules, and only one selects the intended quote.
Keep the underlying text intact
We also supplied overlapping ranges, an empty range and an end beyond the available text in a short string. The helper returned the highlighted fragments “bc”, “de” and “f” from “abcdef”. It trimmed overlapping portions it had already assigned, ignored the empty range and stopped at the end of the input. The output still used separate adjacent segments. For a related overlap case, we joined all the returned fragments and checked that the original string came back exactly. That is a useful second assertion. A helper can select the right quote and still accidentally duplicate or drop part of the surrounding text when it builds the segments.
With an empty list of ranges, the helper returned all the input as unhighlighted text. This case checks that absence of a range leaves the text available to read. It also makes the distinction between the text and its highlighting explicit: every segment has text, and the range determines which segments are marked. These cases describe the tested inputs. They leave open how a consumer ought to report malformed metadata or whether a producer should reject it before sending any range at all.
Decide what a range should preserve
Counting code points resolves the emoji example under the stated contract. It can still split a letter from a combining mark. In a local assertion, we supplied a decomposed e followed by a combining acute accent and an exclamation mark. Selecting the first code point highlighted only the e. That is correct for the code-point range we supplied, even though a reader may think of the base letter and its accent as one perceived character. A design that must keep such sequences together needs an additional rule about where selection boundaries may fall.
The local results show how the helper segmented supplied strings. A separate check would need to carry the original text and offsets from their producer through to the rendered highlight. That would answer whether both sides used the same text and unit, and whether the result looked right. The small slice test helps isolate the counting decision before those other things enter the picture.
← All engineering articles