adjust to match JS version
[ghsmtp.git] / iequal.hpp
blobfb6a8488203bba8ddf73520e988e1d2b41d0f546
1 #ifndef IEQUAL_DOT_HPP
2 #define IEQUAL_DOT_HPP
4 #include <algorithm>
5 #include <cctype>
6 #include <string_view>
8 // Like boost, but ASCII only. Only C locale required.
10 inline bool iequal_char(char a, char b)
12 return std::toupper(static_cast<unsigned char>(a)) ==
13 std::toupper(static_cast<unsigned char>(b));
16 inline bool iequal(std::string_view a, std::string_view b)
18 return (size(a) == size(b)) &&
19 std::equal(begin(b), end(b), begin(a), iequal_char);
22 inline bool iless_char(char a, char b)
24 return std::toupper(static_cast<unsigned char>(a)) <
25 std::toupper(static_cast<unsigned char>(b));
28 inline bool iless(std::string_view a, std::string_view b)
30 return std::lexicographical_compare(begin(a), end(a), begin(b), end(b),
31 iless_char);
34 inline bool istarts_with(std::string_view str, std::string_view prefix)
36 return (str.size() >= prefix.size()) &&
37 iequal(str.substr(0, prefix.size()), prefix);
40 struct ci_less {
41 bool operator()(std::string_view a, std::string_view b) const
43 return iless(a, b);
47 // Ahh, the olden days of loops and pointers...
49 // inline bool iequal(char const* a, char const* b)
50 // {
51 // if (a == b)
52 // return true;
53 // for (;;) {
54 // if (!iequal_char(*a, *b))
55 // return false;
56 // if (*a == '\0')
57 // return true;
58 // ++a;
59 // ++b;
60 // }
61 // }
63 #endif // IEQUAL_DOT_HPP