splitLinkFlags function

List<String> splitLinkFlags(
  1. String line
)

Splits a linker flag line on whitespace, keeping quoted segments whole: "..." and '...' may contain spaces, a backslash escapes the next character. The surrounding quotes are stripped; an unterminated quote runs to the end of the line rather than dropping the flag.

Implementation

List<String> splitLinkFlags(String line) {
  final flags = <String>[];
  final current = StringBuffer();
  var inFlag = false;
  var quote = '';
  for (var i = 0; i < line.length; i++) {
    final ch = line[i];
    if (quote.isNotEmpty) {
      if (ch == quote) {
        quote = '';
      } else if (ch == r'\' && i + 1 < line.length) {
        current.write(line[++i]);
      } else {
        current.write(ch);
      }
    } else if (ch == '"' || ch == "'") {
      quote = ch;
      inFlag = true;
    } else if (ch == r'\' && i + 1 < line.length) {
      current.write(line[++i]);
      inFlag = true;
    } else if (_isFlagSpace(ch)) {
      if (inFlag) {
        flags.add(current.toString());
        current.clear();
        inFlag = false;
      }
    } else {
      current.write(ch);
      inFlag = true;
    }
  }
  if (inFlag) flags.add(current.toString());
  return flags;
}