3
0
Fork 0
mirror of https://github.com/YosysHQ/yosys synced 2025-10-09 17:31:59 +00:00

io.cc: Support single quoted arguments

Single quoted strings have no escape character and are treated verbatim.
This is useful for minimizing the number of backslashes in (for example) `logger -expect` regexps (e.g. `"\\\""` -> `'\"'`).
`Yosys::quote()` will use single quotes by default, unless the string contains a single quote and then it will use `std::quoted()`.

Fix strange behaviour arrising from always using `std::quoted(result)`.  Because of the way the function interacts with streams, if there is only one quotation mark then the result would end at the first whitespace.
This commit is contained in:
Krystine Sherwin 2025-09-24 17:24:40 +12:00
parent 6328c808db
commit d6f3ac60f1
No known key found for this signature in database

View file

@ -38,15 +38,16 @@ std::string next_token(std::string &text, const char *sep, bool long_strings)
if (pos_begin == std::string::npos)
pos_begin = text.size();
if (long_strings && pos_begin != text.size() && text[pos_begin] == '"') {
if (long_strings && pos_begin != text.size() && (text[pos_begin] == '"' || text[pos_begin] == '\'')) {
std::string sep_string = sep;
for (size_t i = pos_begin+1; i < text.size(); i++) {
if (text[i-1] != '\\' && text[i] == '"' && (i+1 == text.size() || sep_string.find(text[i+1]) != std::string::npos)) {
bool close_quote = (text[i] == text[pos_begin]) && (text[i-1] != '\\' || text[pos_begin] == '\'');
if (close_quote && (i+1 == text.size() || sep_string.find(text[i+1]) != std::string::npos)) {
std::string token = text.substr(pos_begin, i-pos_begin+1);
text = text.substr(i+1);
return token;
}
if (i+1 < text.size() && text[i] == '"' && text[i+1] == ';' && (i+2 == text.size() || sep_string.find(text[i+2]) != std::string::npos)) {
if (i+1 < text.size() && close_quote && text[i+1] == ';' && (i+2 == text.size() || sep_string.find(text[i+2]) != std::string::npos)) {
std::string token = text.substr(pos_begin, i-pos_begin+1);
text = text.substr(i+2);
return token + ";";
@ -594,7 +595,7 @@ void format_emit_void_ptr(std::string &result, std::string_view spec, int *dynam
}
bool needs_quote(const std::string &s) {
for (auto c : {' ', '\\', '#', ';', '"'}) {
for (auto c : {' ', '\\', '#', ';', '"', '\''}) {
if (s.find(c) != std::string::npos) return true;
}
return false;
@ -602,15 +603,25 @@ bool needs_quote(const std::string &s) {
std::string quote(const std::string &s) {
std::ostringstream ss;
ss << std::quoted(s);
if (s.find('\'') != std::string::npos)
ss << std::quoted(s);
else
ss << '\'' << s << '\'';
return ss.str();
}
std::string unquote(const std::string &s) {
std::string result;
std::istringstream ss(s);
ss >> std::quoted(result);
return result;
if (s.length() >= 2) {
if (s.front() == '\'' && s.back() == '\'')
return s.substr(1, s.length()-2);
else if (s.front() == '"' && s.back() == '"') {
std::string result;
std::istringstream ss(s);
ss >> std::quoted(result);
return result;
}
}
return s;
}
YOSYS_NAMESPACE_END