static inline void StripSpaces(llvm::StringRef &Str) { while (!Str.empty() && isspace(Str[0])) Str = Str.substr(1); while (!Str.empty() && isspace(Str.back())) Str = Str.substr(0, Str.size()-1); }
static bool IsValidBasename(const llvm::StringRef &basename) { // Check that the basename matches with the following regular expression or is // an operator name: // "^~?([A-Za-z_][A-Za-z_0-9]*)(<.*>)?$" // We are using a hand written implementation because it is significantly more // efficient then // using the general purpose regular expression library. size_t idx = 0; if (basename.size() > 0 && basename[0] == '~') idx = 1; if (basename.size() <= idx) return false; // Empty string or "~" if (!std::isalpha(basename[idx]) && basename[idx] != '_') return false; // First charater (after removing the possible '~'') isn't in // [A-Za-z_] // Read all characters matching [A-Za-z_0-9] ++idx; while (idx < basename.size()) { if (!std::isalnum(basename[idx]) && basename[idx] != '_') break; ++idx; } // We processed all characters. It is a vaild basename. if (idx == basename.size()) return true; // Check for basename with template arguments // TODO: Improve the quality of the validation with validating the template // arguments if (basename[idx] == '<' && basename.back() == '>') return true; // Check if the basename is a vaild C++ operator name if (!basename.startswith("operator")) return false; static RegularExpression g_operator_regex( llvm::StringRef("^(operator)( " "?)([A-Za-z_][A-Za-z_0-9]*|\\(\\)|" "\\[\\]|[\\^<>=!\\/" "*+-]+)(<.*>)?(\\[\\])?$")); std::string basename_str(basename.str()); return g_operator_regex.Execute(basename_str, nullptr); }
Status OptionValueString::SetValueFromString(llvm::StringRef value, VarSetOperationType op) { Status error; std::string value_str = value.str(); value = value.trim(); if (value.size() > 0) { switch (value.front()) { case '"': case '\'': { if (value.size() <= 1 || value.back() != value.front()) { error.SetErrorString("mismatched quotes"); return error; } value = value.drop_front().drop_back(); } break; } value_str = value.str(); } switch (op) { case eVarSetOperationInvalid: case eVarSetOperationInsertBefore: case eVarSetOperationInsertAfter: case eVarSetOperationRemove: if (m_validator) { error = m_validator(value_str.c_str(), m_validator_baton); if (error.Fail()) return error; } error = OptionValue::SetValueFromString(value, op); break; case eVarSetOperationAppend: { std::string new_value(m_current_value); if (value.size() > 0) { if (m_options.Test(eOptionEncodeCharacterEscapeSequences)) { std::string str; Args::EncodeEscapeSequences(value_str.c_str(), str); new_value.append(str); } else new_value.append(value); } if (m_validator) { error = m_validator(new_value.c_str(), m_validator_baton); if (error.Fail()) return error; } m_current_value.assign(new_value); NotifyValueChanged(); } break; case eVarSetOperationClear: Clear(); NotifyValueChanged(); break; case eVarSetOperationReplace: case eVarSetOperationAssign: if (m_validator) { error = m_validator(value_str.c_str(), m_validator_baton); if (error.Fail()) return error; } m_value_was_set = true; if (m_options.Test(eOptionEncodeCharacterEscapeSequences)) { Args::EncodeEscapeSequences(value_str.c_str(), m_current_value); } else { SetCurrentValue(value_str); } NotifyValueChanged(); break; } return error; }
bool needsQuotes(const llvm::StringRef &arg) { return // not already quoted !(arg.size() > 1 && arg[0] == '"' && arg.back() == '"') && // empty or min 1 space or min 1 double quote (arg.empty() || arg.find(' ') != arg.npos || arg.find('"') != arg.npos); }
static inline void RStrip(llvm::StringRef &Str, char c) { if (!Str.empty() && Str.back() == c) Str = Str.substr(0, Str.size()-1); }