util: safer `ends_with` (#31943)

* safer ends_with

* improve
pull/32018/head
Dean Lee 1 year ago committed by GitHub
parent bbd08baacd
commit d05c19b9f4
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 5
      common/util.cc
  2. 2
      common/util.h

@ -256,8 +256,9 @@ bool starts_with(const std::string &s1, const std::string &s2) {
return strncmp(s1.c_str(), s2.c_str(), s2.size()) == 0;
}
bool ends_with(const std::string &s1, const std::string &s2) {
return strcmp(s1.c_str() + (s1.size() - s2.size()), s2.c_str()) == 0;
bool ends_with(const std::string& s, const std::string& suffix) {
return s.size() >= suffix.size() &&
strcmp(s.c_str() + (s.size() - suffix.size()), suffix.c_str()) == 0;
}
std::string check_output(const std::string& command) {

@ -77,7 +77,7 @@ float getenv(const char* key, float default_val);
std::string hexdump(const uint8_t* in, const size_t size);
std::string dir_name(std::string const& path);
bool starts_with(const std::string &s1, const std::string &s2);
bool ends_with(const std::string &s1, const std::string &s2);
bool ends_with(const std::string &s, const std::string &suffix);
// ***** random helpers *****
int random_int(int min, int max);

Loading…
Cancel
Save