-
Notifications
You must be signed in to change notification settings - Fork 4
/
case_insensitive_char_traits.cpp
57 lines (50 loc) · 1.65 KB
/
case_insensitive_char_traits.cpp
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
// Case-insensitive strings via changing the char trait
// Example taken from http://en.cppreference.com/w/cpp/string/char_traits
// which got it from http://www.gotw.ca/gotw/029.htm
#include <cctype>
#include <cstddef>
#include <iostream>
#include <string>
struct ci_char_traits : public std::char_traits<char> {
static bool eq(char c1, char c2) { return ::toupper(c1) == ::toupper(c2); }
static bool ne(char c1, char c2) { return ::toupper(c1) != ::toupper(c2); }
static bool lt(char c1, char c2) { return ::toupper(c1) < ::toupper(c2); }
static int compare(const char* s1, const char* s2, std::size_t n) {
while (n-- != 0) {
if (::toupper(*s1) < ::toupper(*s2)) {
return -1;
}
if (::toupper(*s1) > ::toupper(*s2)) {
return 1;
}
++s1;
++s2;
}
return 0;
}
static const char* find(const char* s, int n, char a) {
while (n-- > 0 && ::toupper(*s) != ::toupper(a)) {
++s;
}
return s;
}
};
// case-insensitive string
using ci_string = std::basic_string<char, ci_char_traits>;
// need to overwrite the insertion and extraction operators
std::ostream& operator<<(std::ostream& os, const ci_string& str) {
return os.write(str.data(), str.size());
}
std::istream& operator>>(std::istream& os, ci_string& str) {
std::string tmp;
os >> tmp;
str.assign(tmp.data(), tmp.size());
return os;
}
int main() {
ci_string s1{"Test"}, s2{"test"};
std::cout << std::boolalpha << (s1 == s2) << std::endl;
ci_string str;
std::cin >> str;
std::cout << str << std::endl;
}