Clean up some env.cpp export code

Adopt some modern practices.
This commit is contained in:
ridiculousfish 2018-05-27 15:45:30 -07:00
parent eb0539af60
commit 5492a16719

View file

@ -1492,17 +1492,17 @@ static void get_exported(const env_node_t *n, var_table_t &h) {
} }
} }
// Given a map from key to value, add values to out of the form key=value. // Given a map from key to value, return a vector of strings of the form key=value
static void export_func(const var_table_t &envs, std::vector<std::string> &out) { static std::vector<std::string> get_export_list(const var_table_t &envs) {
out.reserve(out.size() + envs.size()); std::vector<std::string> result;
for (auto iter = envs.begin(); iter != envs.end(); ++iter) { result.reserve(envs.size());
const wcstring &key = iter->first; for (const auto &kv : envs) {
const std::string &ks = wcs2string(key); std::string ks = wcs2string(kv.first);
std::string vs = wcs2string(iter->second.as_string()); std::string vs = wcs2string(kv.second.as_string());
// Arrays in the value are ASCII record separator (0x1e) delimited. But some variables // Arrays in the value are ASCII record separator (0x1e) delimited. But some variables
// should have colons. Add those. // should have colons. Add those.
if (variable_is_colon_delimited_var(key)) { if (variable_is_colon_delimited_var(kv.first)) {
// Replace ARRAY_SEP with colon. // Replace ARRAY_SEP with colon.
std::replace(vs.begin(), vs.end(), (char)ARRAY_SEP, ':'); std::replace(vs.begin(), vs.end(), (char)ARRAY_SEP, ':');
} }
@ -1512,8 +1512,9 @@ static void export_func(const var_table_t &envs, std::vector<std::string> &out)
str.append(ks); str.append(ks);
str.append("="); str.append("=");
str.append(vs); str.append(vs);
out.push_back(std::move(str)); result.push_back(std::move(str));
} }
return result;
} }
void var_stack_t::update_export_array_if_necessary() { void var_stack_t::update_export_array_if_necessary() {
@ -1527,8 +1528,7 @@ void var_stack_t::update_export_array_if_necessary() {
if (uvars()) { if (uvars()) {
const wcstring_list_t uni = uvars()->get_names(true, false); const wcstring_list_t uni = uvars()->get_names(true, false);
for (size_t i = 0; i < uni.size(); i++) { for (const wcstring &key : uni) {
const wcstring &key = uni.at(i);
auto var = uvars()->get(key); auto var = uvars()->get(key);
if (!var.missing_or_empty()) { if (!var.missing_or_empty()) {
@ -1539,9 +1539,7 @@ void var_stack_t::update_export_array_if_necessary() {
} }
} }
std::vector<std::string> local_export_buffer; export_array.set(get_export_list(vals));
export_func(vals, local_export_buffer);
export_array.set(local_export_buffer);
has_changed_exported = false; has_changed_exported = false;
} }