isZwsBetweenCjk function

bool isZwsBetweenCjk(
  1. String text,
  2. int offset
)

Returns true if the code unit at offset in text is a zero-width space (U+200B) that sits between two CJK characters. In that case the ZWS should not produce a visible caret stop — the cursor should glide from one CJK character to the next without stopping on the invisible separator.

Implementation

bool isZwsBetweenCjk(String text, int offset) {
  if (offset < 0 || offset >= text.length) return false;
  if (text.codeUnitAt(offset) != 0x200B) return false;
  // Check the previous character (skip surrogate pairs)
  int prev = offset - 1;
  if (prev < 0) return false;
  // If prev is a low surrogate, step back one more to the high surrogate
  if ((text.codeUnitAt(prev) & 0xFC00) == 0xDC00) prev--;
  if (prev < 0) return false;
  // Check the next character (skip surrogate pairs)
  int next = offset + 1;
  if (next >= text.length) return false;
  // If next is a high surrogate, step forward one more to include the pair
  if ((text.codeUnitAt(next) & 0xFC00) == 0xD800) next++;
  if (next >= text.length) return false;
  return isCjk(text.codeUnitAt(prev)) && isCjk(text.codeUnitAt(next));
}