From bafee1a67d2574e764014f69036d016a326fe387 Mon Sep 17 00:00:00 2001 From: Jesse Yurkovich Date: Sun, 10 Mar 2024 20:25:07 -0700 Subject: [PATCH] build(deps): Remove boost from strutil.cpp (#4181) Remove boost from `Strutil`. Use existing mechanisms and STL features instead. Partially addresses #4158 Signed-off-by: Jesse Yurkovich --- src/libutil/strutil.cpp | 52 ++++++++++++++++++++++-------------- src/libutil/strutil_test.cpp | 4 +++ 2 files changed, 36 insertions(+), 20 deletions(-) diff --git a/src/libutil/strutil.cpp b/src/libutil/strutil.cpp index dc3edf9df1..2ac8f561e9 100644 --- a/src/libutil/strutil.cpp +++ b/src/libutil/strutil.cpp @@ -3,6 +3,7 @@ // https://github.com/AcademySoftwareFoundation/OpenImageIO +#include #include #include #include @@ -21,9 +22,6 @@ # include #endif -#include -#include - #include #include #include @@ -527,7 +525,6 @@ Strutil::ends_with(string_view a, string_view b) if (asize < bsize) // a can't start with b if a is smaller return false; return strncmp(a.data() + asize - bsize, b.data(), bsize) == 0; - // return boost::algorithm::ends_with(a, b); } @@ -539,7 +536,6 @@ Strutil::iends_with(string_view a, string_view b) if (asize < bsize) // a can't start with b if a is smaller return false; return strncasecmp(a.data() + asize - bsize, b.data(), bsize) == 0; - // return boost::algorithm::iends_with(a, b, std::locale::classic()); } @@ -547,9 +543,6 @@ bool Strutil::contains(string_view a, string_view b) { return find(a, b) != string_view::npos; - // We used to use the boost contains, but it seems to be about 2x more - // expensive than (find() != npos). - // return boost::algorithm::contains(a, b); } @@ -557,9 +550,6 @@ bool Strutil::icontains(string_view a, string_view b) { return ifind(a, b) != string_view::npos; - // We used to use the boost icontains, but it seems to be about 2x more - // expensive than (ifind() != npos). - // return boost::algorithm::icontains(a, b, std::locale::classic()); } @@ -600,8 +590,19 @@ Strutil::ifind(string_view a, string_view b) return string_view::npos; if (b.empty()) return 0; - auto f = boost::algorithm::ifind_first(a, b, std::locale::classic()); - return f.empty() ? string_view::npos : f.begin() - a.data(); + + if (b.size() <= a.size()) { + const char* start = a.data(); + const char* last = a.data() + a.size() - b.size(); + while (start <= last) { + if (Strutil::strncasecmp(start, b.data(), b.size()) == 0) { + return size_t(start - a.data()); + } + start++; + } + } + + return string_view::npos; } @@ -612,22 +613,36 @@ Strutil::irfind(string_view a, string_view b) return string_view::npos; if (b.empty()) return a.size(); - auto f = boost::algorithm::ifind_last(a, b, std::locale::classic()); - return f.empty() ? string_view::npos : f.begin() - a.data(); + + if (b.size() <= a.size()) { + const char* start = a.data() + (a.size() - b.size()); + while (start >= a.data()) { + if (Strutil::strncasecmp(start, b.data(), b.size()) == 0) { + return size_t(start - a.data()); + } + start--; + } + } + + return string_view::npos; } void Strutil::to_lower(std::string& a) { - boost::algorithm::to_lower(a, std::locale::classic()); + const std::locale& loc = std::locale::classic(); + std::transform(a.cbegin(), a.cend(), a.begin(), + [&loc](char c) { return std::tolower(c, loc); }); } void Strutil::to_upper(std::string& a) { - boost::algorithm::to_upper(a, std::locale::classic()); + const std::locale& loc = std::locale::classic(); + std::transform(a.cbegin(), a.cend(), a.begin(), + [&loc](char c) { return std::toupper(c, loc); }); } @@ -636,7 +651,6 @@ bool Strutil::StringIEqual::operator()(const char* a, const char* b) const noexcept { return Strutil::strcasecmp(a, b) == 0; - // return boost::algorithm::iequals(a, b, std::locale::classic()); } @@ -644,8 +658,6 @@ bool Strutil::StringILess::operator()(const char* a, const char* b) const noexcept { return Strutil::strcasecmp(a, b) < 0; - // return boost::algorithm::ilexicographical_compare(a, b, - // std::locale::classic()); } diff --git a/src/libutil/strutil_test.cpp b/src/libutil/strutil_test.cpp index 0776acc9c6..d1abf22c08 100644 --- a/src/libutil/strutil_test.cpp +++ b/src/libutil/strutil_test.cpp @@ -464,12 +464,16 @@ test_comparisons() OIIO_CHECK_EQUAL(Strutil::ifind("abcdeabcde", "BC"), 1); OIIO_CHECK_EQUAL(Strutil::ifind("abcdeabcde", "ac"), std::string::npos); OIIO_CHECK_EQUAL(Strutil::ifind("abcdeabcde", ""), 0); + OIIO_CHECK_EQUAL(Strutil::ifind("Xabcdeabcde", "x"), 0); + OIIO_CHECK_EQUAL(Strutil::ifind("abcdeabcdeX", "x"), 10); OIIO_CHECK_EQUAL(Strutil::ifind("", "abc"), std::string::npos); OIIO_CHECK_EQUAL(Strutil::ifind("", ""), std::string::npos); OIIO_CHECK_EQUAL(Strutil::irfind("abcdeabcde", "bc"), 6); OIIO_CHECK_EQUAL(Strutil::irfind("abcdeabcde", "BC"), 6); OIIO_CHECK_EQUAL(Strutil::irfind("abcdeabcde", "ac"), std::string::npos); OIIO_CHECK_EQUAL(Strutil::irfind("abcdeabcde", ""), 10); + OIIO_CHECK_EQUAL(Strutil::irfind("Xabcdeabcde", "x"), 0); + OIIO_CHECK_EQUAL(Strutil::irfind("abcdeabcdeX", "x"), 10); OIIO_CHECK_EQUAL(Strutil::irfind("", "abc"), std::string::npos); OIIO_CHECK_EQUAL(Strutil::irfind("", ""), std::string::npos);