Refactor util::ends_with and remove iends_with_l

This commit is contained in:
Tatsuhiro Tsujikawa
2024-04-27 18:48:09 +09:00
parent ae678f4f30
commit 34433fdcb3
4 changed files with 13 additions and 25 deletions

View File

@@ -1572,7 +1572,7 @@ int read_tls_sct_from_dir(std::vector<uint8_t> &dst, const StringRef &opt,
auto name = StringRef{ent->d_name};
if (name[0] == '.' || !util::iends_with_l(name, ".sct")) {
if (name[0] == '.' || !util::iends_with(name, ".sct"_sr)) {
continue;
}

View File

@@ -867,7 +867,7 @@ bool check_path(const std::string &path) {
path.find('\\') == std::string::npos &&
path.find("/../") == std::string::npos &&
path.find("/./") == std::string::npos &&
!util::ends_with_l(path, "/..") && !util::ends_with_l(path, "/.");
!util::ends_with(path, "/.."_sr) && !util::ends_with(path, "/."_sr);
}
int64_t to_time64(const timeval &tv) {

View File

@@ -325,37 +325,28 @@ template <typename S, typename T> bool istarts_with(const S &a, const T &b) {
template <typename InputIterator1, typename InputIterator2>
bool ends_with(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2) {
if (last1 - first1 < last2 - first2) {
return false;
}
return std::equal(first2, last2, last1 - (last2 - first2));
auto len1 = std::distance(first1, last1);
auto len2 = std::distance(first2, last2);
return len1 >= len2 && std::equal(first2, last2, first1 + (len1 - len2));
}
template <typename T, typename S> bool ends_with(const T &a, const S &b) {
return ends_with(a.begin(), a.end(), b.begin(), b.end());
}
template <typename T, typename CharT, size_t N>
bool ends_with_l(const T &a, const CharT (&b)[N]) {
return ends_with(a.begin(), a.end(), b, b + N - 1);
return ends_with(std::begin(a), std::end(a), std::begin(b), std::end(b));
}
template <typename InputIterator1, typename InputIterator2>
bool iends_with(InputIterator1 first1, InputIterator1 last1,
InputIterator2 first2, InputIterator2 last2) {
if (last1 - first1 < last2 - first2) {
return false;
}
return std::equal(first2, last2, last1 - (last2 - first2), CaseCmp());
auto len1 = std::distance(first1, last1);
auto len2 = std::distance(first2, last2);
return len1 >= len2 &&
std::equal(first2, last2, first1 + (len1 - len2), CaseCmp());
}
template <typename T, typename S> bool iends_with(const T &a, const S &b) {
return iends_with(a.begin(), a.end(), b.begin(), b.end());
}
template <typename T, typename CharT, size_t N>
bool iends_with_l(const T &a, const CharT (&b)[N]) {
return iends_with(a.begin(), a.end(), b, b + N - 1);
return iends_with(std::begin(a), std::end(a), std::begin(b), std::end(b));
}
template <typename InputIt1, typename InputIt2>

View File

@@ -464,9 +464,6 @@ void test_util_ends_with(void) {
assert_true(util::iends_with("foo"_sr, StringRef{}));
assert_true(util::iends_with("oFoo"_sr, "fOO"_sr));
assert_false(util::iends_with("ofoo"_sr, "fo"_sr));
assert_true(util::iends_with_l("oFoo"_sr, "fOO"));
assert_false(util::iends_with_l("ofoo"_sr, "fo"));
}
void test_util_parse_http_date(void) {