parse method

  1. @override
SMSParsedResult? parse(
  1. Result result
)
override

Attempts to parse the raw Result's contents as a particular type of information (email, URL, etc.) and return a ParsedResult encapsulating the result of parsing.

@param theResult the raw Result to parse @return ParsedResult encapsulating the parsing result

Implementation

@override
SMSParsedResult? parse(Result result) {
  final rawText = ResultParser.getMassagedText(result);
  if (!(rawText.startsWith('sms:') ||
      rawText.startsWith('SMS:') ||
      rawText.startsWith('mms:') ||
      rawText.startsWith('MMS:'))) {
    return null;
  }

  // Check up front if this is a URI syntax string with query arguments
  final nameValuePairs = parseNameValuePairs(rawText);
  String? subject;
  String? body;
  bool querySyntax = false;
  if (nameValuePairs != null && nameValuePairs.isNotEmpty) {
    subject = nameValuePairs['subject'];
    body = nameValuePairs['body'];
    querySyntax = true;
  }

  // Drop sms, query portion
  final queryStart = rawText.indexOf('?', 4);
  String smsURIWithoutQuery;
  // If it's not query syntax, the question mark is part of the subject or message
  if (queryStart < 0 || !querySyntax) {
    smsURIWithoutQuery = rawText.substring(4);
  } else {
    smsURIWithoutQuery = rawText.substring(4, queryStart);
  }

  int lastComma = -1;
  int comma;
  final numbers = <String>[];
  final vias = <String>[];
  while (
      (comma = smsURIWithoutQuery.indexOf(',', lastComma + 1)) > lastComma) {
    final numberPart = smsURIWithoutQuery.substring(lastComma + 1, comma);
    _addNumberVia(numbers, vias, numberPart);
    lastComma = comma;
  }
  _addNumberVia(numbers, vias, smsURIWithoutQuery.substring(lastComma + 1));

  return SMSParsedResult(numbers.toList(), vias.toList(), subject, body);
}