answeredBy method

({int end, int start, int total})? answeredBy(
  1. String? contentRange
)

What a Content-Range header (contentRange) says was sent, when it is exactly this part of the file — clipped to the file's end, as a server does; null when it is anything else.

Implementation

({int start, int end, int total})? answeredBy(String? contentRange) {
  final m = RegExp(
    r'^bytes (\d{1,18})-(\d{1,18})/(\d{1,18})$',
  ).firstMatch(contentRange?.trim() ?? '');
  if (m == null) return null;
  final first = int.parse(m[1]!);
  final last = int.parse(m[2]!);
  final total = int.parse(m[3]!);
  if (last < first || last >= total) return null;
  final suffix = _suffix;
  final int wantFirst;
  final int wantLast;
  if (suffix != null) {
    wantFirst = total > suffix ? total - suffix : 0;
    wantLast = total - 1;
  } else {
    wantFirst = _start!;
    wantLast = _end! < total ? _end : total - 1;
  }
  if (first != wantFirst || last != wantLast) return null;
  return (start: first, end: last, total: total);
}