From 68fa4011c80aa72ff905b025dbe2a4917d54935e Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Thu, 30 Jul 2020 20:57:21 +0200 Subject: [PATCH] Remove last usage of legacy str_startswith macro --- src/Util.cpp | 8 ++++++++ src/Util.hpp | 3 ++- src/ccache.cpp | 14 ++++++++++---- unittest/test_Util.cpp | 17 +++++++++++++++++ 4 files changed, 37 insertions(+), 5 deletions(-) diff --git a/src/Util.cpp b/src/Util.cpp index 8678b1c62..0476bc224 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -1137,6 +1137,14 @@ split_into_strings(string_view input, const char* separators) return split_at(input, separators); } +bool +starts_with(const char* string, nonstd::string_view prefix) +{ + // Optimized version of starts_with(string_view, string_view): avoid computing + // the length of the string argument. + return strncmp(string, prefix.data(), prefix.length()) == 0; +} + bool starts_with(string_view string, string_view prefix) { diff --git a/src/Util.hpp b/src/Util.hpp index 0141f3fae..149d709ff 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -356,7 +356,8 @@ std::vector split_into_views(nonstd::string_view input, std::vector split_into_strings(nonstd::string_view input, const char* separators); -// Return true if prefix is a prefix of string. +// Return true if `prefix` is a prefix of `string`. +bool starts_with(const char* string, nonstd::string_view prefix); bool starts_with(nonstd::string_view string, nonstd::string_view prefix); // Returns a copy of string with the specified ANSI CSI sequences removed. diff --git a/src/ccache.cpp b/src/ccache.cpp index 2ecc1f10a..3e8a2341f 100644 --- a/src/ccache.cpp +++ b/src/ccache.cpp @@ -425,6 +425,13 @@ process_preprocessed_file(Context& ctx, // There must be at least 7 characters (# 1 "x") left to potentially find an // include file path. while (q < end - 7) { + static const string_view pragma_gcc_pch_preprocess = + "pragma GCC pch_preprocess "; + static const string_view hash_31_command_line_newline = + "# 31 \"\"\n"; + static const string_view hash_32_command_line_2_newline = + "# 32 \"\" 2\n"; + // Check if we look at a line containing the file name of an included file. // At least the following formats exist (where N is a positive integer): // @@ -449,15 +456,14 @@ process_preprocessed_file(Context& ctx, // GCC: && ((q[1] == ' ' && q[2] >= '0' && q[2] <= '9') // GCC precompiled header: - || (q[1] == 'p' - && str_startswith(&q[2], "ragma GCC pch_preprocess ")) + || Util::starts_with(&q[1], pragma_gcc_pch_preprocess) // HP/AIX: || (q[1] == 'l' && q[2] == 'i' && q[3] == 'n' && q[4] == 'e' && q[5] == ' ')) && (q == data.data() || q[-1] == '\n')) { // Workarounds for preprocessor linemarker bugs in GCC version 6. if (q[2] == '3') { - if (str_startswith(q, "# 31 \"\"\n")) { + if (Util::starts_with(q, hash_31_command_line_newline)) { // Bogus extra line with #31, after the regular #1: Ignore the whole // line, and continue parsing. hash.hash(p, q - p); @@ -467,7 +473,7 @@ process_preprocessed_file(Context& ctx, q++; p = q; continue; - } else if (str_startswith(q, "# 32 \"\" 2\n")) { + } else if (Util::starts_with(q, hash_32_command_line_2_newline)) { // Bogus wrong line with #32, instead of regular #1: Replace the line // number with the usual one. hash.hash(p, q - p); diff --git a/unittest/test_Util.cpp b/unittest/test_Util.cpp index fb76f43b7..34f2c5d9d 100644 --- a/unittest/test_Util.cpp +++ b/unittest/test_Util.cpp @@ -764,6 +764,7 @@ TEST_CASE("Util::split_into_strings") TEST_CASE("Util::starts_with") { + // starts_with(const char*, string_view) CHECK(Util::starts_with("", "")); CHECK(Util::starts_with("x", "")); CHECK(Util::starts_with("x", "x")); @@ -778,6 +779,22 @@ TEST_CASE("Util::starts_with") CHECK_FALSE(Util::starts_with("", "x")); CHECK_FALSE(Util::starts_with("x", "y")); CHECK_FALSE(Util::starts_with("x", "xy")); + + // starts_with(string_view, string_view) + CHECK(Util::starts_with(std::string(""), "")); + CHECK(Util::starts_with(std::string("x"), "")); + CHECK(Util::starts_with(std::string("x"), "x")); + CHECK(Util::starts_with(std::string("xy"), "")); + CHECK(Util::starts_with(std::string("xy"), "x")); + CHECK(Util::starts_with(std::string("xy"), "xy")); + CHECK(Util::starts_with(std::string("xyz"), "")); + CHECK(Util::starts_with(std::string("xyz"), "x")); + CHECK(Util::starts_with(std::string("xyz"), "xy")); + CHECK(Util::starts_with(std::string("xyz"), "xyz")); + + CHECK_FALSE(Util::starts_with(std::string(""), "x")); + CHECK_FALSE(Util::starts_with(std::string("x"), "y")); + CHECK_FALSE(Util::starts_with(std::string("x"), "xy")); } TEST_CASE("Util::strip_whitespace") -- 2.47.3