convert method

  1. @override
List<String> convert(
  1. String? input
)
override

Converts input and returns the result of the conversion.

Implementation

@override
List<String> convert(String? input) {
  if (input == null || input.isEmpty) {
    return [];
  }

  final List<String> result = [];

  var current = "";

  String? inQuote;
  bool lastTokenHasBeenQuoted = false;

  for (int index = 0; index < input.length; index++) {
    final token = input[index];

    if (inQuote != null) {
      if (token == inQuote) {
        lastTokenHasBeenQuoted = true;
        inQuote = null;
      } else {
        current += token;
      }
    } else {
      switch (token) {
        case "'": // '
        case '"': // ""
          inQuote = token;
          continue;
        case " ": // space
          if (lastTokenHasBeenQuoted || current.isNotEmpty) {
            result.add(current);
            current = "";
          }
          break;
        default:
          current += token;
          lastTokenHasBeenQuoted = false;
      }
    }
  }

  if (lastTokenHasBeenQuoted || current.isNotEmpty) {
    result.add(current);
  }

  if (inQuote != null) {
    throw Exception("Unbalanced quote $inQuote in input:\n$input");
  }

  return result;
}