-
Notifications
You must be signed in to change notification settings - Fork 0
/
sanitize.hpp
42 lines (31 loc) · 911 Bytes
/
sanitize.hpp
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
#pragma once
#include <string>
#include <iostream>
void sanitize(std::string &);
bool shouldFilter(const char &);
// Sanitizes a given word by removing non-essential characters from the start
// and end.
void sanitize(std::string & word) {
size_t start_index = 0;
size_t end_index = word.size() - 1;
while (shouldFilter(word[start_index])) {
start_index++;
}
while (end_index <= word.size() - 1 && shouldFilter(word[end_index])) {
end_index--;
}
if (start_index != 0 || end_index != word.size() - 1) {
size_t len = end_index - start_index;
word = word.substr(start_index, len + 1);
}
}
// Returns true if character should be filtered out.
bool shouldFilter(const char & target) {
char bad_char[] = { ' ', '_', '-', '\"', '\'', '(', ')' };
for (unsigned int i = 0; i < 7; ++i) {
if (target == bad_char[i]) {
return true;
}
}
return false;
}