-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathalgorithms.hpp
More file actions
135 lines (125 loc) · 3.03 KB
/
algorithms.hpp
File metadata and controls
135 lines (125 loc) · 3.03 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
#pragma once
#include <cstddef>
template<class chtype>
struct Algorithms {
typedef chtype char_type;
static inline bool isdigit(chtype c) {
return c >= L'0' && c <= L'9';
}
static inline bool isupper(chtype c) {
return c >= L'A' && c <= L'Z';
}
static inline bool islower(chtype c) {
return c >= L'a' && c <= L'z';
}
static inline chtype toupper(chtype c) {
if(islower(c)) {
return c - (L'a' - L'A');
}
return c;
}
static inline chtype tolower(chtype c) {
if(isupper(c)) {
return c + (L'a' - L'A');
}
return c;
}
struct Camelize {
inline static bool Run(const chtype* str, size_t len, chtype* out) {
unsigned int j = 0;
bool changed = false;
if(isdigit(str[0]) || str[0] == L'-') {
return false;
}
for(unsigned int i = 0; i < len; ++i) {
auto c = str[i];
if(c == L'_' || c == L' ' || c == L'-') {
changed = true;
c = str[++i];
if(c == 0) {
return false;
}
out[j++] = toupper(c);
} else if(i == 0 && isupper(c)) {
changed = true;
out[j++] = tolower(c);
} else {
out[j++] = c;
}
}
out[j] = 0;
return changed;
}
};
struct Decamelize {
inline static bool Run(const chtype* str, size_t len, chtype* out, chtype separator = L'_') {
unsigned int j = 0;
if(!islower(str[0])) {
return false;
}
bool changed = false;
for(unsigned int i = 0; i < len; ++i) {
auto c = str[i];
if(isupper(c)) {
out[j++] = separator;
out[j++] = tolower(c);
changed = true;
} else {
out[j++] = c;
}
}
out[j] = 0;
return changed;
}
};
struct Pascalize {
inline static bool Run(const chtype* str, size_t len, chtype* out) {
unsigned int j = 0;
bool changed = false;
if(isdigit(str[0]) || str[0] == L'-') {
return false;
}
for(unsigned int i = 0; i < len; ++i) {
auto c = str[i];
if(c == L'_' || c == L' ' || c == L'-') {
changed = true;
c = str[++i];
if(c == 0) {
return false;
}
out[j++] = toupper(c);
} else if(i == 0 && islower(c)) {
changed = true;
out[j++] = toupper(c);
} else {
out[j++] = c;
}
}
out[j] = 0;
return changed;
}
};
struct Depascalize {
inline static bool Run(const chtype* str, size_t len, chtype* out, chtype separator = L'_') {
unsigned int j = 0;
if(!isupper(str[0])) {
return false;
}
bool changed = false;
for(unsigned int i = 0; i < len; ++i) {
auto c = str[i];
if(isupper(c)) {
if(i > 0) {
out[j++] = separator;
}
out[j++] = tolower(c);
changed = true;
} else {
out[j++] = c;
}
}
out[j] = 0;
return changed;
}
};
};