setextUnderlineLevel function

int? setextUnderlineLevel(
  1. String line
)

The level of a setext underline — 1 for ===, 2 for --- — or null.

Up to three spaces of indent, then one run of a single character, then only whitespace. Internal spaces (- - -) make it a thematic break, not an underline.

Implementation

int? setextUnderlineLevel(String line) {
  final indent = firstNonSpace(line);
  if (indent == -1 || indent > 3) {
    return null;
  }
  final first = line.codeUnitAt(indent);
  if (first != 0x3D /* = */ && first != 0x2D /* - */ ) {
    return null;
  }
  final t = line.trim();
  if (t.isEmpty) {
    return null;
  }
  final c = t.codeUnitAt(0);
  if (c != 0x3D /* = */ && c != 0x2D /* - */ ) {
    return null;
  }
  for (var i = 1; i < t.length; i++) {
    if (t.codeUnitAt(i) != c) {
      return null;
    }
  }
  return c == 0x3D ? 1 : 2;
}