findUnquotedChar function

int findUnquotedChar(
  1. String content,
  2. String char, [
  3. int start = 0
])

Finds the index of a specific character outside of quoted sections.

@param content The string to search in @param char The character to look for @param start Optional starting index (defaults to 0) @returns The index of the character, or -1 if not found outside quotes

Implementation

int findUnquotedChar(String content, String char, [int start = 0]) {
  bool inQuotes = false;
  int i = start;

  while (i < content.length) {
    if (content[i] == backslash && i + 1 < content.length && inQuotes) {
      // Skip escaped character
      i += 2;
      continue;
    }

    if (content[i] == doubleQuote) {
      inQuotes = !inQuotes;
      i++;
      continue;
    }

    if (content[i] == char && !inQuotes) {
      return i;
    }

    i++;
  }

  return -1;
}