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 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
|
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc. All rights reserved.
// http://code.google.com/p/protobuf/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// from google3/strings/strutil.cc
#include <errno.h>
#include <float.h> // FLT_DIG and DBL_DIG
#include <limits.h>
#include <stdio.h>
#include <iterator>
#include <limits>
#include <string>
#include <vector>
#include "protoc/StrUtil.h"
#if HAVE_CONFIG_H
#include <config.h>
#endif // HAVE_CONFIG_H
// Required for Protobuf 3.7 onwards
#ifdef HAVE_GOOGLE_PROTOBUF_IO_STRTOD_H
#include <google/protobuf/io/strtod.h>
#endif // HAVE_GOOGLE_PROTOBUF_IO_STRTOD_H
#ifdef HAVE_GOOGLE_PROTOBUF_STUBS_LOGGING_H
#include <google/protobuf/stubs/logging.h>
#endif // HAVE_GOOGLE_PROTOBUF_STUBS_LOGGING_H
#ifdef HAVE_GOOGLE_PROTOBUF_STUBS_STL_UTIL_H
#include <google/protobuf/stubs/stl_util.h>
#endif // HAVE_GOOGLE_PROTOBUF_STUBS_STL_UTIL_H
#ifdef _WIN32
// MSVC has only _snprintf, not snprintf.
//
// MinGW has both snprintf and _snprintf, but they appear to be different
// functions. The former is buggy. When invoked like so:
// char buffer[32];
// snprintf(buffer, 32, "%.*g\n", FLT_DIG, 1.23e10f);
// it prints "1.23000e+10". This is plainly wrong: %g should never print
// trailing zeros after the decimal point. For some reason this bug only
// occurs with some input values, not all. In any case, _snprintf does the
// right thing, so we use it.
#define snprintf _snprintf
#endif // _WIN32
namespace ola {
// ----------------------------------------------------------------------
// StringReplace()
// Replace the "old" pattern with the "new" pattern in a string,
// and append the result to "res". If replace_all is false,
// it only replaces the first instance of "old."
// ----------------------------------------------------------------------
void StringReplace(const string& s, const string& oldsub,
const string& newsub, bool replace_all,
string* res) {
if (oldsub.empty()) {
res->append(s); // if empty, append the given string.
return;
}
string::size_type start_pos = 0;
string::size_type pos;
do {
pos = s.find(oldsub, start_pos);
if (pos == string::npos) {
break;
}
res->append(s, start_pos, pos - start_pos);
res->append(newsub);
start_pos = pos + oldsub.size(); // start searching again after the "old"
} while (replace_all);
res->append(s, start_pos, s.length() - start_pos);
}
// ----------------------------------------------------------------------
// StringReplace()
// Give me a string and two patterns "old" and "new", and I replace
// the first instance of "old" in the string with "new", if it
// exists. If "global" is true; call this repeatedly until it
// fails. RETURN a new string, regardless of whether the replacement
// happened or not.
// ----------------------------------------------------------------------
string StringReplace(const string& s, const string& oldsub,
const string& newsub, bool replace_all) {
string ret;
StringReplace(s, oldsub, newsub, replace_all, &ret);
return ret;
}
// ----------------------------------------------------------------------
// SplitStringUsing()
// Split a string using a character delimiter. Append the components
// to 'result'.
//
// Note: For multi-character delimiters, this routine will split on *ANY* of
// the characters in the string, not the entire string as a single delimiter.
// ----------------------------------------------------------------------
template <typename ITR>
static inline
void SplitStringToIteratorUsing(const string& full,
const char* delim,
ITR* result) {
// Optimize the common case where delim is a single character.
if (delim[0] != '\0' && delim[1] == '\0') {
char c = delim[0];
const char* p = full.data();
const char* end = p + full.size();
while (p != end) {
if (*p == c) {
++p;
} else {
const char* start = p;
while (++p != end && *p != c) {
}
*(*result)++ = string(start, p - start);
}
}
return;
}
string::size_type begin_index, end_index;
begin_index = full.find_first_not_of(delim);
while (begin_index != string::npos) {
end_index = full.find_first_of(delim, begin_index);
if (end_index == string::npos) {
*(*result)++ = full.substr(begin_index);
return;
}
*(*result)++ = full.substr(begin_index, (end_index - begin_index));
begin_index = full.find_first_not_of(delim, end_index);
}
}
void SplitStringUsing(const string& full,
const char* delim,
vector<string>* result) {
std::back_insert_iterator< vector<string> > it(*result);
SplitStringToIteratorUsing(full, delim, &it);
}
// Protocol buffers doesn't ever care about errors, but I don't want to remove
// the code.
#define LOG_STRING(LEVEL, VECTOR) GOOGLE_LOG_IF(LEVEL, false)
// ----------------------------------------------------------------------
// FastIntToBuffer()
// FastInt64ToBuffer()
// FastHexToBuffer()
// FastHex64ToBuffer()
// FastHex32ToBuffer()
// ----------------------------------------------------------------------
// Offset into buffer where FastInt64ToBuffer places the end of string
// null character. Also used by FastInt64ToBufferLeft.
static const int kFastInt64ToBufferOffset = 21;
char *FastInt64ToBuffer(int64_t i, char* buffer) {
// We could collapse the positive and negative sections, but that
// would be slightly slower for positive numbers...
// 22 bytes is enough to store -2**64, -18446744073709551616.
char* p = buffer + kFastInt64ToBufferOffset;
*p-- = '\0';
if (i >= 0) {
do {
*p-- = '0' + i % 10;
i /= 10;
} while (i > 0);
return p + 1;
} else {
// On different platforms, % and / have different behaviors for
// negative numbers, so we need to jump through hoops to make sure
// we don't divide negative numbers.
if (i > -10) {
i = -i;
*p-- = '0' + i;
*p = '-';
return p;
} else {
// Make sure we aren't at MIN_INT, in which case we can't say i = -i
i = i + 10;
i = -i;
*p-- = '0' + i % 10;
// Undo what we did a moment ago
i = i / 10 + 1;
do {
*p-- = '0' + i % 10;
i /= 10;
} while (i > 0);
*p = '-';
return p;
}
}
}
// Offset into buffer where FastInt32ToBuffer places the end of string
// null character. Also used by FastInt32ToBufferLeft
static const int kFastInt32ToBufferOffset = 11;
// Yes, this is a duplicate of FastInt64ToBuffer. But, we need this for the
// compiler to generate 32 bit arithmetic instructions. It's much faster, at
// least with 32 bit binaries.
char *FastInt32ToBuffer(int32_t i, char* buffer) {
// We could collapse the positive and negative sections, but that
// would be slightly slower for positive numbers...
// 12 bytes is enough to store -2**32, -4294967296.
char* p = buffer + kFastInt32ToBufferOffset;
*p-- = '\0';
if (i >= 0) {
do {
*p-- = '0' + i % 10;
i /= 10;
} while (i > 0);
return p + 1;
} else {
// On different platforms, % and / have different behaviors for
// negative numbers, so we need to jump through hoops to make sure
// we don't divide negative numbers.
if (i > -10) {
i = -i;
*p-- = '0' + i;
*p = '-';
return p;
} else {
// Make sure we aren't at MIN_INT, in which case we can't say i = -i
i = i + 10;
i = -i;
*p-- = '0' + i % 10;
// Undo what we did a moment ago
i = i / 10 + 1;
do {
*p-- = '0' + i % 10;
i /= 10;
} while (i > 0);
*p = '-';
return p;
}
}
}
char *FastHexToBuffer(int i, char* buffer) {
GOOGLE_CHECK(i >= 0)
<< "FastHexToBuffer() wants non-negative integers, not " << i;
static const char *hexdigits = "0123456789abcdef";
char *p = buffer + 21;
*p-- = '\0';
do {
*p-- = hexdigits[i & 15]; // mod by 16
i >>= 4; // divide by 16
} while (i > 0);
return p + 1;
}
char *InternalFastHexToBuffer(uint64_t value, char* buffer, int num_byte) {
static const char *hexdigits = "0123456789abcdef";
buffer[num_byte] = '\0';
for (int i = num_byte - 1; i >= 0; i--) {
#ifdef _M_X64
// MSVC x64 platform has a bug optimizing the uint32_t(value) in the #else
// block. Given that the uint32_t cast was to improve performance on 32-bit
// platforms, we use 64-bit '&' directly.
buffer[i] = hexdigits[value & 0xf];
#else
buffer[i] = hexdigits[uint32_t(value) & 0xf];
#endif // _M_X64
value >>= 4;
}
return buffer;
}
char *FastHex64ToBuffer(uint64_t value, char* buffer) {
return InternalFastHexToBuffer(value, buffer, 16);
}
char *FastHex32ToBuffer(uint32_t value, char* buffer) {
return InternalFastHexToBuffer(value, buffer, 8);
}
// ----------------------------------------------------------------------
// FastInt32ToBufferLeft()
// FastUInt32ToBufferLeft()
// FastInt64ToBufferLeft()
// FastUInt64ToBufferLeft()
//
// Like the Fast*ToBuffer() functions above, these are intended for speed.
// Unlike the Fast*ToBuffer() functions, however, these functions write
// their output to the beginning of the buffer (hence the name, as the
// output is left-aligned). The caller is responsible for ensuring that
// the buffer has enough space to hold the output.
//
// Returns a pointer to the end of the string (i.e. the null character
// terminating the string).
// ----------------------------------------------------------------------
static const char two_ASCII_digits[100][2] = {
{'0', '0'}, {'0', '1'}, {'0', '2'}, {'0', '3'}, {'0', '4'},
{'0', '5'}, {'0', '6'}, {'0', '7'}, {'0', '8'}, {'0', '9'},
{'1', '0'}, {'1', '1'}, {'1', '2'}, {'1', '3'}, {'1', '4'},
{'1', '5'}, {'1', '6'}, {'1', '7'}, {'1', '8'}, {'1', '9'},
{'2', '0'}, {'2', '1'}, {'2', '2'}, {'2', '3'}, {'2', '4'},
{'2', '5'}, {'2', '6'}, {'2', '7'}, {'2', '8'}, {'2', '9'},
{'3', '0'}, {'3', '1'}, {'3', '2'}, {'3', '3'}, {'3', '4'},
{'3', '5'}, {'3', '6'}, {'3', '7'}, {'3', '8'}, {'3', '9'},
{'4', '0'}, {'4', '1'}, {'4', '2'}, {'4', '3'}, {'4', '4'},
{'4', '5'}, {'4', '6'}, {'4', '7'}, {'4', '8'}, {'4', '9'},
{'5', '0'}, {'5', '1'}, {'5', '2'}, {'5', '3'}, {'5', '4'},
{'5', '5'}, {'5', '6'}, {'5', '7'}, {'5', '8'}, {'5', '9'},
{'6', '0'}, {'6', '1'}, {'6', '2'}, {'6', '3'}, {'6', '4'},
{'6', '5'}, {'6', '6'}, {'6', '7'}, {'6', '8'}, {'6', '9'},
{'7', '0'}, {'7', '1'}, {'7', '2'}, {'7', '3'}, {'7', '4'},
{'7', '5'}, {'7', '6'}, {'7', '7'}, {'7', '8'}, {'7', '9'},
{'8', '0'}, {'8', '1'}, {'8', '2'}, {'8', '3'}, {'8', '4'},
{'8', '5'}, {'8', '6'}, {'8', '7'}, {'8', '8'}, {'8', '9'},
{'9', '0'}, {'9', '1'}, {'9', '2'}, {'9', '3'}, {'9', '4'},
{'9', '5'}, {'9', '6'}, {'9', '7'}, {'9', '8'}, {'9', '9'}
};
char* FastUInt32ToBufferLeft(uint32_t u, char* buffer) {
int digits;
const char *ASCII_digits = NULL;
// The idea of this implementation is to trim the number of divides to as few
// as possible by using multiplication and subtraction rather than mod (%),
// and by outputting two digits at a time rather than one.
// The huge-number case is first, in the hopes that the compiler will output
// that case in one branch-free block of code, and only output conditional
// branches into it from below.
if (u >= 1000000000) { // >= 1,000,000,000
digits = u / 100000000; // 100,000,000
ASCII_digits = two_ASCII_digits[digits];
buffer[0] = ASCII_digits[0];
buffer[1] = ASCII_digits[1];
buffer += 2;
sublt100_000_000:
u -= digits * 100000000; // 100,000,000
lt100_000_000:
digits = u / 1000000; // 1,000,000
ASCII_digits = two_ASCII_digits[digits];
buffer[0] = ASCII_digits[0];
buffer[1] = ASCII_digits[1];
buffer += 2;
sublt1_000_000:
u -= digits * 1000000; // 1,000,000
lt1_000_000:
digits = u / 10000; // 10,000
ASCII_digits = two_ASCII_digits[digits];
buffer[0] = ASCII_digits[0];
buffer[1] = ASCII_digits[1];
buffer += 2;
sublt10_000:
u -= digits * 10000; // 10,000
lt10_000:
digits = u / 100;
ASCII_digits = two_ASCII_digits[digits];
buffer[0] = ASCII_digits[0];
buffer[1] = ASCII_digits[1];
buffer += 2;
sublt100:
u -= digits * 100;
lt100:
digits = u;
ASCII_digits = two_ASCII_digits[digits];
buffer[0] = ASCII_digits[0];
buffer[1] = ASCII_digits[1];
buffer += 2;
done:
*buffer = 0;
return buffer;
}
if (u < 100) {
digits = u;
if (u >= 10) goto lt100;
*buffer++ = '0' + digits;
goto done;
}
if (u < 10000) { // 10,000
if (u >= 1000) goto lt10_000;
digits = u / 100;
*buffer++ = '0' + digits;
goto sublt100;
}
if (u < 1000000) { // 1,000,000
if (u >= 100000) goto lt1_000_000;
digits = u / 10000; // 10,000
*buffer++ = '0' + digits;
goto sublt10_000;
}
if (u < 100000000) { // 100,000,000
if (u >= 10000000) goto lt100_000_000;
digits = u / 1000000; // 1,000,000
*buffer++ = '0' + digits;
goto sublt1_000_000;
}
// we already know that u < 1,000,000,000
digits = u / 100000000; // 100,000,000
*buffer++ = '0' + digits;
goto sublt100_000_000;
}
char* FastInt32ToBufferLeft(int32_t i, char* buffer) {
uint32_t u = i;
if (i < 0) {
*buffer++ = '-';
u = -i;
}
return FastUInt32ToBufferLeft(u, buffer);
}
char* FastUInt64ToBufferLeft(uint64_t u64, char* buffer) {
int digits;
const char *ASCII_digits = NULL;
uint32_t u = static_cast<uint32_t>(u64);
if (u == u64) return FastUInt32ToBufferLeft(u, buffer);
uint64_t top_11_digits = u64 / 1000000000;
buffer = FastUInt64ToBufferLeft(top_11_digits, buffer);
u = u64 - (top_11_digits * 1000000000);
digits = u / 10000000; // 10,000,000
GOOGLE_DCHECK_LT(digits, 100);
ASCII_digits = two_ASCII_digits[digits];
buffer[0] = ASCII_digits[0];
buffer[1] = ASCII_digits[1];
buffer += 2;
u -= digits * 10000000; // 10,000,000
digits = u / 100000; // 100,000
ASCII_digits = two_ASCII_digits[digits];
buffer[0] = ASCII_digits[0];
buffer[1] = ASCII_digits[1];
buffer += 2;
u -= digits * 100000; // 100,000
digits = u / 1000; // 1,000
ASCII_digits = two_ASCII_digits[digits];
buffer[0] = ASCII_digits[0];
buffer[1] = ASCII_digits[1];
buffer += 2;
u -= digits * 1000; // 1,000
digits = u / 10;
ASCII_digits = two_ASCII_digits[digits];
buffer[0] = ASCII_digits[0];
buffer[1] = ASCII_digits[1];
buffer += 2;
u -= digits * 10;
digits = u;
*buffer++ = '0' + digits;
*buffer = 0;
return buffer;
}
char* FastInt64ToBufferLeft(int64_t i, char* buffer) {
uint64_t u = i;
if (i < 0) {
*buffer++ = '-';
u = -i;
}
return FastUInt64ToBufferLeft(u, buffer);
}
// ----------------------------------------------------------------------
// SimpleItoa()
// Description: converts an integer to a string.
//
// Return value: string
// ----------------------------------------------------------------------
string SimpleItoa(int i) {
char buffer[kFastToBufferSize];
return (sizeof(i) == 4) ?
FastInt32ToBuffer(i, buffer) :
FastInt64ToBuffer(i, buffer);
}
string SimpleItoa(unsigned int i) {
char buffer[kFastToBufferSize];
return string(buffer, (sizeof(i) == 4) ?
FastUInt32ToBufferLeft(i, buffer) :
FastUInt64ToBufferLeft(i, buffer));
}
string SimpleItoa(long i) { // NOLINT(runtime/int)
char buffer[kFastToBufferSize];
return (sizeof(i) == 4) ?
FastInt32ToBuffer(i, buffer) :
FastInt64ToBuffer(i, buffer);
}
string SimpleItoa(unsigned long i) { // NOLINT(runtime/int)
char buffer[kFastToBufferSize];
return string(buffer, (sizeof(i) == 4) ?
FastUInt32ToBufferLeft(i, buffer) :
FastUInt64ToBufferLeft(i, buffer));
}
string SimpleItoa(long long i) { // NOLINT(runtime/int)
char buffer[kFastToBufferSize];
return (sizeof(i) == 4) ?
FastInt32ToBuffer(i, buffer) :
FastInt64ToBuffer(i, buffer);
}
string SimpleItoa(unsigned long long i) { // NOLINT(runtime/int)
char buffer[kFastToBufferSize];
return string(buffer, (sizeof(i) == 4) ?
FastUInt32ToBufferLeft(i, buffer) :
FastUInt64ToBufferLeft(i, buffer));
}
} // namespace ola
|