-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathstring_filters.hpp
More file actions
152 lines (118 loc) · 4.95 KB
/
string_filters.hpp
File metadata and controls
152 lines (118 loc) · 4.95 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
151
152
#pragma once
#ifndef STRING_FILTERS_HPP
#define STRING_FILTERS_HPP
#include <string>
#include <algorithm>
#include <cctype>
#include <cstring>
#include <iterator>
#include <unordered_set>
#include <vector>
#include <ranges>
#include <string_view>
#include "strings_util.hpp"
namespace filter {
/**
* Utility that filters a string to include only legal characters.
* Usage: std::string result = legal_characters(input);
* Or with pipe syntax: std::string result = input | legal_characters;
*/
struct legal_characters_t {
std::string process(const std::string& str) const {
static const std::unordered_set<char> legal_special_chars {
' ', '!', ',', '.', '{', '}', '@', '#', '$', '%', '^', '&', '*',
'-', '_', '+', '=', ':', '<', '>', '?', '/', '~', '\'', '\\', '`'
};
auto is_legal = [](char c) { return std::isalnum(c) || legal_special_chars.count(c) > 0; };
std::string result;
result.reserve(str.size());
std::copy_if(str.begin(), str.end(), std::back_inserter(result), [is_legal](char c) { return is_legal(c); });
if (result.find_first_not_of(' ') == std::string::npos) result.clear();
return result;
}
std::string operator()(const std::string& str) const { return process(str); }
friend std::string operator|(const std::string& str, const legal_characters_t& filter) { return filter(str); }
};
// Not constexpr due to the unordered_set member
// Not constexpr due to the unordered_set member
inline legal_characters_t legal_characters{};
/**
* Utility that strips text after the last '[' or '(' character.
* Usage: std::string result = strip_number(input);
* Or with pipe syntax: std::string result = input | strip_number;
*/
struct strip_number_t {
std::string process(const std::string& str) const {
auto pos = str.find_last_of("[(");
if (pos == std::string::npos) return str;
auto trim_pos = pos;
// NOTE: this should probably be a while loop, but there are units who named themselve with trailing spaces
// and I don't want to change that at this time.
if (trim_pos > 0 && std::isspace(str[trim_pos-1])) trim_pos--;
return str.substr(0, trim_pos);
}
std::string operator()(const std::string& str) const { return process(str); }
friend std::string operator|(const std::string& str, const strip_number_t& stripper) { return stripper(str); }
};
inline constexpr strip_number_t strip_number{};
/**
* Utility that capitalizes a string.
* Usage: std::string result = capitalize(input);
* Or with pipe syntax: std::string result = input | capitalize;
*/
struct capitalize_t {
std::string process(const std::string& str) const {
std::string result = str;
if (!result.empty()) {
result[0] = std::toupper(result[0]);
}
return result;
}
std::string operator()(const std::string& str) const { return process(str); }
friend std::string operator|(const std::string& str, const capitalize_t& capitalizer) { return capitalizer(str); }
};
inline constexpr capitalize_t capitalize{};
/**
* Utility that lowercases a string.
* Usage: std::string result = lowercase(input);
* Or with pipe syntax: std::string result = input | lowercase;
*/
struct lowercase_t {
std::string process(const std::string& str) const {
std::string result = str;
std::transform(result.begin(), result.end(), result.begin(), ::tolower);
return result;
}
std::string operator()(const std::string& str) const { return process(str); }
friend std::string operator|(const std::string& str, const lowercase_t& lowercaser) { return lowercaser(str); }
};
inline constexpr lowercase_t lowercase{};
/**
* Utility that canonicalizes a string.
* It capitalizes each word and replaces spaces between words with underscores.
* Example: "summon wind" -> "Summon_Wind"
* Usage: std::string result = canonicalize(input);
* Or with pipe syntax: std::string result = input | canonicalize;
*/
struct canonicalize_t {
std::string process(const std::string& str) const {
auto parts_view = str
| std::views::split(std::string_view{" "}) // Split by space
| std::views::transform([](auto&& subrange) {
std::string str = std::string(subrange.begin(), subrange.end());
return str | capitalize;
});
std::vector<std::string> processed_words;
// Efficiently copy the view elements into the vector
std::ranges::copy(parts_view, std::back_inserter(processed_words));
// Join the processed words with an underscore
return strings::join(processed_words, "_");
}
std::string operator()(const std::string& str) const { return process(str); }
friend std::string operator|(const std::string& str, const canonicalize_t& canonicalizer) {
return canonicalizer(str);
}
};
inline constexpr canonicalize_t canonicalize{};
} // namespace filter
#endif // STRING_FILTERS_HPP