wcstring expand_escape_variable(const env_var_t &var) { wcstring buff; wcstring_list_t lst; var.to_list(lst); if (lst.size() == 0) { ; // empty list expands to nothing } else if (lst.size() == 1) { const wcstring &el = lst.at(0); if (el.find(L' ') != wcstring::npos && is_quotable(el)) { buff.append(L"'"); buff.append(el); buff.append(L"'"); } else { buff.append(escape_string(el, 1)); } } else { for (size_t j = 0; j < lst.size(); j++) { const wcstring &el = lst.at(j); if (j) buff.append(L" "); if (is_quotable(el)) { buff.append(L"'"); buff.append(el); buff.append(L"'"); } else { buff.append(escape_string(el, 1)); } } } return buff; }
wcstring expand_escape_variable(const wcstring &in) { wcstring_list_t lst; wcstring buff; tokenize_variable_array(in, lst); switch (lst.size()) { case 0: buff.append(L"''"); break; case 1: { const wcstring &el = lst.at(0); if (el.find(L' ') != wcstring::npos && is_quotable(el)) { buff.append(L"'"); buff.append(el); buff.append(L"'"); } else { buff.append(escape_string(el, 1)); } break; } default: { for (size_t j=0; j<lst.size(); j++) { const wcstring &el = lst.at(j); if (j) buff.append(L" "); if (is_quotable(el)) { buff.append(L"'"); buff.append(el); buff.append(L"'"); } else { buff.append(escape_string(el, 1)); } } } } return buff; }
/// Test if the specified string does not contain character which can not be used inside a quoted /// string. static int is_quotable(const wchar_t *str) { switch (*str) { case 0: { return 1; } case L'\n': case L'\t': case L'\r': case L'\b': case L'\x1B': { return 0; } default: { return is_quotable(str + 1); } } return 0; }
/** Test if the specified string does not contain character which can not be used inside a quoted string. */ static int is_quotable(const wchar_t *str) { switch (*str) { case 0: return 1; case L'\n': case L'\t': case L'\r': case L'\b': case L'\x1b': return 0; default: return is_quotable(str+1); } return 0; }
static int is_quotable(const wcstring &str) { return is_quotable(str.c_str()); }