OSSIA
Open Scenario System for Interactive Application
Loading...
Searching...
No Matches
case_insensitive.hpp
1#pragma once
2#include <ossia/detail/config.hpp>
3
5#include <ossia/detail/hash.hpp>
6#include <ossia/detail/hash_map.hpp>
7
8namespace ossia
9{
10static constexpr unsigned char ascii_tolower(unsigned char c) noexcept
11{
12 constexpr unsigned char A = 'A', Z = 'Z', a = 'a';
13 if(c >= A && c <= Z)
14 return c - (A - a);
15 else
16 return c;
17}
18
19struct ascii_tolower_less
20{
21 constexpr bool operator()(unsigned char c1, unsigned char c2) const noexcept
22 {
23 return ascii_tolower(c1) < ascii_tolower(c2);
24 }
25};
26
27struct ascii_tolower_equal
28{
29 constexpr bool operator()(unsigned char c1, unsigned char c2) const noexcept
30 {
31 return ascii_tolower(c1) == ascii_tolower(c2);
32 }
33};
34
35struct case_insensitive_comparator
36{
37 using is_transparent = std::true_type;
38 bool operator()(std::string_view s1, std::string_view s2) const noexcept
39 {
40 return std::lexicographical_compare(
41 s1.begin(), s1.end(), s2.begin(), s2.end(), ascii_tolower_less{});
42 }
43};
44
45struct case_insensitive_hash
46{
47 static constexpr int buffer_size = 64;
48 using is_transparent = std::true_type;
49 using is_avalanching = std::true_type;
50
51 uint64_t operator()(std::string_view s1) const noexcept
52 {
53
54 uint64_t res = UINT64_C(0xff51afd7ed558ccd);
55 unsigned char buf[buffer_size + 1];
56 const char* ptr = s1.data();
57 uint64_t remaining = s1.size();
58
59 while(remaining > 0)
60 {
61 if(remaining >= buffer_size)
62 {
63 for(int i = 0; i < buffer_size; i++)
64 buf[i] = ascii_tolower(ptr[i]);
65 res = rapidhash_withSeed(buf, buffer_size, res);
66 remaining -= buffer_size;
67 ptr += buffer_size;
68 }
69 else
70 {
71 [[likely]];
72 for(uint64_t i = 0; i < remaining; i++)
73 buf[i] = ascii_tolower(ptr[i]);
74 res = rapidhash_withSeed(buf, remaining, res);
75 remaining = 0;
76 }
77 }
78
79 return res;
80 }
81};
82
83struct case_insensitive_equal
84{
85 using is_transparent = std::true_type;
86 constexpr bool operator()(std::string_view s1, std::string_view s2) const noexcept
87 {
88 return std::equal(s1.begin(), s1.end(), s2.begin(), s2.end(), ascii_tolower_equal{});
89 }
90};
91
92template <typename Value>
93using case_insensitive_string_map
94 = ossia::hash_map<std::string, Value, case_insensitive_hash, case_insensitive_equal>;
95}
Definition git_info.h:7