Repository navigation
Expand file tree
/
Copy pathstring.ixx
More file actions
150 lines (127 loc) · 5.02 KB
/
Copy pathstring.ixx
File metadata and controls
150 lines (127 loc) · 5.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Copyright (c) 2024 The RefValue Project
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
module;
#include <essence/compat.hpp>
export module essence.basic:string;
import std;
import :abi.string;
import :char_and_byte;
import :hashing;
export namespace essence {
ES_API(CPPESSENCE) constexpr std::string_view ascii_blank_chars{" \t"};
/**
* A hash function for const char*, std::string_view and std::string.
*/
struct string_hash {
using hash_type = std::hash<std::string_view>;
using is_transparent = void;
ES_API(CPPESSENCE) std::size_t operator()(const char* str) const {
return hash_type{}(str);
}
ES_API(CPPESSENCE) std::size_t operator()(std::string_view str) const {
return hash_type{}(str);
}
ES_API(CPPESSENCE) std::size_t operator()(const std::string& str) const {
return hash_type{}(str);
}
};
/**
* A case-insensitive hash function for const char*, std::string_view and std::string.
*/
struct icase_string_hash {
using is_transparent = void;
ES_API(CPPESSENCE) std::size_t operator()(const char* str) const {
return (*this)(std::string_view{str});
}
ES_API(CPPESSENCE) std::size_t operator()(const std::string& str) const {
return (*this)(std::string_view{str});
}
ES_API(CPPESSENCE) std::size_t operator()(std::string_view value) const {
std::size_t result{};
std::ranges::for_each(value, [&](char c) { hash_combine(result, to_lower(c)); });
return result;
}
};
/**
* A case-insensitive equality comparer for two strings.
*/
struct icase_string_comparer {
constexpr bool operator()(std::string_view left, std::string_view right) const {
return left.size() == right.size() && std::ranges::equal(left, right, [](char c1, char c2) {
return c1 == c2 || to_lower(c1) == to_lower(c2);
});
}
};
/**
* Checks whether the first string is lexicographically less than the second string,
in a case-insensitive way.
*/
struct icase_lexicographical_compare {
constexpr bool operator()(std::string_view left, std::string_view right) const {
return std::ranges::lexicographical_compare(
left, right, [](char c1, char c2) { return to_lower(c1) < to_lower(c2); });
}
};
/**
* Removes a group of characters from the beginning of a string.
* @param str The string.
* @param group The group of characters.
* @return The result.
*/
constexpr std::string_view trim_left(std::string_view str, std::string_view group = ascii_blank_chars) noexcept {
if (const auto index = str.find_first_not_of(group); index != std::string_view::npos) {
return str.substr(index);
}
return str;
}
/**
* Removes a group of characters from the end of a string.
* @param str The string.
* @param group The group of characters.
* @return The result.
*/
constexpr std::string_view trim_right(std::string_view str, std::string_view group = ascii_blank_chars) noexcept {
return str.substr(0, str.find_last_not_of(group) + 1);
}
/**
* Removes a group of characters from the beginning and the end of a string.
* @param str The string.
* @param group The group of characters.
* @return The result.
*/
constexpr std::string_view trim(std::string_view str, std::string_view group = ascii_blank_chars) noexcept {
return trim_right(trim_left(str, group), group);
}
/**
* Lowercases a string.
* @param str The string.
* @return The result.
*/
ES_API(CPPESSENCE) abi::string to_lower(std::string_view str);
/**
* Capitalizes a string.
* @param str The string.
* @return The result.
*
*/
ES_API(CPPESSENCE) abi::string to_upper(std::string_view str);
} // namespace essence