From d837eee09d481b7f47863e63d50d404c8f78f7ea Mon Sep 17 00:00:00 2001 From: Aaron Gyes Date: Thu, 14 Mar 2019 15:12:14 -0700 Subject: [PATCH] remove some wcstring -> wchar_t* -> wcstring conversions Mostly related to usage _(L"foo"), keeping in mind the _ macro does a wcstring().c_str() already. And a smattering of other trivial micro-optimizations certain to not help tangibly. --- src/builtin.cpp | 4 ++-- src/builtin.h | 2 +- src/builtin_argparse.cpp | 3 +-- src/builtin_complete.cpp | 2 +- src/builtin_math.cpp | 4 ++-- src/builtin_string.cpp | 2 +- src/common.cpp | 3 +-- src/complete.cpp | 6 +++--- src/env.cpp | 4 ++-- src/expand.h | 1 + src/fish_tests.cpp | 4 ++-- src/history.cpp | 4 ++-- src/output.cpp | 14 +++++++------- src/parse_tree.cpp | 2 +- src/tokenizer.cpp | 5 +++-- src/tokenizer.h | 2 +- src/wildcard.cpp | 6 ++---- src/wutil.cpp | 4 ++-- 18 files changed, 35 insertions(+), 37 deletions(-) diff --git a/src/builtin.cpp b/src/builtin.cpp index 76e235813..f7103a0bf 100644 --- a/src/builtin.cpp +++ b/src/builtin.cpp @@ -545,8 +545,8 @@ void builtin_get_names(std::vector *list) { } /// Return a one-line description of the specified builtin. -wcstring builtin_get_desc(const wcstring &name) { - wcstring result; +const wchar_t *builtin_get_desc(const wcstring &name) { + const wchar_t *result; const builtin_data_t *builtin = builtin_lookup(name); if (builtin) { result = _(builtin->desc); diff --git a/src/builtin.h b/src/builtin.h index e9fcd2c78..8e5fd5e18 100644 --- a/src/builtin.h +++ b/src/builtin.h @@ -83,7 +83,7 @@ proc_status_t builtin_run(parser_t &parser, int job_pgrp, wchar_t **argv, io_str wcstring_list_t builtin_get_names(); void builtin_get_names(std::vector *list); -wcstring builtin_get_desc(const wcstring &b); +const wchar_t *builtin_get_desc(const wcstring &b); /// Support for setting and removing transient command lines. This is used by /// 'complete -C' in order to make the commandline builtin operate on the string diff --git a/src/builtin_argparse.cpp b/src/builtin_argparse.cpp index 1e87a03a4..4490ae7d2 100644 --- a/src/builtin_argparse.cpp +++ b/src/builtin_argparse.cpp @@ -448,8 +448,7 @@ static int validate_arg(parser_t &parser, const argparse_cmd_opts_t &opts, optio if (is_long_flag) { vars.set_one(var_name_prefix + L"name", ENV_LOCAL, opt_spec->long_flag); } else { - vars.set_one(var_name_prefix + L"name", ENV_LOCAL, - wcstring(1, opt_spec->short_flag).c_str()); + vars.set_one(var_name_prefix + L"name", ENV_LOCAL, wcstring(1, opt_spec->short_flag)); } vars.set_one(var_name_prefix + L"value", ENV_LOCAL, woptarg); diff --git a/src/builtin_complete.cpp b/src/builtin_complete.cpp index 438a33bab..c96571210 100644 --- a/src/builtin_complete.cpp +++ b/src/builtin_complete.cpp @@ -49,7 +49,7 @@ static void builtin_complete_add2(const wchar_t *cmd, int cmd_type, const wchar_ comp, desc, flags); } - if (old_opt.empty() && gnu_opt.empty() && std::wcslen(short_opt) == 0) { + if (old_opt.empty() && gnu_opt.empty() && short_opt[0] == L'\0') { complete_add(cmd, cmd_type, wcstring(), option_type_args_only, result_mode, condition, comp, desc, flags); } diff --git a/src/builtin_math.cpp b/src/builtin_math.cpp index 7798fe1a8..fdbe0a980 100644 --- a/src/builtin_math.cpp +++ b/src/builtin_math.cpp @@ -129,7 +129,7 @@ static const wchar_t *math_get_arg(int *argidx, wchar_t **argv, wcstring *storag return math_get_arg_argv(argidx, argv); } -static wcstring math_describe_error(te_error_t& error) { +static const wchar_t *math_describe_error(te_error_t& error) { if (error.position == 0) return L"NO ERROR?!?"; switch(error.type) { @@ -210,7 +210,7 @@ static int evaluate_expression(const wchar_t *cmd, parser_t &parser, io_streams_ streams.out.push_back(L'\n'); } } else { - streams.err.append_format(L"%ls: Error: %ls\n", cmd, math_describe_error(error).c_str()); + streams.err.append_format(L"%ls: Error: %ls\n", cmd, math_describe_error(error)); streams.err.append_format(L"'%ls'\n", expression.c_str()); streams.err.append_format(L"%*ls%ls\n", error.position - 1, L" ",L"^"); retval = STATUS_CMD_ERROR; diff --git a/src/builtin_string.cpp b/src/builtin_string.cpp index a62392299..a136b1fb2 100644 --- a/src/builtin_string.cpp +++ b/src/builtin_string.cpp @@ -1008,13 +1008,13 @@ bool regex_replacer_t::replace_matches(const wcstring &arg) { } } - wcstring outstr(output, outlen); bool rc = true; if (pcre2_rc < 0) { string_error(streams, _(L"%ls: Regular expression substitute error: %ls\n"), argv0, pcre2_strerror(pcre2_rc).c_str()); rc = false; } else { + wcstring outstr(output, outlen); bool replacement_occurred = pcre2_rc > 0; if (!opts.quiet && (!opts.filter || replacement_occurred)) { streams.out.append(outstr); diff --git a/src/common.cpp b/src/common.cpp index 41d7ee3e0..3728ff269 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -1484,8 +1484,7 @@ static bool unescape_string_internal(const wchar_t *const input, const size_t in if (unescape_special && input_position == 0 && !std::wcscmp(input, PROCESS_EXPAND_SELF_STR)) { to_append_or_none = PROCESS_EXPAND_SELF; - input_position += - std::wcslen(PROCESS_EXPAND_SELF_STR) - 1; // skip over 'self' part. + input_position += PROCESS_EXPAND_SELF_STR_LEN - 1; // skip over 'self's } break; } diff --git a/src/complete.cpp b/src/complete.cpp index 30bab6506..481cff7d5 100644 --- a/src/complete.cpp +++ b/src/complete.cpp @@ -508,7 +508,7 @@ static void parse_cmd_string(const wcstring &str, wcstring *path, wcstring *cmd, const environment_t &vars) { if (!path_get_path(str, path, vars)) { /// Use the empty string as the 'path' for commands that can not be found. - *path = L""; + path->clear(); } // Make sure the path is not included in the command. @@ -1150,8 +1150,8 @@ bool completer_t::complete_variable(const wcstring &str, size_t start_offset) { auto var = vars.get(env_name); if (!var) continue; - wcstring value = expand_escape_variable(*var); if (this->type() != COMPLETE_AUTOSUGGEST) { + wcstring value = expand_escape_variable(*var); desc = format_string(COMPLETE_VAR_DESC_VAL, value.c_str()); } } @@ -1493,7 +1493,7 @@ void completer_t::perform() { bool cursor_in_whitespace = !plain_statement.location_in_or_at_end_of_source_range(pos); if (cursor_in_whitespace) { - current_argument = L""; + current_argument.clear(); previous_argument = matching_arg; } else { current_argument = matching_arg; diff --git a/src/env.cpp b/src/env.cpp index 40bc26b34..3cfe58715 100644 --- a/src/env.cpp +++ b/src/env.cpp @@ -481,7 +481,7 @@ static void update_fish_color_support(const environment_t &vars) { debug(2, L"256 color support enabled for TERM=%ls", term.c_str()); } else if (term.find(L"xterm") != wcstring::npos) { // Assume that all 'xterm's can handle 256, except for Terminal.app from Snow Leopard - wcstring term_program, term_version; + wcstring term_program; if (auto tp = vars.get(L"TERM_PROGRAM")) term_program = tp->as_string(); if (auto tpv = vars.get(L"TERM_PROGRAM_VERSION")) { if (term_program == L"Apple_Terminal" && @@ -821,7 +821,7 @@ static void handle_fish_history_change(const wcstring &op, const wcstring &var_n env_stack_t &vars) { UNUSED(op); UNUSED(var_name); - reader_change_history(history_session_id(vars).c_str()); + reader_change_history(history_session_id(vars)); } static void handle_function_path_change(const wcstring &op, const wcstring &var_name, diff --git a/src/expand.h b/src/expand.h index 8dd0da247..2386f45c4 100644 --- a/src/expand.h +++ b/src/expand.h @@ -102,6 +102,7 @@ enum expand_error_t { /// The string represented by PROCESS_EXPAND_SELF #define PROCESS_EXPAND_SELF_STR L"%self" +#define PROCESS_EXPAND_SELF_STR_LEN 5 /// Perform various forms of expansion on in, such as tilde expansion (\~USER becomes the users home /// directory), variable expansion (\$VAR_NAME becomes the value of the environment variable diff --git a/src/fish_tests.cpp b/src/fish_tests.cpp index 217753c82..52692f0bc 100644 --- a/src/fish_tests.cpp +++ b/src/fish_tests.cpp @@ -2668,7 +2668,7 @@ static void test_complete() { do_test((completions.at(1).flags & COMPLETE_NO_SPACE) != 0); // Test wraps. - do_test(comma_join(complete_get_wrap_targets(L"wrapper1")) == L""); + do_test(comma_join(complete_get_wrap_targets(L"wrapper1")).empty()); complete_add_wrapper(L"wrapper1", L"wrapper2"); do_test(comma_join(complete_get_wrap_targets(L"wrapper1")) == L"wrapper2"); complete_add_wrapper(L"wrapper2", L"wrapper3"); @@ -2679,7 +2679,7 @@ static void test_complete() { do_test(comma_join(complete_get_wrap_targets(L"wrapper2")) == L"wrapper3"); do_test(comma_join(complete_get_wrap_targets(L"wrapper3")) == L"wrapper1"); complete_remove_wrapper(L"wrapper1", L"wrapper2"); - do_test(comma_join(complete_get_wrap_targets(L"wrapper1")) == L""); + do_test(comma_join(complete_get_wrap_targets(L"wrapper1")).empty()); do_test(comma_join(complete_get_wrap_targets(L"wrapper2")) == L"wrapper3"); do_test(comma_join(complete_get_wrap_targets(L"wrapper3")) == L"wrapper1"); } diff --git a/src/history.cpp b/src/history.cpp index fafcd72ce..dcc041c97 100644 --- a/src/history.cpp +++ b/src/history.cpp @@ -371,7 +371,7 @@ static size_t trim_leading_spaces(std::string &str) { static bool extract_prefix_and_unescape_yaml(std::string *key, std::string *value, const std::string &line) { - size_t where = line.find(":"); + size_t where = line.find(':'); if (where != std::string::npos) { key->assign(line, 0, where); @@ -1868,7 +1868,7 @@ wcstring history_session_id(const environment_t &vars) { if (var) { wcstring session_id = var->as_string(); if (session_id.empty()) { - result = L""; + result.clear(); } else if (session_id == L"default") { ; // using the default value } else if (valid_var_name(session_id)) { diff --git a/src/output.cpp b/src/output.cpp index 14d210c3c..ddabac3b4 100644 --- a/src/output.cpp +++ b/src/output.cpp @@ -234,7 +234,7 @@ void outputter_t::set_color(rgb_color_t c, rgb_color_t c2) { if (c == c2) c = (c2 == rgb_color_t::white()) ? rgb_color_t::black() : rgb_color_t::white(); } - if ((enter_bold_mode != 0) && (std::strlen(enter_bold_mode) > 0)) { + if (enter_bold_mode && enter_bold_mode[0] != '\0') { if (bg_set && !last_bg_set) { // Background color changed and is set, so we enter bold mode to make reading easier. // This means bold mode is _always_ on when the background color is set. @@ -295,7 +295,7 @@ void outputter_t::set_color(rgb_color_t c, rgb_color_t c2) { } // Lastly, we set bold, underline, italics, dim, and reverse modes correctly. - if (is_bold && !was_bold && enter_bold_mode && std::strlen(enter_bold_mode) > 0 && !bg_set) { + if (is_bold && !was_bold && enter_bold_mode && enter_bold_mode[0] != '\0' && !bg_set) { // The unconst cast is for NetBSD's benefit. DO NOT REMOVE! writembs_nofail(*this, tparm((char *)enter_bold_mode)); was_bold = is_bold; @@ -310,27 +310,27 @@ void outputter_t::set_color(rgb_color_t c, rgb_color_t c2) { } was_underline = is_underline; - if (was_italics && !is_italics && enter_italics_mode && std::strlen(enter_italics_mode) > 0) { + if (was_italics && !is_italics && enter_italics_mode && enter_italics_mode[0] != '\0') { writembs_nofail(*this, exit_italics_mode); was_italics = is_italics; } - if (!was_italics && is_italics && enter_italics_mode && std::strlen(enter_italics_mode) > 0) { + if (!was_italics && is_italics && enter_italics_mode && enter_italics_mode[0] != '\0') { writembs_nofail(*this, enter_italics_mode); was_italics = is_italics; } - if (is_dim && !was_dim && enter_dim_mode && std::strlen(enter_dim_mode) > 0) { + if (is_dim && !was_dim && enter_dim_mode && enter_dim_mode[0] != '\0') { writembs_nofail(*this, enter_dim_mode); was_dim = is_dim; } if (is_reverse && !was_reverse) { // Some terms do not have a reverse mode set, so standout mode is a fallback. - if (enter_reverse_mode && std::strlen(enter_reverse_mode) > 0) { + if (enter_reverse_mode && enter_reverse_mode[0] != '\0') { writembs_nofail(*this, enter_reverse_mode); was_reverse = is_reverse; - } else if (enter_standout_mode && std::strlen(enter_standout_mode) > 0) { + } else if (enter_standout_mode && enter_standout_mode[0] != '\0') { writembs_nofail(*this, enter_standout_mode); was_reverse = is_reverse; } diff --git a/src/parse_tree.cpp b/src/parse_tree.cpp index 52be15884..cd7b35568 100644 --- a/src/parse_tree.cpp +++ b/src/parse_tree.cpp @@ -691,7 +691,7 @@ void parse_ll_t::report_tokenizer_error(const tok_t &tok) { parse_error_code_t parse_error_code = parse_error_from_tokenizer_error(tok.error); this->parse_error_at_location(tok.offset, tok.length, tok.offset + tok.error_offset, parse_error_code, L"%ls", - tokenizer_get_error_message(tok.error).c_str()); + tokenizer_get_error_message(tok.error)); } void parse_ll_t::parse_error_unexpected_token(const wchar_t *expected, parse_token_t token) { diff --git a/src/tokenizer.cpp b/src/tokenizer.cpp index 8de6f3da2..ef55b7877 100644 --- a/src/tokenizer.cpp +++ b/src/tokenizer.cpp @@ -17,7 +17,8 @@ #include "tokenizer.h" #include "wutil.h" // IWYU pragma: keep -wcstring tokenizer_get_error_message(tokenizer_error_t err) { +// _(s) is already wgettext(s).c_str(), so let's not convert back to wcstring +const wchar_t * tokenizer_get_error_message(tokenizer_error_t err) { switch (err) { case tokenizer_error_t::none: return L""; @@ -47,7 +48,7 @@ wcstring tokenizer_get_error_message(tokenizer_error_t err) { return _(L"Unexpected ')' found, expecting '}'"); } assert(0 && "Unexpected tokenizer error"); - return NULL; + return nullptr; } // Whether carets redirect stderr. diff --git a/src/tokenizer.h b/src/tokenizer.h index 5eb0806a3..0d0527e86 100644 --- a/src/tokenizer.h +++ b/src/tokenizer.h @@ -61,7 +61,7 @@ enum class tokenizer_error_t { }; /// Get the error message for an error \p err. -wcstring tokenizer_get_error_message(tokenizer_error_t err); +const wchar_t *tokenizer_get_error_message(tokenizer_error_t err); struct tok_t { // The type of the token. diff --git a/src/wildcard.cpp b/src/wildcard.cpp index 398b155d8..5e0b47b22 100644 --- a/src/wildcard.cpp +++ b/src/wildcard.cpp @@ -336,7 +336,7 @@ bool wildcard_match(const wcstring &str, const wcstring &wc, bool leading_dots_f /// \param stat_res The result of calling stat on the file /// \param buf The struct buf output of calling stat on the file /// \param err The errno value after a failed stat call on the file. -static wcstring file_get_desc(const wcstring &filename, int lstat_res, const struct stat &lbuf, +static const wchar_t *file_get_desc(const wcstring &filename, int lstat_res, const struct stat &lbuf, int stat_res, const struct stat &buf, int err) { if (lstat_res) { return COMPLETE_FILE_DESC; @@ -929,14 +929,12 @@ int wildcard_expand_string(const wcstring &wc, const wcstring &working_directory // Check for a leading slash. If we find one, we have an absolute path: the prefix is empty, the // base dir is /, and the wildcard is the remainder. If we don't find one, the prefix is the // working directory, the base dir is empty. - wcstring prefix, base_dir, effective_wc; + wcstring prefix = L"", base_dir = L"", effective_wc; if (string_prefixes_string(L"/", wc)) { - prefix = L""; base_dir = L"/"; effective_wc = wc.substr(1); } else { prefix = working_directory; - base_dir = L""; effective_wc = wc; } diff --git a/src/wutil.cpp b/src/wutil.cpp index 2004dcf42..fa250c59b 100644 --- a/src/wutil.cpp +++ b/src/wutil.cpp @@ -39,7 +39,7 @@ static owning_lock> wgettext_map; bool wreaddir_resolving(DIR *dir, const wcstring &dir_path, wcstring &out_name, bool *out_is_dir) { struct dirent *result = readdir(dir); if (!result) { - out_name = L""; + out_name.clear(); return false; } @@ -85,7 +85,7 @@ bool wreaddir_resolving(DIR *dir, const wcstring &dir_path, wcstring &out_name, bool wreaddir(DIR *dir, wcstring &out_name) { struct dirent *result = readdir(dir); if (!result) { - out_name = L""; + out_name.clear(); return false; }