Avoid appending chars one by one in some parsing functions in CommandManager

This commit is contained in:
Maxime Coste 2015-07-25 08:56:27 +01:00
parent 4a20882a8d
commit 6bed464105

View File

@ -82,20 +82,25 @@ bool is_command_separator(char c)
String get_until_delimiter(StringView base, ByteCount& pos, char delimiter) String get_until_delimiter(StringView base, ByteCount& pos, char delimiter)
{ {
const ByteCount length = base.length(); const ByteCount length = base.length();
ByteCount beg = pos;
String str; String str;
while (pos < length) while (pos < length)
{ {
char c = base[pos]; char c = base[pos];
if (c == delimiter) if (c == delimiter)
{ {
if (base[pos-1] != '\\') str += base.substr(beg, pos - beg);
break; if (pos != 0 and base[pos-1] == '\\')
str.back() = delimiter; {
str.back() = delimiter;
beg = pos+1;
}
else
return str;
} }
else
str += c;
++pos; ++pos;
} }
str += base.substr(beg, pos - beg);
return str; return str;
} }
@ -300,27 +305,31 @@ String expand(StringView str, const Context& context,
const EnvVarMap& env_vars) const EnvVarMap& env_vars)
{ {
String res; String res;
auto pos = 0_byte; auto pos = 0_byte, beg = 0_byte;
auto length = str.length(); auto length = str.length();
while (pos < length) while (pos < length)
{ {
if (str[pos] == '\\') if (str[pos] == '\\')
{ {
char c = str[++pos]; char c = str[++pos];
if (c != '%' and c != '\\') if (c == '%' or c == '\\')
res += '\\'; {
res += c; res += str.substr(beg, pos - beg);
++pos; res.back() = c;
beg = ++pos;
}
} }
else if (str[pos] == '%') else if (str[pos] == '%')
{ {
res += str.substr(beg, pos - beg);
Token token = parse_percent_token<true>(str, pos); Token token = parse_percent_token<true>(str, pos);
res += expand_token(token, context, shell_params, env_vars); res += expand_token(token, context, shell_params, env_vars);
++pos; beg = ++pos;
} }
else else
res += str[pos++]; ++pos;
} }
res += str.substr(beg, pos - beg);
return res; return res;
} }