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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
/******************************************************************************
*
* Project: PROJ
* Purpose: ISO19111:2018 implementation
* Author: Even Rouault <even dot rouault at spatialys dot com>
*
******************************************************************************
* Copyright (c) 2018, Even Rouault <even dot rouault at spatialys dot com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#ifndef FROM_PROJ_CPP
#define FROM_PROJ_CPP
#endif
#include "proj/internal/internal.hpp"
#include <cstdint>
#include <cstring>
#ifdef _MSC_VER
#include <string.h>
#else
#include <strings.h>
#endif
#include <exception>
#include <iomanip> // std::setprecision
#include <locale>
#include <sstream> // std::istringstream and std::ostringstream
#include <string>
NS_PROJ_START
namespace internal {
// ---------------------------------------------------------------------------
/**
* Replace all occurrences of before with after.
*/
std::string replaceAll(const std::string &str, const std::string &before,
const std::string &after) {
std::string ret(str);
const size_t nBeforeSize = before.size();
const size_t nAfterSize = after.size();
if (nBeforeSize) {
size_t nStartPos = 0;
while ((nStartPos = ret.find(before, nStartPos)) != std::string::npos) {
ret.replace(nStartPos, nBeforeSize, after);
nStartPos += nAfterSize;
}
}
return ret;
}
// ---------------------------------------------------------------------------
inline static bool EQUALN(const char *a, const char *b, size_t size) {
#ifdef _MSC_VER
return _strnicmp(a, b, size) == 0;
#else
return strncasecmp(a, b, size) == 0;
#endif
}
/**
* Case-insensitive equality test
*/
bool ci_equal(const std::string &a, const std::string &b) noexcept {
const auto size = a.size();
if (size != b.size()) {
return false;
}
return EQUALN(a.c_str(), b.c_str(), size);
}
bool ci_equal(const std::string &a, const char *b) noexcept {
const auto size = a.size();
if (size != strlen(b)) {
return false;
}
return EQUALN(a.c_str(), b, size);
}
bool ci_equal(const char *a, const char *b) noexcept {
const auto size = strlen(a);
if (size != strlen(b)) {
return false;
}
return EQUALN(a, b, size);
}
// ---------------------------------------------------------------------------
bool ci_less(const std::string &a, const std::string &b) noexcept {
#ifdef _MSC_VER
return _stricmp(a.c_str(), b.c_str()) < 0;
#else
return strcasecmp(a.c_str(), b.c_str()) < 0;
#endif
}
// ---------------------------------------------------------------------------
/**
* Convert to lower case.
*/
std::string tolower(const std::string &str)
{
std::string ret(str);
for (size_t i = 0; i < ret.size(); i++)
ret[i] = static_cast<char>(::tolower(ret[i]));
return ret;
}
// ---------------------------------------------------------------------------
/**
* Convert to upper case.
*/
std::string toupper(const std::string &str)
{
std::string ret(str);
for (size_t i = 0; i < ret.size(); i++)
ret[i] = static_cast<char>(::toupper(ret[i]));
return ret;
}
// ---------------------------------------------------------------------------
/** Strip leading and trailing double quote characters */
std::string stripQuotes(const std::string &str) {
if (str.size() >= 2 && str[0] == '"' && str.back() == '"') {
return str.substr(1, str.size() - 2);
}
return str;
}
// ---------------------------------------------------------------------------
size_t ci_find(const std::string &str, const char *needle) noexcept {
const size_t needleSize = strlen(needle);
for (size_t i = 0; i + needleSize <= str.size(); i++) {
if (EQUALN(str.c_str() + i, needle, needleSize)) {
return i;
}
}
return std::string::npos;
}
// ---------------------------------------------------------------------------
size_t ci_find(const std::string &str, const std::string &needle,
size_t startPos) noexcept {
const size_t needleSize = needle.size();
for (size_t i = startPos; i + needleSize <= str.size(); i++) {
if (EQUALN(str.c_str() + i, needle.c_str(), needleSize)) {
return i;
}
}
return std::string::npos;
}
// ---------------------------------------------------------------------------
/*
bool starts_with(const std::string &str, const std::string &prefix) noexcept {
if (str.size() < prefix.size()) {
return false;
}
return std::memcmp(str.c_str(), prefix.c_str(), prefix.size()) == 0;
}
*/
// ---------------------------------------------------------------------------
/*
bool starts_with(const std::string &str, const char *prefix) noexcept {
const size_t prefixSize = std::strlen(prefix);
if (str.size() < prefixSize) {
return false;
}
return std::memcmp(str.c_str(), prefix, prefixSize) == 0;
}
*/
// ---------------------------------------------------------------------------
bool ci_starts_with(const char *str, const char *prefix) noexcept {
const auto str_size = strlen(str);
const auto prefix_size = strlen(prefix);
if (str_size < prefix_size) {
return false;
}
return EQUALN(str, prefix, prefix_size);
}
// ---------------------------------------------------------------------------
bool ci_starts_with(const std::string &str,
const std::string &prefix) noexcept {
if (str.size() < prefix.size()) {
return false;
}
return EQUALN(str.c_str(), prefix.c_str(), prefix.size());
}
// ---------------------------------------------------------------------------
bool ends_with(const std::string &str, const std::string &suffix) noexcept {
if (str.size() < suffix.size()) {
return false;
}
return std::memcmp(str.c_str() + str.size() - suffix.size(), suffix.c_str(),
suffix.size()) == 0;
}
// ---------------------------------------------------------------------------
double c_locale_stod(const std::string &s) {
const auto s_size = s.size();
// Fast path
if (s_size > 0 && s_size < 15) {
std::int64_t acc = 0;
std::int64_t div = 1;
bool afterDot = false;
size_t i = 0;
if (s[0] == '-') {
++i;
div = -1;
} else if (s[0] == '+') {
++i;
}
for (; i < s_size; ++i) {
const auto ch = s[i];
if (ch >= '0' && ch <= '9') {
acc = acc * 10 + ch - '0';
if (afterDot) {
div *= 10;
}
} else if (ch == '.') {
afterDot = true;
} else {
div = 0;
}
}
if (div) {
return static_cast<double>(acc) / div;
}
}
std::istringstream iss(s);
iss.imbue(std::locale::classic());
double d;
iss >> d;
if (!iss.eof() || iss.fail()) {
throw std::invalid_argument("non double value");
}
return d;
}
// ---------------------------------------------------------------------------
std::vector<std::string> split(const std::string &str, char separator) {
std::vector<std::string> res;
size_t lastPos = 0;
size_t newPos = 0;
while ((newPos = str.find(separator, lastPos)) != std::string::npos) {
res.push_back(str.substr(lastPos, newPos - lastPos));
lastPos = newPos + 1;
}
res.push_back(str.substr(lastPos));
return res;
}
// ---------------------------------------------------------------------------
std::string toString(int val) {
std::ostringstream buffer;
buffer.imbue(std::locale::classic());
buffer << val;
return buffer.str();
}
std::string toString(double val, int precision) {
std::ostringstream buffer;
buffer.imbue(std::locale::classic());
buffer << std::setprecision(precision);
buffer << val;
auto str = buffer.str();
if (precision == 15 && str.find("9999999999") != std::string::npos) {
buffer.str("");
buffer.clear();
buffer << std::setprecision(14);
buffer << val;
return buffer.str();
}
return str;
}
// ---------------------------------------------------------------------------
std::string concat(const char *a, const std::string &b) {
std::string res(a);
res += b;
return res;
}
std::string concat(const char *a, const std::string &b, const char *c) {
std::string res(a);
res += b;
res += c;
return res;
}
// ---------------------------------------------------------------------------
} // namespace internal
NS_PROJ_END
|