Line data Source code
1 : #ifndef THINGER_UTIL_ASCII_HPP
2 : #define THINGER_UTIL_ASCII_HPP
3 :
4 : #include <string>
5 : #include <string_view>
6 :
7 : // ASCII case folding, for protocol elements (header names, tokens, host names). Unlike
8 : // boost::iequals or boost::to_lower, it does not depend on the global locale, and does not
9 : // copy it on each call (its reference count is shared by every thread).
10 : namespace thinger::util::ascii {
11 :
12 29876478 : constexpr char to_lower(char c) {
13 29876478 : return c >= 'A' && c <= 'Z' ? static_cast<char>(c - 'A' + 'a') : c;
14 : }
15 :
16 5111524 : constexpr bool iequals(std::string_view a, std::string_view b) {
17 5111524 : if (a.size() != b.size()) return false;
18 12604074 : for (size_t i = 0; i < a.size(); ++i) {
19 11582923 : if (to_lower(a[i]) != to_lower(b[i])) return false;
20 : }
21 1021151 : return true;
22 : }
23 :
24 2767 : constexpr bool istarts_with(std::string_view value, std::string_view prefix) {
25 2767 : return value.size() >= prefix.size() && iequals(value.substr(0, prefix.size()), prefix);
26 : }
27 :
28 742174 : inline void to_lower(std::string& value) {
29 6944231 : for (auto& c : value) c = to_lower(c);
30 742174 : }
31 :
32 : }
33 :
34 : #endif
|