process method

List<Rule> process(
  1. List<String> rules
)

Converts a list of strings into a list of rules.

Lines that contain comments (start with //) or are empty (i.e. zero-length or only contain whitespace characters) are removed completely.

Since publicsuffix.org defines rules as the part of a line before the first whitespace character, any text that follows after this is also removed.

Finally, all lines are changed to lower case to ease case-insensitive comparisons later.

The resulting list is returned as a list of Rules. Comments containing BEGIN PRIVATE and END PRIVATE can be used to indicate a section of rules that should be marked as isIcann = false ("private rules").

Implementation

List<Rule> process(List<String> rules) {
  var isIcann = true;
  var newList = <Rule>[];

  for (var line in rules) {
    if (_isComment(line)) {
      if (line.contains('BEGIN PRIVATE')) {
        isIcann = false;
      } else if (line.contains('END PRIVATE')) {
        isIcann = true;
      }
    } else if (!_isComment(line) && !_isEmpty(line)) {
      var firstSpace = line.indexOf(' ');

      if (firstSpace != -1) {
        line = line.substring(0, firstSpace).trim();
      }

      newList.add(Rule(line.toLowerCase(), isIcann: isIcann));
    }
  }

  return newList;
}