Skip to content

Main content

Unicode Escape Source Offset Shift

Shift an explicit source UTF-16 code-unit offset by a supplied signed source-length change without interpreting escape sequences.

Loading tool…

Description

Shift a Unicode-source offset by a supplied number of source code units.

Unicode text has several distinct length and position bases: UTF-16 code units, code points, grapheme clusters and rendered glyphs. An edit offset in source text need not equal an offset in decoded text. Unicode properties also belong to a specific version; recording a property value is distinct from looking it up or validating it.

Shift a Unicode-source offset by a supplied number of source code units.

When to use this tool

Use Unicode Escape Source Offset Shift when you already have sourceOffset, insertedSourceCodeUnits and need to shift a Unicode-source offset by a supplied number of source code units.

How to use the tool and how it works

  1. Enter sourceOffset, insertedSourceCodeUnits. Use the units and conventions below.
  2. Run the tool. Correct any format, range, or cross-field input error before interpreting the output.
  3. Read shiftedOffset. Use the output convention below.

Require nonnegative safe whole sourceOffset and signed safe whole insertedSourceCodeUnits, with exact BigInt sum>=0; export the sum as a number.1

UTF-16 represents supplementary code points with two code units. Source offsets must retain their code-unit basis rather than being treated as decoded-character counts.2

Input fieldMeaning and unitsAccepted input
sourceOffsetSource OffsetRequired. Safe integer; minimum 0.
insertedSourceCodeUnitsSigned change in source UTF-16 code units, not decoded characters.Required. Safe integer.
Output fieldInterpretationReturned shape
shiftedOffsetNonnegative safe integer after exact addition; an unsafe output is rejected.Required. Safe integer.
json
{
  "input": {
    "sourceOffset": 12,
    "insertedSourceCodeUnits": 5
  },
  "output": {
    "shiftedOffset": 17
  }
}

Limitations and assumptions

  • Offsets count source UTF-16 code units, not decoded characters or code points. An output beyond the safe-integer range is rejected.
  • Inputs must satisfy both the field types and the cross-field conditions above. Invalid input causes an error; a successful false flag or documented placeholder is a different result.
  • Numeric calculations use binary floating point unless exact decimal-integer arithmetic is explicitly stated. No hidden measurement tolerance or decimal rounding is added; nonfinite or out-of-contract results are rejected. Numeric signed-zero results are exported as zero.

Alternative or Complementary methods

Use a Unicode property database for the stated version when an actual property lookup is needed. For source edits, map offsets in the original text and keep UTF-16 positions separate from decoded code points, grapheme boundaries and display widths.

References

  1. Calculation and input contract: text/unicode-escape-source-offset-shift — tool.io

  2. Unicode 17.0: characters and encoding forms — Unicode Consortium

Similar or alternative tools