code: private int findLast(CharSequence input, int beginIndex) {
          boolean firstInSubDomain = true;
          boolean canEndSubDomain = false;
          int firstDot = -1;
          int last = -1;
          for (int i = beginIndex; i < input.length(); i++) {
              char c = input.charAt(i);
              if (firstInSubDomain) {
                  if (subDomainAllowed(c)) {
                      last = i;
                      firstInSubDomain = false;
                      canEndSubDomain = true;
                  } else {
                      break;
                  }
              } else {
                  if (c == '.') {
                      if (!canEndSubDomain) {
                          break;
                      }
                      firstInSubDomain = true;
                      if (firstDot == -1) {
                          firstDot = i;
                      }
                  } else if (c == '-') {
                      canEndSubDomain = false;
                  } else if (subDomainAllowed(c)) {
                      last = i;
                      canEndSubDomain = true;
                  } else {
                      break;
                  }
              }
          }
          if (domainMustHaveDot && (firstDot == -1 || firstDot > last)) {
              return -1;
          } else {
              return last;
          }
      }
docs: See "Domain" in RFC 5321, plus extension of "sub-domain" in RFC 6531