-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringutil.cpp
57 lines (51 loc) · 1.04 KB
/
stringutil.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
#include <assert.h>
#include <ctype.h>
#include <regex.h>
#include <string.h>
#include "stringutil.h"
bool endsWith(const char *s, const char *suffix) {
if (!s || !suffix) {
return false;
}
int len = strlen(s);
int lens = strlen(suffix);
if (lens > len) {
return false;
}
return !strncmp(s + len - lens, suffix, lens);
}
char* trim(char *s) {
while (isspace(*s)) {
s++;
}
char *t = s + strlen(s) - 1;
while ((t > s) && isspace(*t)) {
t--;
}
*(t + 1) = '\0';
return s;
}
char* unquote(char *s) {
int len = strlen(s);
if (len >= 2) {
char *last = s + strlen(s) - 1;
if (*s == '"' && *last == '"') {
s++;
*last = '\0';
}
}
return s;
}
char* split(char *s, char separator) {
char *t = strchr(s, separator);
if (!t) {
return NULL;
}
*t = '\0';
return t + 1;
}
bool isFen(string s) {
regex_t r;
assert(!regcomp(&r, "([a-z1-8]+/){7}[a-z1-8]+ [bw] [-kq]+ [-a-h1-8]+ [0-9]+ [0-9]+", REG_EXTENDED | REG_ICASE | REG_NOSUB));
return !regexec(&r, s.c_str(), 0, NULL, 0);
}