validate static method

bool validate(
  1. String? email, [
  2. bool allowTopLevelDomains = false,
  3. bool allowInternational = true
])

Validate the specified email address.

Implementation

static bool validate(String? email,
    [bool allowTopLevelDomains = false, bool allowInternational = true]) {
  _index = 0;

  if (email == null) {
    return false;
  }

  if (email.isEmpty || email.length >= 255) {
    return false;
  }
  if (email[_index] == '"') {
    if (!_skipQuoted(email, allowInternational) || _index >= email.length) {
      return false;
    }
  } else {
    if (!_skipAtom(email, allowInternational) || _index >= email.length) {
      return false;
    }

    while (email[_index] == '.') {
      _index++;

      if (_index >= email.length) {
        return false;
      }

      if (!_skipAtom(email, allowInternational)) {
        return false;
      }

      if (_index >= email.length) {
        return false;
      }
    }
  }

  if (_index + 1 >= email.length || _index > 64 || email[_index++] != '@') {
    return false;
  }

  if (email[_index] != '[') {
    // domain
    if (!_skipDomain(email, allowTopLevelDomains, allowInternational)) {
      return false;
    }

    return _index == email.length;
  }

  // address literal
  _index++;

  // we need at least 8 more characters
  if (_index + 8 >= email.length) {
    return false;
  }

  final ipv6 = email.substring(_index - 1).toLowerCase();

  if (ipv6.contains('ipv6:')) {
    _index += 'IPv6:'.length;
    if (!_skipIPv6Literal(email)) {
      return false;
    }
  } else {
    if (!_skipIPv4Literal(email)) {
      return false;
    }
  }

  if (_index >= email.length || email[_index++] != ']') {
    return false;
  }

  return _index == email.length;
}