stripIndent function

String stripIndent(
  1. String line,
  2. int max
)

Remove up to max columns of leading indentation, returning the remainder.

Implementation

String stripIndent(String line, int max) {
  var removed = 0;
  var idx = 0;
  while (idx < line.length && removed < max) {
    final c = line.codeUnitAt(idx);
    if (c == _space) {
      removed += 1;
      idx += 1;
    } else if (c == _tab) {
      // A tab only partly inside the indent leaves the rest of its width
      // as spaces.
      if (removed + 4 > max) {
        return ' ' * (removed + 4 - max) + line.substring(idx + 1);
      }
      removed += 4;
      idx += 1;
    } else {
      break;
    }
  }
  return line.substring(idx);
}