Add a variant of valid_var_name which accepts const wchar_t *

This avoids creating some unnecessary strings.
This commit is contained in:
ridiculousfish 2021-02-13 18:47:50 -08:00
parent d5ac8a01b6
commit ee15bc2a36
2 changed files with 10 additions and 1 deletions

View file

@ -1875,7 +1875,15 @@ bool valid_var_name_char(wchar_t chr) { return fish_iswalnum(chr) || chr == L'_'
/// Test if the given string is a valid variable name.
bool valid_var_name(const wcstring &str) {
return std::find_if_not(str.begin(), str.end(), valid_var_name_char) == str.end();
// Note do not use c_str(), we want to fail on embedded nul bytes.
return std::all_of(str.begin(), str.end(), valid_var_name_char);
}
bool valid_var_name(const wchar_t *str) {
for (size_t i = 0; str[i] != L'\0'; i++) {
if (!valid_var_name_char(str[i])) return false;
}
return true;
}
/// Test if the string is a valid function name.

View file

@ -638,6 +638,7 @@ std::string get_path_to_tmp_dir();
bool valid_var_name_char(wchar_t chr);
bool valid_var_name(const wcstring &str);
bool valid_var_name(const wchar_t *str);
bool valid_func_name(const wcstring &str);
// Return values (`$status` values for fish scripts) for various situations.