-
Notifications
You must be signed in to change notification settings - Fork 431
/
text.cpp
171 lines (153 loc) · 3.51 KB
/
text.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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "text.hpp"
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
/**
* Returns an empty string if `s` is valid utf8;
* otherwise returns an error message.
*/
std::string check_utf8(std::string s) {
for (size_t i = 0; i < s.size(); i++) {
size_t fail = 0;
if ((s[i] & 0x80) == 0x80) {
if ((s[i] & 0xE0) == 0xC0) {
if (i + 1 >= s.size() || (s[i + 1] & 0xC0) != 0x80) {
fail = 2;
} else {
i += 1;
}
} else if ((s[i] & 0xF0) == 0xE0) {
if (i + 2 >= s.size() || (s[i + 1] & 0xC0) != 0x80 || (s[i + 2] & 0xC0) != 0x80) {
fail = 3;
} else {
i += 2;
}
} else if ((s[i] & 0xF8) == 0xF0) {
if (i + 3 >= s.size() || (s[i + 1] & 0xC0) != 0x80 || (s[i + 2] & 0xC0) != 0x80 || (s[i + 3] & 0xC0) != 0x80) {
fail = 4;
} else {
i += 3;
}
} else {
fail = 1;
}
}
if (fail != 0) {
std::string out = "\"" + s + "\" is not valid UTF-8 (";
for (size_t j = 0; j < fail && i + j < s.size(); j++) {
if (j != 0) {
out += " ";
}
char tmp[6];
sprintf(tmp, "0x%02X", s[i + j] & 0xFF);
out += std::string(tmp);
}
out += ")";
return out;
}
}
return "";
}
const char *utf8_next(const char *s, long *c) {
if (s == NULL) {
*c = -1;
return NULL;
}
if (*s == '\0') {
*c = -1;
return NULL;
}
if ((s[0] & 0x80) == 0x80) {
if ((s[0] & 0xE0) == 0xC0) {
if ((s[1] & 0xC0) != 0x80) {
*c = 0xFFFD;
s++;
} else {
*c = ((long) (s[0] & 0x1F) << 6) | ((long) (s[1] & 0x7F));
s += 2;
}
} else if ((s[0] & 0xF0) == 0xE0) {
if ((s[1] & 0xC0) != 0x80 || (s[2] & 0xC0) != 0x80) {
*c = 0xFFFD;
s++;
} else {
*c = ((long) (s[0] & 0x0F) << 12) | ((long) (s[1] & 0x7F) << 6) | ((long) (s[2] & 0x7F));
s += 3;
}
} else if ((s[0] & 0xF8) == 0xF0) {
if ((s[1] & 0xC0) != 0x80 || (s[2] & 0xC0) != 0x80 || (s[3] & 0xC0) != 0x80) {
*c = 0xFFFD;
s++;
} else {
*c = ((long) (s[0] & 0x0F) << 18) | ((long) (s[1] & 0x7F) << 12) | ((long) (s[2] & 0x7F) << 6) | ((long) (s[3] & 0x7F));
s += 4;
}
} else {
*c = 0xFFFD;
s++;
}
} else {
*c = s[0];
s++;
}
return s;
}
std::string truncate16(std::string const &s, size_t runes) {
const char *cp = s.c_str();
const char *start = cp;
const char *lastgood = cp;
size_t len = 0;
long c;
while ((cp = utf8_next(cp, &c)) != NULL) {
if (c <= 0xFFFF) {
len++;
} else {
len += 2;
}
if (len <= runes) {
lastgood = cp;
} else {
break;
}
}
return std::string(s, 0, lastgood - start);
}
int integer_zoom(std::string where, std::string text) {
double d = atof(text.c_str());
if (!isfinite(d) || d != floor(d) || d < 0 || d > 32) {
fprintf(stderr, "%s: Expected integer zoom level in \"tippecanoe\" GeoJSON extension, not %s\n", where.c_str(), text.c_str());
exit(EXIT_FAILURE);
}
return d;
}
std::string format_commandline(int argc, char **argv) {
std::string out;
for (int i = 0; i < argc; i++) {
bool need_quote = false;
for (char *cp = argv[i]; *cp != '\0'; cp++) {
if (!isalpha(*cp) && !isdigit(*cp) &&
*cp != '/' && *cp != '-' && *cp != '_' && *cp != '@' && *cp != ':' &&
*cp != '.' && *cp != '%' && *cp != ',') {
need_quote = true;
break;
}
}
if (need_quote) {
out.push_back('\'');
for (char *cp = argv[i]; *cp != '\0'; cp++) {
if (*cp == '\'') {
out.append("'\"'\"'");
} else {
out.push_back(*cp);
}
}
out.push_back('\'');
} else {
out.append(argv[i]);
}
if (i + 1 < argc) {
out.push_back(' ');
}
}
return out;
}