Skip to content

Commit

Permalink
[Target] Switched from escaped spaces to quoted spaces.
Browse files Browse the repository at this point in the history
Instead of -attr=value\ with\ spaces, will instead be written as
-attr='value with spaces'.
  • Loading branch information
Lunderberg committed Aug 3, 2021
1 parent eb725aa commit 37b79ae
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
45 changes: 32 additions & 13 deletions src/target/target.cc
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,10 @@ static int FindFirstSubstr(const std::string& str, const std::string& substr) {
return pos == std::string::npos ? -1 : pos;
}

static Optional<String> JoinString(const std::vector<String>& array, char separator,
char escape = '\\') {
static Optional<String> JoinString(const std::vector<String>& array, char separator) {
char escape = '\\';
char quote = '\'';

if (array.empty()) {
return NullOpt;
}
Expand All @@ -160,18 +162,27 @@ static Optional<String> JoinString(const std::vector<String>& array, char separa
}

std::string str = array[i];
for (char c : str) {
if (c == separator) {
os << escape;

if ((str.find(separator) == std::string::npos) && (str.find(quote) == std::string::npos)) {
os << str;
} else {
os << quote;
for (char c : str) {
if (c == separator || c == quote) {
os << escape;
}
os << c;
}
os << c;
os << quote;
}
}
return String(os.str());
}

static std::vector<std::string> SplitString(const std::string& str, char separator,
char escape = '\\') {
static std::vector<std::string> SplitString(const std::string& str, char separator) {
char escape = '\\';
char quote = '\'';

std::vector<std::string> output;

const char* start = str.data();
Expand All @@ -188,18 +199,26 @@ static std::vector<std::string> SplitString(const std::string& str, char separat
}
};

bool pos_quoted = false;

while (pos < end) {
if ((*pos == escape) && (pos + 1 < end) && (pos[1] == separator)) {
current_word << separator;
pos += 2;
} else if (*pos == separator) {
if ((*pos == separator) && !pos_quoted) {
finish_word();
pos++;
} else if ((*pos == escape) && (pos + 1 < end) && (pos[1] == quote)) {
current_word << quote;
pos += 2;
} else if (*pos == quote) {
pos_quoted = !pos_quoted;
pos++;
} else {
current_word << *pos;
pos++;
}
}

ICHECK(!pos_quoted) << "Mismatched quotes '' in string";

finish_word();

return output;
Expand Down Expand Up @@ -276,7 +295,7 @@ ObjectRef TargetInternal::ParseType(const std::string& str,
// Parsing string, strip leading/trailing spaces
auto start = str.find_first_not_of(' ');
auto end = str.find_last_not_of(' ');
return String(str.substr(start, (end - start - 1)));
return String(str.substr(start, (end - start + 1)));

} else if (info.type_index == Target::ContainerType::_GetOrAllocRuntimeTypeIndex()) {
// Parsing target
Expand Down
2 changes: 1 addition & 1 deletion tests/python/unittest/test_target_target.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def test_target_string_parse():

def test_target_string_with_spaces():
target = tvm.target.Target(
"vulkan -device_name=Name\ of\ GPU\ with\ spaces -device_type=discrete"
"vulkan -device_name='Name of GPU with spaces' -device_type=discrete"
)
assert target.attrs["device_name"] == "Name of GPU with spaces"
assert target.attrs["device_type"] == "discrete"
Expand Down

0 comments on commit 37b79ae

Please sign in to comment.