patternIgnoreCase function Null safety

Parser<String> patternIgnoreCase(
  1. String element,
  2. [String? message]
)

Returns a parser that accepts a single character of a given case-insensitive character set provided as a string.

Characters match themselves. A dash - between two characters matches the range of those characters. A caret ^ at the beginning negates the pattern.

For example, the parser patternIgnoreCase('aoU') accepts the character 'a', 'o', 'u' and 'A', 'O', 'U', and fails for any other input. The parser patternIgnoreCase('a-c') accepts 'a', 'b', 'c' and 'A', 'B', 'C'; and fails for any other character. The parser `patternIgnoreCase('^A') accepts any character, but fails for the characters 'a' or 'A'.

Implementation

Parser<String> patternIgnoreCase(String element, [String? message]) {
  var normalized = element;
  final isNegated = normalized.startsWith('^');
  if (isNegated) {
    normalized = normalized.substring(1);
  }
  final isDashed = normalized.endsWith('-');
  if (isDashed) {
    normalized = normalized.substring(0, normalized.length - 1);
  }
  return pattern(
      '${isNegated ? '^' : ''}'
      '${normalized.toLowerCase()}${normalized.toUpperCase()}'
      '${isDashed ? '-' : ''}',
      message ?? '[${toReadableString(element)}] (case-insensitive) expected');
}