2
0
Fork 0
mirror of https://github.com/fish-shell/fish-shell synced 2025-01-15 06:24:01 +00:00

Simplify tokenize_variable_array()

This commit is contained in:
ridiculousfish 2018-03-05 22:05:05 -08:00
parent 37e748ad54
commit 3b2de931cd

View file

@ -111,29 +111,26 @@ static void init_curses();
/// This is used to convert a serialized env_var_t back into a list. It is used when reading legacy /// This is used to convert a serialized env_var_t back into a list. It is used when reading legacy
/// (fish 2.x) encoded vars stored in the universal variable file and the environment. /// (fish 2.x) encoded vars stored in the universal variable file and the environment.
static void tokenize_variable_array(const wcstring &val, wcstring_list_t &out) { static wcstring_list_t tokenize_variable_array(const wcstring &val) {
out.clear(); // ensure the output var is empty -- this will normally be a no-op
// Zero element arrays are externally encoded as this placeholder string. // Zero element arrays are externally encoded as this placeholder string.
if (val == ENV_NULL) return; if (val == ENV_NULL) return {};
wcstring_list_t out;
size_t pos = 0, end = val.size(); size_t pos = 0, end = val.size();
while (pos <= end) { while (pos <= end) {
size_t next_pos = val.find(ARRAY_SEP, pos); size_t next_pos = val.find(ARRAY_SEP, pos);
if (next_pos == wcstring::npos) { if (next_pos == wcstring::npos) {
next_pos = end; next_pos = end;
} }
out.resize(out.size() + 1); out.emplace_back(val, pos, next_pos - pos);
out.back().assign(val, pos, next_pos - pos);
pos = next_pos + 1; // skip the separator, or skip past the end pos = next_pos + 1; // skip the separator, or skip past the end
} }
return out;
} }
/// This is used to convert a serialized env_var_t back into a list. /// This is used to convert a serialized env_var_t back into a list.
wcstring_list_t decode_serialized(const wcstring &s) { wcstring_list_t decode_serialized(const wcstring &s) {
wcstring_list_t values; return tokenize_variable_array(s);
tokenize_variable_array(s, values);
return values;
} }
// Struct representing one level in the function variable stack. // Struct representing one level in the function variable stack.