Loading base/include/android-base/strings.h +7 −13 Original line number Diff line number Diff line Loading @@ -56,23 +56,17 @@ extern template std::string Join(const std::vector<std::string>&, const std::str extern template std::string Join(const std::vector<const char*>&, const std::string&); // Tests whether 's' starts with 'prefix'. // TODO: string_view bool StartsWith(const std::string& s, const char* prefix); bool StartsWithIgnoreCase(const std::string& s, const char* prefix); bool StartsWith(const std::string& s, const std::string& prefix); bool StartsWithIgnoreCase(const std::string& s, const std::string& prefix); bool StartsWith(const std::string& s, char prefix); bool StartsWith(std::string_view s, std::string_view prefix); bool StartsWith(std::string_view s, char prefix); bool StartsWithIgnoreCase(std::string_view s, std::string_view prefix); // Tests whether 's' ends with 'suffix'. // TODO: string_view bool EndsWith(const std::string& s, const char* suffix); bool EndsWithIgnoreCase(const std::string& s, const char* suffix); bool EndsWith(const std::string& s, const std::string& suffix); bool EndsWithIgnoreCase(const std::string& s, const std::string& suffix); bool EndsWith(const std::string& s, char suffix); bool EndsWith(std::string_view s, std::string_view suffix); bool EndsWith(std::string_view s, char suffix); bool EndsWithIgnoreCase(std::string_view s, std::string_view suffix); // Tests whether 'lhs' equals 'rhs', ignoring case. bool EqualsIgnoreCase(const std::string& lhs, const std::string& rhs); bool EqualsIgnoreCase(std::string_view lhs, std::string_view rhs); } // namespace base } // namespace android base/strings.cpp +15 −40 Original line number Diff line number Diff line Loading @@ -87,58 +87,33 @@ template std::string Join(const std::vector<const char*>&, char); template std::string Join(const std::vector<std::string>&, const std::string&); template std::string Join(const std::vector<const char*>&, const std::string&); bool StartsWith(const std::string& s, const char* prefix) { return strncmp(s.c_str(), prefix, strlen(prefix)) == 0; bool StartsWith(std::string_view s, std::string_view prefix) { return s.substr(0, prefix.size()) == prefix; } bool StartsWith(const std::string& s, const std::string& prefix) { return strncmp(s.c_str(), prefix.c_str(), prefix.size()) == 0; bool StartsWith(std::string_view s, char prefix) { return !s.empty() && s.front() == prefix; } bool StartsWith(const std::string& s, char prefix) { return *s.c_str() == prefix; // Use c_str() to guarantee there is at least a '\0'. bool StartsWithIgnoreCase(std::string_view s, std::string_view prefix) { return s.size() >= prefix.size() && strncasecmp(s.data(), prefix.data(), prefix.size()) == 0; } bool StartsWithIgnoreCase(const std::string& s, const char* prefix) { return strncasecmp(s.c_str(), prefix, strlen(prefix)) == 0; bool EndsWith(std::string_view s, std::string_view suffix) { return s.size() >= suffix.size() && s.substr(s.size() - suffix.size(), suffix.size()) == suffix; } bool StartsWithIgnoreCase(const std::string& s, const std::string& prefix) { return strncasecmp(s.c_str(), prefix.c_str(), prefix.size()) == 0; bool EndsWith(std::string_view s, char suffix) { return !s.empty() && s.back() == suffix; } static bool EndsWith(const std::string& s, const char* suffix, size_t suffix_length, bool case_sensitive) { size_t string_length = s.size(); if (suffix_length > string_length) { return false; } size_t offset = string_length - suffix_length; return (case_sensitive ? strncmp : strncasecmp)(s.c_str() + offset, suffix, suffix_length) == 0; } bool EndsWith(const std::string& s, const char* suffix) { return EndsWith(s, suffix, strlen(suffix), true); } bool EndsWith(const std::string& s, const std::string& suffix) { return EndsWith(s, suffix.c_str(), suffix.size(), true); } bool EndsWith(const std::string& s, char suffix) { return EndsWith(s, &suffix, 1, true); } bool EndsWithIgnoreCase(const std::string& s, const char* suffix) { return EndsWith(s, suffix, strlen(suffix), false); } bool EndsWithIgnoreCase(const std::string& s, const std::string& suffix) { return EndsWith(s, suffix.c_str(), suffix.size(), false); bool EndsWithIgnoreCase(std::string_view s, std::string_view suffix) { return s.size() >= suffix.size() && strncasecmp(s.data() + (s.size() - suffix.size()), suffix.data(), suffix.size()) == 0; } bool EqualsIgnoreCase(const std::string& lhs, const std::string& rhs) { return strcasecmp(lhs.c_str(), rhs.c_str()) == 0; bool EqualsIgnoreCase(std::string_view lhs, std::string_view rhs) { return lhs.size() == rhs.size() && strncasecmp(lhs.data(), rhs.data(), lhs.size()) == 0; } } // namespace base Loading fastboot/fuzzy_fastboot/fixtures.cpp +5 −0 Original line number Diff line number Diff line Loading @@ -130,10 +130,14 @@ void FastBootTest::SetUp() { ASSERT_EQ(device_path, cb_scratch); // The path can not change } fb = std::unique_ptr<FastBootDriver>(new FastBootDriver(transport.get(), {}, true)); // No error checking since non-A/B devices may not support the command fb->GetVar("current-slot", &initial_slot); } void FastBootTest::TearDown() { EXPECT_TRUE(UsbStillAvailible()) << USB_PORT_GONE; // No error checking since non-A/B devices may not support the command fb->SetActive(initial_slot); TearDownSerial(); Loading Loading @@ -232,6 +236,7 @@ void FastBootTest::SetLockState(bool unlock, bool assert_change) { std::string FastBootTest::device_path = ""; std::string FastBootTest::cb_scratch = ""; std::string FastBootTest::initial_slot = ""; int FastBootTest::serial_port = 0; template <bool UNLOCKED> Loading fastboot/fuzzy_fastboot/fixtures.h +1 −0 Original line number Diff line number Diff line Loading @@ -70,6 +70,7 @@ class FastBootTest : public testing::Test { // This is an annoying hack static std::string cb_scratch; static std::string device_path; static std::string initial_slot; }; template <bool UNLOCKED> Loading fs_mgr/tests/adb-remount-test.sh +27 −20 Original line number Diff line number Diff line Loading @@ -447,59 +447,71 @@ die() { exit 1 } [ "USAGE: EXPECT_EQ <lval> <rval> [message] [ "USAGE: EXPECT_EQ <lval> <rval> [--warning [message]] Returns true if (regex) lval matches rval" ] EXPECT_EQ() { local lval="${1}" local rval="${2}" shift 2 local error=1 local prefix="${RED}[ ERROR ]${NORMAL}" if [ X"${1}" = X"--warning" ]; then prefix="${RED}[ WARNING ]${NORMAL}" error=0 shift 1 fi if ! ( echo X"${rval}" | grep '^X'"${lval}"'$' >/dev/null 2>/dev/null ); then if [ `echo ${lval}${rval}${*} | wc -c` -gt 50 -o "${rval}" != "${rval% *}" ]; then echo "ERROR: expected \"${lval}\"" >&2 echo " got \"${rval}\"" | echo "${prefix} expected \"${lval}\"" >&2 echo "${prefix} got \"${rval}\"" | sed ': again N s/\(\n\)\([^ ]\)/\1 \2/ t again' >&2 if [ -n "${*}" ] ; then echo " ${*}" >&2 echo "${prefix} ${*}" >&2 fi else echo "ERROR: expected \"${lval}\" got \"${rval}\" ${*}" >&2 echo "${prefix} expected \"${lval}\" got \"${rval}\" ${*}" >&2 fi return 1 return ${error} fi if [ -n "${*}" ] ; then if [ X"${lval}" != X"${rval}" ]; then prefix="${GREEN}[ INFO ]${NORMAL}" if [ X"${lval}" != X"${rval}" ]; then # we were supplied a regex? if [ `echo ${lval}${rval}${*} | wc -c` -gt 60 -o "${rval}" != "${rval% *}" ]; then echo "INFO: ok \"${lval}\"" >&2 echo "${prefix} ok \"${lval}\"" >&2 echo " = \"${rval}\"" | sed ': again N s/\(\n\)\([^ ]\)/\1 \2/ t again' >&2 if [ -n "${*}" ] ; then echo " ${*}" >&2 echo "${prefix} ${*}" >&2 fi else echo "INFO: ok \"${lval}\" = \"${rval}\" ${*}" >&2 echo "${prefix} ok \"${lval}\" = \"${rval}\" ${*}" >&2 fi else echo "INFO: ok \"${lval}\" ${*}" >&2 echo "${prefix} ok \"${lval}\" ${*}" >&2 fi fi return 0 } [ "USAGE: check_eq <lval> <rval> [message] [ "USAGE: check_eq <lval> <rval> [--warning [message]] Exits if (regex) lval mismatches rval" ] check_eq() { local lval="${1}" local rval="${2}" shift 2 if [ X"${1}" = X"--warning" ]; then EXPECT_EQ "${lval}" "${rval}" ${*} return fi EXPECT_EQ "${lval}" "${rval}" || die "${@}" } Loading Loading @@ -1085,14 +1097,9 @@ else check_eq "cat: /vendor/hello: No such file or directory" "${B}" \ vendor content after flash vendor else ( echo "${ORANGE}[ WARNING ]${NORMAL} user fastboot missing required to invalidate, ignoring a failure" >&2 restore() { true } check_eq "cat: /vendor/hello: No such file or directory" "${B}" \ vendor content after flash vendor ) --warning vendor content after flash vendor fi fi Loading Loading
base/include/android-base/strings.h +7 −13 Original line number Diff line number Diff line Loading @@ -56,23 +56,17 @@ extern template std::string Join(const std::vector<std::string>&, const std::str extern template std::string Join(const std::vector<const char*>&, const std::string&); // Tests whether 's' starts with 'prefix'. // TODO: string_view bool StartsWith(const std::string& s, const char* prefix); bool StartsWithIgnoreCase(const std::string& s, const char* prefix); bool StartsWith(const std::string& s, const std::string& prefix); bool StartsWithIgnoreCase(const std::string& s, const std::string& prefix); bool StartsWith(const std::string& s, char prefix); bool StartsWith(std::string_view s, std::string_view prefix); bool StartsWith(std::string_view s, char prefix); bool StartsWithIgnoreCase(std::string_view s, std::string_view prefix); // Tests whether 's' ends with 'suffix'. // TODO: string_view bool EndsWith(const std::string& s, const char* suffix); bool EndsWithIgnoreCase(const std::string& s, const char* suffix); bool EndsWith(const std::string& s, const std::string& suffix); bool EndsWithIgnoreCase(const std::string& s, const std::string& suffix); bool EndsWith(const std::string& s, char suffix); bool EndsWith(std::string_view s, std::string_view suffix); bool EndsWith(std::string_view s, char suffix); bool EndsWithIgnoreCase(std::string_view s, std::string_view suffix); // Tests whether 'lhs' equals 'rhs', ignoring case. bool EqualsIgnoreCase(const std::string& lhs, const std::string& rhs); bool EqualsIgnoreCase(std::string_view lhs, std::string_view rhs); } // namespace base } // namespace android
base/strings.cpp +15 −40 Original line number Diff line number Diff line Loading @@ -87,58 +87,33 @@ template std::string Join(const std::vector<const char*>&, char); template std::string Join(const std::vector<std::string>&, const std::string&); template std::string Join(const std::vector<const char*>&, const std::string&); bool StartsWith(const std::string& s, const char* prefix) { return strncmp(s.c_str(), prefix, strlen(prefix)) == 0; bool StartsWith(std::string_view s, std::string_view prefix) { return s.substr(0, prefix.size()) == prefix; } bool StartsWith(const std::string& s, const std::string& prefix) { return strncmp(s.c_str(), prefix.c_str(), prefix.size()) == 0; bool StartsWith(std::string_view s, char prefix) { return !s.empty() && s.front() == prefix; } bool StartsWith(const std::string& s, char prefix) { return *s.c_str() == prefix; // Use c_str() to guarantee there is at least a '\0'. bool StartsWithIgnoreCase(std::string_view s, std::string_view prefix) { return s.size() >= prefix.size() && strncasecmp(s.data(), prefix.data(), prefix.size()) == 0; } bool StartsWithIgnoreCase(const std::string& s, const char* prefix) { return strncasecmp(s.c_str(), prefix, strlen(prefix)) == 0; bool EndsWith(std::string_view s, std::string_view suffix) { return s.size() >= suffix.size() && s.substr(s.size() - suffix.size(), suffix.size()) == suffix; } bool StartsWithIgnoreCase(const std::string& s, const std::string& prefix) { return strncasecmp(s.c_str(), prefix.c_str(), prefix.size()) == 0; bool EndsWith(std::string_view s, char suffix) { return !s.empty() && s.back() == suffix; } static bool EndsWith(const std::string& s, const char* suffix, size_t suffix_length, bool case_sensitive) { size_t string_length = s.size(); if (suffix_length > string_length) { return false; } size_t offset = string_length - suffix_length; return (case_sensitive ? strncmp : strncasecmp)(s.c_str() + offset, suffix, suffix_length) == 0; } bool EndsWith(const std::string& s, const char* suffix) { return EndsWith(s, suffix, strlen(suffix), true); } bool EndsWith(const std::string& s, const std::string& suffix) { return EndsWith(s, suffix.c_str(), suffix.size(), true); } bool EndsWith(const std::string& s, char suffix) { return EndsWith(s, &suffix, 1, true); } bool EndsWithIgnoreCase(const std::string& s, const char* suffix) { return EndsWith(s, suffix, strlen(suffix), false); } bool EndsWithIgnoreCase(const std::string& s, const std::string& suffix) { return EndsWith(s, suffix.c_str(), suffix.size(), false); bool EndsWithIgnoreCase(std::string_view s, std::string_view suffix) { return s.size() >= suffix.size() && strncasecmp(s.data() + (s.size() - suffix.size()), suffix.data(), suffix.size()) == 0; } bool EqualsIgnoreCase(const std::string& lhs, const std::string& rhs) { return strcasecmp(lhs.c_str(), rhs.c_str()) == 0; bool EqualsIgnoreCase(std::string_view lhs, std::string_view rhs) { return lhs.size() == rhs.size() && strncasecmp(lhs.data(), rhs.data(), lhs.size()) == 0; } } // namespace base Loading
fastboot/fuzzy_fastboot/fixtures.cpp +5 −0 Original line number Diff line number Diff line Loading @@ -130,10 +130,14 @@ void FastBootTest::SetUp() { ASSERT_EQ(device_path, cb_scratch); // The path can not change } fb = std::unique_ptr<FastBootDriver>(new FastBootDriver(transport.get(), {}, true)); // No error checking since non-A/B devices may not support the command fb->GetVar("current-slot", &initial_slot); } void FastBootTest::TearDown() { EXPECT_TRUE(UsbStillAvailible()) << USB_PORT_GONE; // No error checking since non-A/B devices may not support the command fb->SetActive(initial_slot); TearDownSerial(); Loading Loading @@ -232,6 +236,7 @@ void FastBootTest::SetLockState(bool unlock, bool assert_change) { std::string FastBootTest::device_path = ""; std::string FastBootTest::cb_scratch = ""; std::string FastBootTest::initial_slot = ""; int FastBootTest::serial_port = 0; template <bool UNLOCKED> Loading
fastboot/fuzzy_fastboot/fixtures.h +1 −0 Original line number Diff line number Diff line Loading @@ -70,6 +70,7 @@ class FastBootTest : public testing::Test { // This is an annoying hack static std::string cb_scratch; static std::string device_path; static std::string initial_slot; }; template <bool UNLOCKED> Loading
fs_mgr/tests/adb-remount-test.sh +27 −20 Original line number Diff line number Diff line Loading @@ -447,59 +447,71 @@ die() { exit 1 } [ "USAGE: EXPECT_EQ <lval> <rval> [message] [ "USAGE: EXPECT_EQ <lval> <rval> [--warning [message]] Returns true if (regex) lval matches rval" ] EXPECT_EQ() { local lval="${1}" local rval="${2}" shift 2 local error=1 local prefix="${RED}[ ERROR ]${NORMAL}" if [ X"${1}" = X"--warning" ]; then prefix="${RED}[ WARNING ]${NORMAL}" error=0 shift 1 fi if ! ( echo X"${rval}" | grep '^X'"${lval}"'$' >/dev/null 2>/dev/null ); then if [ `echo ${lval}${rval}${*} | wc -c` -gt 50 -o "${rval}" != "${rval% *}" ]; then echo "ERROR: expected \"${lval}\"" >&2 echo " got \"${rval}\"" | echo "${prefix} expected \"${lval}\"" >&2 echo "${prefix} got \"${rval}\"" | sed ': again N s/\(\n\)\([^ ]\)/\1 \2/ t again' >&2 if [ -n "${*}" ] ; then echo " ${*}" >&2 echo "${prefix} ${*}" >&2 fi else echo "ERROR: expected \"${lval}\" got \"${rval}\" ${*}" >&2 echo "${prefix} expected \"${lval}\" got \"${rval}\" ${*}" >&2 fi return 1 return ${error} fi if [ -n "${*}" ] ; then if [ X"${lval}" != X"${rval}" ]; then prefix="${GREEN}[ INFO ]${NORMAL}" if [ X"${lval}" != X"${rval}" ]; then # we were supplied a regex? if [ `echo ${lval}${rval}${*} | wc -c` -gt 60 -o "${rval}" != "${rval% *}" ]; then echo "INFO: ok \"${lval}\"" >&2 echo "${prefix} ok \"${lval}\"" >&2 echo " = \"${rval}\"" | sed ': again N s/\(\n\)\([^ ]\)/\1 \2/ t again' >&2 if [ -n "${*}" ] ; then echo " ${*}" >&2 echo "${prefix} ${*}" >&2 fi else echo "INFO: ok \"${lval}\" = \"${rval}\" ${*}" >&2 echo "${prefix} ok \"${lval}\" = \"${rval}\" ${*}" >&2 fi else echo "INFO: ok \"${lval}\" ${*}" >&2 echo "${prefix} ok \"${lval}\" ${*}" >&2 fi fi return 0 } [ "USAGE: check_eq <lval> <rval> [message] [ "USAGE: check_eq <lval> <rval> [--warning [message]] Exits if (regex) lval mismatches rval" ] check_eq() { local lval="${1}" local rval="${2}" shift 2 if [ X"${1}" = X"--warning" ]; then EXPECT_EQ "${lval}" "${rval}" ${*} return fi EXPECT_EQ "${lval}" "${rval}" || die "${@}" } Loading Loading @@ -1085,14 +1097,9 @@ else check_eq "cat: /vendor/hello: No such file or directory" "${B}" \ vendor content after flash vendor else ( echo "${ORANGE}[ WARNING ]${NORMAL} user fastboot missing required to invalidate, ignoring a failure" >&2 restore() { true } check_eq "cat: /vendor/hello: No such file or directory" "${B}" \ vendor content after flash vendor ) --warning vendor content after flash vendor fi fi Loading