SourceOffsetIndex constructor

SourceOffsetIndex(
  1. String source
)

Implementation

SourceOffsetIndex(String source) : utf16Length = source.length {
  int utf16Offset = 0;
  int utf8Offset = 0;
  while (utf16Offset < source.length) {
    final int first = source.codeUnitAt(utf16Offset);
    int codeUnitCount = 1;
    int byteCount;
    if (first <= 0x7f) {
      byteCount = 1;
    } else if (first <= 0x7ff) {
      byteCount = 2;
    } else if (first >= 0xd800 &&
        first <= 0xdbff &&
        utf16Offset + 1 < source.length &&
        source.codeUnitAt(utf16Offset + 1) >= 0xdc00 &&
        source.codeUnitAt(utf16Offset + 1) <= 0xdfff) {
      codeUnitCount = 2;
      byteCount = 4;
    } else {
      byteCount = 3;
    }
    utf16Offset += codeUnitCount;
    utf8Offset += byteCount;
    _utf16Offsets.add(utf16Offset);
    _utf8Offsets.add(utf8Offset);
  }
}