findMatches function

List<_MatchedString> findMatches(
  1. String text,
  2. String types,
  3. bool humanize, {
  4. RegExp? regExp,
})

Implementation

List<_MatchedString> findMatches(String text, String types, bool humanize,
    {RegExp? regExp}) {
  List<_MatchedString> matched = [
    _MatchedString(type: MatchType.none, text: text)
  ];

  /// If there is phone number in the text it will be added
  /// to the highlighted string list
  if (types.contains('phone')) {
    List<_MatchedString> newMatched = [];
    for (_MatchedString matchedBefore in matched) {
      if (matchedBefore.type == MatchType.none) {
        newMatched
            .addAll(_findLinksByType(matchedBefore.text, MatchType.phone));
      } else
        newMatched.add(matchedBefore);
    }
    matched = newMatched;
  }

  /// If there is email in the text it will be added
  /// to the highlighted string list
  if (types.contains('email')) {
    List<_MatchedString> newMatched = [];
    for (_MatchedString matchedBefore in matched) {
      if (matchedBefore.type == MatchType.none) {
        newMatched.addAll(_findLinksByType(matchedBefore.text, MatchType.email,
            regExp: _emailRegExp));
      } else
        newMatched.add(matchedBefore);
    }
    matched = newMatched;
  }

  /// If there is web link in the text it will be added
  /// to the highlighted string list
  if (types.contains('web')) {
    List<_MatchedString> newMatched = [];
    for (_MatchedString matchedBefore in matched) {
      if (matchedBefore.type == MatchType.none) {
        final webMatches = _findLinksByType(matchedBefore.text, MatchType.link,
            regExp: _linksRegExp);
        for (_MatchedString webMatch in webMatches) {
          if (webMatch.type == MatchType.link &&
              (webMatch.text.startsWith('http://') ||
                  webMatch.text.startsWith('https://')) &&
              humanize) {
            newMatched.add(_MatchedString(
                text: webMatch.text
                    .substring(webMatch.text.startsWith('http://') ? 7 : 8),
                type: MatchType.link));
          } else {
            newMatched.add(webMatch);
          }
        }
      } else
        newMatched.add(matchedBefore);
    }
    matched = newMatched;
  }

  /// If there is any custom regExp provided then it will add the matching
  /// string to the highlighted string list
  if (types.contains('regExp')) {
    List<_MatchedString> newMatched = [];
    for (_MatchedString matchedBefore in matched) {
      if (matchedBefore.type == MatchType.none) {
        newMatched.addAll(_findLinksByType(
            matchedBefore.text, MatchType.customRegExp,
            regExp: regExp));
      } else
        newMatched.add(matchedBefore);
    }
    matched = newMatched;
  }

  return matched;
}