From 2ab79f49384ecb18c5f09ab44b05baa9e77af086 Mon Sep 17 00:00:00 2001 From: Tatsuhiro Tsujikawa Date: Fri, 25 Mar 2016 23:00:34 +0900 Subject: [PATCH] src: Don't allow const char * in iends_with and iends_with_l --- src/util.h | 19 +++++-------------- src/util_test.cc | 15 +++++++++------ 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/src/util.h b/src/util.h index 7c54e490..cb61fe54 100644 --- a/src/util.h +++ b/src/util.h @@ -260,22 +260,13 @@ bool iends_with(InputIterator1 first1, InputIterator1 last1, return std::equal(first2, last2, last1 - (last2 - first2), CaseCmp()); } -inline bool iends_with(const std::string &a, const std::string &b) { - return iends_with(std::begin(a), std::end(a), std::begin(b), std::end(b)); +template bool iends_with(const T &a, const S &b) { + return iends_with(a.begin(), a.end(), b.begin(), b.end()); } -inline bool iends_with(const StringRef &a, const StringRef &b) { - return iends_with(std::begin(a), std::end(a), std::begin(b), std::end(b)); -} - -template -bool iends_with_l(const std::string &a, const CharT(&b)[N]) { - return iends_with(std::begin(a), std::end(a), b, b + N - 1); -} - -template -bool iends_with_l(const StringRef &a, const CharT(&b)[N]) { - return iends_with(std::begin(a), std::end(a), b, b + N - 1); +template +bool iends_with_l(const T &a, const CharT(&b)[N]) { + return iends_with(a.begin(), a.end(), b, b + N - 1); } template diff --git a/src/util_test.cc b/src/util_test.cc index ae5a88bb..37604512 100644 --- a/src/util_test.cc +++ b/src/util_test.cc @@ -399,13 +399,16 @@ void test_util_ends_with(void) { CU_ASSERT(util::ends_with("ofoo", "foo")); CU_ASSERT(!util::ends_with("ofoo", "fo")); - CU_ASSERT(util::iends_with("fOo", "Foo")); - CU_ASSERT(util::iends_with("foo", "")); - CU_ASSERT(util::iends_with("oFoo", "fOO")); - CU_ASSERT(!util::iends_with("ofoo", "fo")); + CU_ASSERT( + util::iends_with(StringRef::from_lit("fOo"), StringRef::from_lit("Foo"))); + CU_ASSERT(util::iends_with(StringRef::from_lit("foo"), StringRef{})); + CU_ASSERT(util::iends_with(StringRef::from_lit("oFoo"), + StringRef::from_lit("fOO"))); + CU_ASSERT(!util::iends_with(StringRef::from_lit("ofoo"), + StringRef::from_lit("fo"))); - CU_ASSERT(util::iends_with_l("oFoo", "fOO")); - CU_ASSERT(!util::iends_with_l("ofoo", "fo")); + CU_ASSERT(util::iends_with_l(StringRef::from_lit("oFoo"), "fOO")); + CU_ASSERT(!util::iends_with_l(StringRef::from_lit("ofoo"), "fo")); } void test_util_parse_http_date(void) {