diff options
author | Dan Goodliffe <dan@randomdan.homeip.net> | 2019-10-19 13:18:03 +0100 |
---|---|---|
committer | Dan Goodliffe <dan@randomdan.homeip.net> | 2019-10-19 13:18:03 +0100 |
commit | 615472ff4b8d17f3745d023a1b20914e2853d160 (patch) | |
tree | 2f9de7e412cd6ebaf11d948a8e1deb5695c2aeac | |
parent | Performance tweaks (diff) | |
download | slicer-615472ff4b8d17f3745d023a1b20914e2853d160.tar.bz2 slicer-615472ff4b8d17f3745d023a1b20914e2853d160.tar.xz slicer-615472ff4b8d17f3745d023a1b20914e2853d160.zip |
Template and inline case_less strncasecmp wrapper
-rw-r--r-- | slicer/slicer/modelParts.cpp | 7 | ||||
-rw-r--r-- | slicer/slicer/modelParts.h | 9 | ||||
-rw-r--r-- | slicer/test/serializers.cpp | 21 |
3 files changed, 18 insertions, 19 deletions
diff --git a/slicer/slicer/modelParts.cpp b/slicer/slicer/modelParts.cpp index 6f68e9c..61cbd25 100644 --- a/slicer/slicer/modelParts.cpp +++ b/slicer/slicer/modelParts.cpp @@ -89,12 +89,5 @@ namespace Slicer { { ch(this->name, modelPart, this); } - - bool - case_less::operator()(std::string_view && a, std::string_view && b) const - { - const auto cmp = strncasecmp(a.data(), b.data(), std::min(a.length(), b.length())); - return (cmp < 0) || (!cmp && a.length() < b.length()); - } } diff --git a/slicer/slicer/modelParts.h b/slicer/slicer/modelParts.h index 9d6384e..3ec4201 100644 --- a/slicer/slicer/modelParts.h +++ b/slicer/slicer/modelParts.h @@ -116,8 +116,13 @@ namespace Slicer { const std::string name; }; - struct DLL_PUBLIC case_less { - bool operator()(std::string_view && lhs, std::string_view && rhs) const; + struct case_less { + template<typename A, typename B> + inline bool operator()(const A & a, const B & b) const + { + const auto cmp = strncasecmp(a.data(), b.data(), std::min(a.length(), b.length())); + return (cmp < 0) || (!cmp && a.length() < b.length()); + } }; class DLL_PUBLIC ModelPart : public std::enable_shared_from_this<ModelPart> { diff --git a/slicer/test/serializers.cpp b/slicer/test/serializers.cpp index 9b0ef95..bf50b22 100644 --- a/slicer/test/serializers.cpp +++ b/slicer/test/serializers.cpp @@ -665,17 +665,18 @@ BOOST_FIXTURE_TEST_SUITE(l, Slicer::case_less); BOOST_AUTO_TEST_CASE(case_less_test) { + using namespace std::literals; const auto & lc { *this }; - BOOST_CHECK(!lc("", "")); - BOOST_CHECK(lc("a", "b")); - BOOST_CHECK(lc("A", "b")); - BOOST_CHECK(lc("Aa", "b")); - BOOST_CHECK(lc("AA", "b")); - BOOST_CHECK(lc("aA", "b")); - BOOST_CHECK(lc("A", "B")); - BOOST_CHECK(lc("Aa", "Bb")); - BOOST_CHECK(lc("AA", "bB")); - BOOST_CHECK(lc("aA", "BB")); + BOOST_CHECK(!lc(""sv, ""sv)); + BOOST_CHECK(lc("a"sv, "b"sv)); + BOOST_CHECK(lc("A"sv, "b"sv)); + BOOST_CHECK(lc("Aa"sv, "b"sv)); + BOOST_CHECK(lc("AA"sv, "b"sv)); + BOOST_CHECK(lc("aA"sv, "b"s)); + BOOST_CHECK(lc("A"s, "B"sv)); + BOOST_CHECK(lc("Aa"sv, "Bb"s)); + BOOST_CHECK(lc("AA"s, "bB"s)); + BOOST_CHECK(lc("aA"s, "BB"s)); } BOOST_AUTO_TEST_SUITE_END(); |