wordWrap function

String wordWrap(
  1. String s,
  2. int maxLineLength, {
  3. int hangingIndent = 0,
})

Word wraps a string to a maximum line length.

Existing newlines in the original string will be preserved. Lines longer than the maximum line length will be split at whitespace nearest to but not beyond the maximum line length. If there is no preceding whitespace in the line, then the line will be split at the maximum line length.

maxLineLength must be positive.

hangingIndent specifies how many spaces to indent all but the first line. It must be non-negative and strictly less than maxLineLength.

Implementation

String wordWrap(
  String s,
  int maxLineLength, {
  int hangingIndent = 0,
}) {
  assert(maxLineLength > 0);
  assert(hangingIndent >= 0);
  assert(hangingIndent < maxLineLength);

  var lines = s.split('\n');
  var result = <String>[];

  var wordBreakRegExp = RegExp(r'\s');
  var hangingIndentString = ' ' * hangingIndent;

  var i = 0;
  var indentString = '';
  var j = 0;
  while (i < lines.length && j < 20) {
    j += 1;
    var line = '$indentString${lines[i]}';
    indentString = hangingIndentString;

    if (line.length <= maxLineLength) {
      result.add(line);
      i += 1;
      continue;
    }

    var breakIndex = line.lastIndexOf(wordBreakRegExp, maxLineLength);
    if (breakIndex < hangingIndent) {
      breakIndex = maxLineLength;
    }
    result.add(line.substring(0, breakIndex).trimRight());
    lines[i] = line.substring(breakIndex).trimLeft();
  }

  return result.join('\n');
}