trimMargin method

String trimMargin([
  1. String marginPrefix = '|'
])

Returns _input, but trims leading whitespace characters followed by the given marginPrefix from each line.

Also removes the first and last lines if they are blank, i.e. they only contain whitespace characters.

For example, given that the marginPrefix is "|" (the default), the input:

  |   Hello
  | there
  |    World

will become:

Hello there World

Leaves lines that don't contain marginPrefix untouched.

Implementation

String trimMargin([String marginPrefix = '|']) {
  if (_inputIsBlank()) return _input;

  final lines = LineSplitter.split(_input);
  final buffer = StringBuffer();
  var i = -1;

  for (final line in lines) {
    i++;

    var result = line;
    final leftTrimmedLine = line.trimLeft();

    if ((i == 0 || i == lines.length - 1) &&
        leftTrimmedLine.trimRight().isEmpty) {
      // If this is the first or the last line, and it's just whitespace, we
      // want to skip it.
      continue;
    }

    if (leftTrimmedLine.length <= line.length) {
      if (leftTrimmedLine.startsWith(marginPrefix)) {
        result = leftTrimmedLine.replaceFirst(marginPrefix, '');
      }
    }

    buffer.writeln(result);
  }

  return buffer.toString();
}