std::optional parseTrampolineNames()

in xar/XarParser.cpp [60:99]


std::optional<XarParserError> parseTrampolineNames(
    const std::string& trampolineNames,
    std::vector<std::string>* ret) {
  if (trampolineNames.size() <= 2) {
    return XarParserError(
        XarParserErrorType::TRAMPOLINE_ERROR,
        "There must be at least one trampoline name. Trampoline names must be"
        "non-empty and surrounded by double quotes");
  }
  if (trampolineNames.front() != '\'' || trampolineNames.back() != '\'') {
    return XarParserError(
        XarParserErrorType::TRAMPOLINE_ERROR,
        "Expected first and last characters to be single quotes that wrap "
        "trampoline names");
  }
  // We have to be careful here to first trim the first and last ' before
  // splitting. Otherwise a ' ' trampoline name might not be handled properly.
  // e.g. consider "' ' 'tramp'"
  *ret = split("' '", trampolineNames.substr(1, trampolineNames.size() - 2));
  bool foundRequiredTrampoline = false;
  for (const auto& trampoline : *ret) {
    if (trampoline.find('\'') != std::string::npos ||
        trampoline.find('"') != std::string::npos) {
      return XarParserError(
          XarParserErrorType::TRAMPOLINE_ERROR,
          "Single or double quotes are not allowed in trampoline names. Maybe "
          "there is more than one space between names?");
    }
    if (trampoline == kGuaranteedTrampolineName) {
      foundRequiredTrampoline = true;
    }
  }
  if (!foundRequiredTrampoline) {
    return XarParserError(
        XarParserErrorType::TRAMPOLINE_ERROR,
        "Missing required trampoline name: " +
            std::string(kGuaranteedTrampolineName));
  }
  return {};
}