capSubtreeText function

String capSubtreeText(
  1. String s
)

tokens is NOT sent: the backend rebuilds it as normalized_text.split(' ') minus empties — byte-identical to tokenise — whenever the key is absent.

Implementation

String capSubtreeText(String s) {
  if (s.length <= kSubtreeTextMaxChars) return s;
  final cut = s.lastIndexOf(' ', kSubtreeTextMaxChars);
  if (cut > 0) return s.substring(0, cut);
  var end = kSubtreeTextMaxChars;
  // A lone high surrogate is invalid UTF-16; jsonEncode/utf8 would mangle it.
  final last = s.codeUnitAt(end - 1);
  if (last >= 0xD800 && last <= 0xDBFF) end -= 1;
  return s.substring(0, end);
}