offsetAt method

int offsetAt(
  1. Position position
)

Converts a Position into a UTF-16 offset (clamped to the document).

Implementation

int offsetAt(Position position) {
  final line = position.line;
  if (line < 0) return 0;
  if (line >= _lineStarts.length) return text.length;
  final base = _lineStarts[line];
  final lineEnd = line + 1 < _lineStarts.length
      ? _lineStarts[line + 1]
      : text.length;
  final offset = base + position.character;
  if (offset < base) return base;
  if (offset > lineEnd) return lineEnd;
  return offset;
}