diff options
author | Andrey Khalyavin <halyavin@gmail.com> | 2022-02-10 16:46:29 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:46:29 +0300 |
commit | f773626848a7c7456803654292e716b83d69cc12 (patch) | |
tree | db052dfcf9134f492bdbb962cb6c16cea58e1ed3 /contrib/libs/re2/util/strutil.cc | |
parent | f43ab775d197d300eb67bd4497632b909cd7c2a5 (diff) | |
download | ydb-f773626848a7c7456803654292e716b83d69cc12.tar.gz |
Restoring authorship annotation for Andrey Khalyavin <halyavin@gmail.com>. Commit 1 of 2.
Diffstat (limited to 'contrib/libs/re2/util/strutil.cc')
-rw-r--r-- | contrib/libs/re2/util/strutil.cc | 150 |
1 files changed, 75 insertions, 75 deletions
diff --git a/contrib/libs/re2/util/strutil.cc b/contrib/libs/re2/util/strutil.cc index fb7e6b1b0c..475216a7e6 100644 --- a/contrib/libs/re2/util/strutil.cc +++ b/contrib/libs/re2/util/strutil.cc @@ -2,16 +2,16 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -#include <stdarg.h> -#include <stdio.h> - -#include "util/strutil.h" - -#ifdef _WIN32 -#define snprintf _snprintf -#define vsnprintf _vsnprintf -#endif - +#include <stdarg.h> +#include <stdio.h> + +#include "util/strutil.h" + +#ifdef _WIN32 +#define snprintf _snprintf +#define vsnprintf _vsnprintf +#endif + namespace re2 { // ---------------------------------------------------------------------- @@ -19,16 +19,16 @@ namespace re2 { // Copies 'src' to 'dest', escaping dangerous characters using // C-style escape sequences. 'src' and 'dest' should not overlap. // Returns the number of bytes written to 'dest' (not including the \0) -// or (size_t)-1 if there was insufficient space. +// or (size_t)-1 if there was insufficient space. // ---------------------------------------------------------------------- -static size_t CEscapeString(const char* src, size_t src_len, - char* dest, size_t dest_len) { +static size_t CEscapeString(const char* src, size_t src_len, + char* dest, size_t dest_len) { const char* src_end = src + src_len; - size_t used = 0; + size_t used = 0; for (; src < src_end; src++) { - if (dest_len - used < 2) // space for two-character escape - return (size_t)-1; + if (dest_len - used < 2) // space for two-character escape + return (size_t)-1; unsigned char c = *src; switch (c) { @@ -43,9 +43,9 @@ static size_t CEscapeString(const char* src, size_t src_len, // digit then that digit must be escaped too to prevent it being // interpreted as part of the character code by C. if (c < ' ' || c > '~') { - if (dest_len - used < 5) // space for four-character escape + \0 - return (size_t)-1; - snprintf(dest + used, 5, "\\%03o", c); + if (dest_len - used < 5) // space for four-character escape + \0 + return (size_t)-1; + snprintf(dest + used, 5, "\\%03o", c); used += 4; } else { dest[used++] = c; break; @@ -54,7 +54,7 @@ static size_t CEscapeString(const char* src, size_t src_len, } if (dest_len - used < 1) // make sure that there is room for \0 - return (size_t)-1; + return (size_t)-1; dest[used] = '\0'; // doesn't count towards return value though return used; @@ -66,10 +66,10 @@ static size_t CEscapeString(const char* src, size_t src_len, // C-style escape sequences. 'src' and 'dest' should not overlap. // ---------------------------------------------------------------------- std::string CEscape(const StringPiece& src) { - const size_t dest_len = src.size() * 4 + 1; // Maximum possible expansion - char* dest = new char[dest_len]; - const size_t used = CEscapeString(src.data(), src.size(), - dest, dest_len); + const size_t dest_len = src.size() * 4 + 1; // Maximum possible expansion + char* dest = new char[dest_len]; + const size_t used = CEscapeString(src.data(), src.size(), + dest, dest_len); std::string s = std::string(dest, used); delete[] dest; return s; @@ -93,57 +93,57 @@ void PrefixSuccessor(std::string* prefix) { } static void StringAppendV(std::string* dst, const char* format, va_list ap) { - // First try with a small fixed size buffer - char space[1024]; - - // It's possible for methods that use a va_list to invalidate - // the data in it upon use. The fix is to make a copy - // of the structure before using it and use that copy instead. - va_list backup_ap; - va_copy(backup_ap, ap); - int result = vsnprintf(space, sizeof(space), format, backup_ap); - va_end(backup_ap); - - if ((result >= 0) && (static_cast<size_t>(result) < sizeof(space))) { - // It fit - dst->append(space, result); - return; - } - - // Repeatedly increase buffer size until it fits - int length = sizeof(space); - while (true) { - if (result < 0) { - // Older behavior: just try doubling the buffer size - length *= 2; - } else { - // We need exactly "result+1" characters - length = result+1; - } - char* buf = new char[length]; - - // Restore the va_list before we use it again - va_copy(backup_ap, ap); - result = vsnprintf(buf, length, format, backup_ap); - va_end(backup_ap); - - if ((result >= 0) && (result < length)) { - // It fit - dst->append(buf, result); - delete[] buf; - return; - } - delete[] buf; - } -} - + // First try with a small fixed size buffer + char space[1024]; + + // It's possible for methods that use a va_list to invalidate + // the data in it upon use. The fix is to make a copy + // of the structure before using it and use that copy instead. + va_list backup_ap; + va_copy(backup_ap, ap); + int result = vsnprintf(space, sizeof(space), format, backup_ap); + va_end(backup_ap); + + if ((result >= 0) && (static_cast<size_t>(result) < sizeof(space))) { + // It fit + dst->append(space, result); + return; + } + + // Repeatedly increase buffer size until it fits + int length = sizeof(space); + while (true) { + if (result < 0) { + // Older behavior: just try doubling the buffer size + length *= 2; + } else { + // We need exactly "result+1" characters + length = result+1; + } + char* buf = new char[length]; + + // Restore the va_list before we use it again + va_copy(backup_ap, ap); + result = vsnprintf(buf, length, format, backup_ap); + va_end(backup_ap); + + if ((result >= 0) && (result < length)) { + // It fit + dst->append(buf, result); + delete[] buf; + return; + } + delete[] buf; + } +} + std::string StringPrintf(const char* format, ...) { - va_list ap; - va_start(ap, format); + va_list ap; + va_start(ap, format); std::string result; - StringAppendV(&result, format, ap); - va_end(ap); - return result; -} - + StringAppendV(&result, format, ap); + va_end(ap); + return result; +} + } // namespace re2 |