byteOffsetAtUtf16 method

  1. @override
int byteOffsetAtUtf16(
  1. int utf16Offset
)
override

Implementation

@override
int byteOffsetAtUtf16(int utf16Offset) {
  RangeError.checkValueInInterval(
    utf16Offset,
    0,
    _utf16Length,
    'utf16Offset',
  );
  var low = 0;
  var high = _lineUtf16Starts.length;
  while (low < high) {
    final middle = low + ((high - low) >> 1);
    if (_lineUtf16Starts[middle] <= utf16Offset) {
      low = middle + 1;
    } else {
      high = middle;
    }
  }
  final row = low - 1;
  var currentUtf16 = _lineUtf16Starts[row];
  var byteOffset = _lineStarts[row];
  while (currentUtf16 < utf16Offset) {
    final first = byteAt(byteOffset);
    final byteWidth = _utf8SequenceLength(first);
    final utf16Width = first >= 0xf0 ? 2 : 1;
    if (currentUtf16 + utf16Width > utf16Offset) {
      throw RangeError('UTF-16 offset splits a surrogate pair');
    }
    currentUtf16 += utf16Width;
    byteOffset += byteWidth;
  }
  return byteOffset;
}