Use const_strlen in a few different places

This may slightly improve performance by allowing the compiler greater
visibility into what is happing on top of not executing at runtime in
some hot paths, but more importantly, it gets rid of magic constants in a
few different places.
This commit is contained in:
Mahmoud Al-Qudsi
2021-02-08 15:09:10 -06:00
parent 5d4c24bae1
commit cb3ab80cab
9 changed files with 32 additions and 31 deletions

View File

@@ -1497,12 +1497,12 @@ static void test_parse_util_cmdsubst_extent() {
}
parse_util_cmdsubst_extent(a, 8, &begin, &end);
if (begin != a + std::wcslen(L"echo (")) {
if (begin != a + const_strlen(L"echo (")) {
err(L"parse_util_cmdsubst_extent failed on line %ld", (long)__LINE__);
}
parse_util_cmdsubst_extent(a, 17, &begin, &end);
if (begin != a + std::wcslen(L"echo (echo (")) {
if (begin != a + const_strlen(L"echo (echo (")) {
err(L"parse_util_cmdsubst_extent failed on line %ld", (long)__LINE__);
}
}
@@ -1811,7 +1811,7 @@ static void test_escape_sequences() {
err(L"test_escape_sequences failed on line %d\n", __LINE__);
if (lc.escape_code_length(L"\x1B[2J") != 4)
err(L"test_escape_sequences failed on line %d\n", __LINE__);
if (lc.escape_code_length(L"\x1B[38;5;123mABC") != std::strlen("\x1B[38;5;123m"))
if (lc.escape_code_length(L"\x1B[38;5;123mABC") != const_strlen("\x1B[38;5;123m"))
err(L"test_escape_sequences failed on line %d\n", __LINE__);
if (lc.escape_code_length(L"\x1B@") != 2)
err(L"test_escape_sequences failed on line %d\n", __LINE__);
@@ -2285,41 +2285,41 @@ static void test_abbreviations() {
result = expand_abbreviation_in_command(L"gc somebranch", 0, vars);
if (!result) err(L"Command not expanded on line %ld", (long)__LINE__);
result = expand_abbreviation_in_command(L"gc somebranch", std::wcslen(L"gc"), vars);
result = expand_abbreviation_in_command(L"gc somebranch", const_strlen(L"gc"), vars);
if (!result) err(L"gc not expanded");
if (result != L"git checkout somebranch")
err(L"gc incorrectly expanded on line %ld to '%ls'", (long)__LINE__, result->c_str());
// Space separation.
result = expand_abbreviation_in_command(L"gx somebranch", std::wcslen(L"gc"), vars);
result = expand_abbreviation_in_command(L"gx somebranch", const_strlen(L"gc"), vars);
if (!result) err(L"gx not expanded");
if (result != L"git checkout somebranch")
err(L"gc incorrectly expanded on line %ld to '%ls'", (long)__LINE__, result->c_str());
result = expand_abbreviation_in_command(L"echo hi ; gc somebranch", std::wcslen(L"echo hi ; g"),
vars);
result = expand_abbreviation_in_command(L"echo hi ; gc somebranch",
const_strlen(L"echo hi ; g"), vars);
if (!result) err(L"gc not expanded on line %ld", (long)__LINE__);
if (result != L"echo hi ; git checkout somebranch")
err(L"gc incorrectly expanded on line %ld", (long)__LINE__);
result = expand_abbreviation_in_command(L"echo (echo (echo (echo (gc ",
std::wcslen(L"echo (echo (echo (echo (gc"), vars);
const_strlen(L"echo (echo (echo (echo (gc"), vars);
if (!result) err(L"gc not expanded on line %ld", (long)__LINE__);
if (result != L"echo (echo (echo (echo (git checkout ")
err(L"gc incorrectly expanded on line %ld to '%ls'", (long)__LINE__, result->c_str());
// If commands should be expanded.
result = expand_abbreviation_in_command(L"if gc", std::wcslen(L"if gc"), vars);
result = expand_abbreviation_in_command(L"if gc", const_strlen(L"if gc"), vars);
if (!result) err(L"gc not expanded on line %ld", (long)__LINE__);
if (result != L"if git checkout")
err(L"gc incorrectly expanded on line %ld to '%ls'", (long)__LINE__, result->c_str());
// Others should not be.
result = expand_abbreviation_in_command(L"of gc", std::wcslen(L"of gc"), vars);
result = expand_abbreviation_in_command(L"of gc", const_strlen(L"of gc"), vars);
if (result) err(L"gc incorrectly expanded on line %ld", (long)__LINE__);
// Others should not be.
result = expand_abbreviation_in_command(L"command gc", std::wcslen(L"command gc"), vars);
result = expand_abbreviation_in_command(L"command gc", const_strlen(L"command gc"), vars);
if (result) err(L"gc incorrectly expanded on line %ld", (long)__LINE__);
vars.pop();
@@ -3824,10 +3824,10 @@ static void test_universal_ok_to_save() {
// Ensure we don't try to save after reading from a newer fish.
say(L"Testing universal Ok to save");
if (system("mkdir -p test/fish_uvars_test/")) err(L"mkdir failed");
const char *contents = "# VERSION: 99999.99\n";
constexpr const char *contents = "# VERSION: 99999.99\n";
FILE *fp = fopen(wcs2string(UVARS_TEST_PATH).c_str(), "w");
assert(fp && "Failed to open UVARS_TEST_PATH for writing");
fwrite(contents, std::strlen(contents), 1, fp);
fwrite(contents, const_strlen(contents), 1, fp);
fclose(fp);
file_id_t before_id = file_id_for_path(UVARS_TEST_PATH);