LCOV - code coverage report
Current view: top level - third_party/protobuf/src/google/protobuf/stubs - strutil.h (source / functions) Hit Total Coverage
Test: tmp.zDYK9MVh93 Lines: 48 66 72.7 %
Date: 2015-10-10 Functions: 9 15 60.0 %

          Line data    Source code
       1             : // Protocol Buffers - Google's data interchange format
       2             : // Copyright 2008 Google Inc.  All rights reserved.
       3             : // https://developers.google.com/protocol-buffers/
       4             : //
       5             : // Redistribution and use in source and binary forms, with or without
       6             : // modification, are permitted provided that the following conditions are
       7             : // met:
       8             : //
       9             : //     * Redistributions of source code must retain the above copyright
      10             : // notice, this list of conditions and the following disclaimer.
      11             : //     * Redistributions in binary form must reproduce the above
      12             : // copyright notice, this list of conditions and the following disclaimer
      13             : // in the documentation and/or other materials provided with the
      14             : // distribution.
      15             : //     * Neither the name of Google Inc. nor the names of its
      16             : // contributors may be used to endorse or promote products derived from
      17             : // this software without specific prior written permission.
      18             : //
      19             : // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
      20             : // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
      21             : // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
      22             : // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
      23             : // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
      24             : // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
      25             : // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
      26             : // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
      27             : // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
      28             : // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
      29             : // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
      30             : 
      31             : // from google3/strings/strutil.h
      32             : 
      33             : #ifndef GOOGLE_PROTOBUF_STUBS_STRUTIL_H__
      34             : #define GOOGLE_PROTOBUF_STUBS_STRUTIL_H__
      35             : 
      36             : #include <stdlib.h>
      37             : #include <vector>
      38             : #include <google/protobuf/stubs/common.h>
      39             : #include <google/protobuf/stubs/stringpiece.h>
      40             : 
      41             : namespace google {
      42             : namespace protobuf {
      43             : 
      44             : #ifdef _MSC_VER
      45             : #define strtoll  _strtoi64
      46             : #define strtoull _strtoui64
      47             : #elif defined(__DECCXX) && defined(__osf__)
      48             : // HP C++ on Tru64 does not have strtoll, but strtol is already 64-bit.
      49             : #define strtoll strtol
      50             : #define strtoull strtoul
      51             : #endif
      52             : 
      53             : // ----------------------------------------------------------------------
      54             : // ascii_isalnum()
      55             : //    Check if an ASCII character is alphanumeric.  We can't use ctype's
      56             : //    isalnum() because it is affected by locale.  This function is applied
      57             : //    to identifiers in the protocol buffer language, not to natural-language
      58             : //    strings, so locale should not be taken into account.
      59             : // ascii_isdigit()
      60             : //    Like above, but only accepts digits.
      61             : // ascii_isspace()
      62             : //    Check if the character is a space character.
      63             : // ----------------------------------------------------------------------
      64             : 
      65             : inline bool ascii_isalnum(char c) {
      66      164522 :   return ('a' <= c && c <= 'z') ||
      67      174623 :          ('A' <= c && c <= 'Z') ||
      68       10101 :          ('0' <= c && c <= '9');
      69             : }
      70             : 
      71             : inline bool ascii_isdigit(char c) {
      72      128408 :   return ('0' <= c && c <= '9');
      73             : }
      74             : 
      75             : inline bool ascii_isspace(char c) {
      76           0 :   return c == ' ' || c == '\t' || c == '\n' || c == '\v' || c == '\f' ||
      77             :       c == '\r';
      78             : }
      79             : 
      80             : inline bool ascii_isupper(char c) {
      81           0 :   return c >= 'A' && c <= 'Z';
      82             : }
      83             : 
      84             : inline bool ascii_islower(char c) {
      85           0 :   return c >= 'a' && c <= 'z';
      86             : }
      87             : 
      88             : inline char ascii_toupper(char c) {
      89           0 :   return ascii_islower(c) ? c - ('a' - 'A') : c;
      90             : }
      91             : 
      92             : inline char ascii_tolower(char c) {
      93           0 :   return ascii_isupper(c) ? c + ('a' - 'A') : c;
      94             : }
      95             : 
      96             : inline int hex_digit_to_int(char c) {
      97             :   /* Assume ASCII. */
      98           0 :   int x = static_cast<unsigned char>(c);
      99           0 :   if (x > '9') {
     100           0 :     x += 9;
     101             :   }
     102           0 :   return x & 0xf;
     103             : }
     104             : 
     105             : // ----------------------------------------------------------------------
     106             : // HasPrefixString()
     107             : //    Check if a string begins with a given prefix.
     108             : // StripPrefixString()
     109             : //    Given a string and a putative prefix, returns the string minus the
     110             : //    prefix string if the prefix matches, otherwise the original
     111             : //    string.
     112             : // ----------------------------------------------------------------------
     113        2342 : inline bool HasPrefixString(const string& str,
     114             :                             const string& prefix) {
     115        6269 :   return str.size() >= prefix.size() &&
     116        3927 :          str.compare(0, prefix.size(), prefix) == 0;
     117             : }
     118             : 
     119           0 : inline string StripPrefixString(const string& str, const string& prefix) {
     120           0 :   if (HasPrefixString(str, prefix)) {
     121           0 :     return str.substr(prefix.size());
     122             :   } else {
     123           0 :     return str;
     124             :   }
     125             : }
     126             : 
     127             : // ----------------------------------------------------------------------
     128             : // HasSuffixString()
     129             : //    Return true if str ends in suffix.
     130             : // StripSuffixString()
     131             : //    Given a string and a putative suffix, returns the string minus the
     132             : //    suffix string if the suffix matches, otherwise the original
     133             : //    string.
     134             : // ----------------------------------------------------------------------
     135         578 : inline bool HasSuffixString(const string& str,
     136             :                             const string& suffix) {
     137        1734 :   return str.size() >= suffix.size() &&
     138        1156 :          str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
     139             : }
     140             : 
     141         149 : inline string StripSuffixString(const string& str, const string& suffix) {
     142         149 :   if (HasSuffixString(str, suffix)) {
     143         298 :     return str.substr(0, str.size() - suffix.size());
     144             :   } else {
     145           0 :     return str;
     146             :   }
     147             : }
     148             : 
     149             : // ----------------------------------------------------------------------
     150             : // StripString
     151             : //    Replaces any occurrence of the character 'remove' (or the characters
     152             : //    in 'remove') with the character 'replacewith'.
     153             : //    Good for keeping html characters or protocol characters (\t) out
     154             : //    of places where they might cause a problem.
     155             : // StripWhitespace
     156             : //    Removes whitespaces from both ends of the given string.
     157             : // ----------------------------------------------------------------------
     158             : LIBPROTOBUF_EXPORT void StripString(string* s, const char* remove,
     159             :                                     char replacewith);
     160             : 
     161             : LIBPROTOBUF_EXPORT void StripWhitespace(string* s);
     162             : 
     163             : 
     164             : // ----------------------------------------------------------------------
     165             : // LowerString()
     166             : // UpperString()
     167             : // ToUpper()
     168             : //    Convert the characters in "s" to lowercase or uppercase.  ASCII-only:
     169             : //    these functions intentionally ignore locale because they are applied to
     170             : //    identifiers used in the Protocol Buffer language, not to natural-language
     171             : //    strings.
     172             : // ----------------------------------------------------------------------
     173             : 
     174       52042 : inline void LowerString(string * s) {
     175       52042 :   string::iterator end = s->end();
     176     2718842 :   for (string::iterator i = s->begin(); i != end; ++i) {
     177             :     // tolower() changes based on locale.  We don't want this!
     178     2614758 :     if ('A' <= *i && *i <= 'Z') *i += 'a' - 'A';
     179             :   }
     180       52042 : }
     181             : 
     182         546 : inline void UpperString(string * s) {
     183         546 :   string::iterator end = s->end();
     184        4292 :   for (string::iterator i = s->begin(); i != end; ++i) {
     185             :     // toupper() changes based on locale.  We don't want this!
     186        3200 :     if ('a' <= *i && *i <= 'z') *i += 'A' - 'a';
     187             :   }
     188         546 : }
     189             : 
     190         546 : inline string ToUpper(const string& s) {
     191         546 :   string out = s;
     192         546 :   UpperString(&out);
     193         546 :   return out;
     194             : }
     195             : 
     196             : // ----------------------------------------------------------------------
     197             : // StringReplace()
     198             : //    Give me a string and two patterns "old" and "new", and I replace
     199             : //    the first instance of "old" in the string with "new", if it
     200             : //    exists.  RETURN a new string, regardless of whether the replacement
     201             : //    happened or not.
     202             : // ----------------------------------------------------------------------
     203             : 
     204             : LIBPROTOBUF_EXPORT string StringReplace(const string& s, const string& oldsub,
     205             :                                         const string& newsub, bool replace_all);
     206             : 
     207             : // ----------------------------------------------------------------------
     208             : // SplitStringUsing()
     209             : //    Split a string using a character delimiter. Append the components
     210             : //    to 'result'.  If there are consecutive delimiters, this function skips
     211             : //    over all of them.
     212             : // ----------------------------------------------------------------------
     213             : LIBPROTOBUF_EXPORT void SplitStringUsing(const string& full, const char* delim,
     214             :                                          vector<string>* res);
     215             : 
     216             : // Split a string using one or more byte delimiters, presented
     217             : // as a nul-terminated c string. Append the components to 'result'.
     218             : // If there are consecutive delimiters, this function will return
     219             : // corresponding empty strings.  If you want to drop the empty
     220             : // strings, try SplitStringUsing().
     221             : //
     222             : // If "full" is the empty string, yields an empty string as the only value.
     223             : // ----------------------------------------------------------------------
     224             : LIBPROTOBUF_EXPORT void SplitStringAllowEmpty(const string& full,
     225             :                                               const char* delim,
     226             :                                               vector<string>* result);
     227             : 
     228             : // ----------------------------------------------------------------------
     229             : // Split()
     230             : //    Split a string using a character delimiter.
     231             : // ----------------------------------------------------------------------
     232         317 : inline vector<string> Split(
     233             :     const string& full, const char* delim, bool skip_empty = true) {
     234             :   vector<string> result;
     235         317 :   if (skip_empty) {
     236         317 :     SplitStringUsing(full, delim, &result);
     237             :   } else {
     238           0 :     SplitStringAllowEmpty(full, delim, &result);
     239             :   }
     240         317 :   return result;
     241             : }
     242             : 
     243             : // ----------------------------------------------------------------------
     244             : // JoinStrings()
     245             : //    These methods concatenate a vector of strings into a C++ string, using
     246             : //    the C-string "delim" as a separator between components. There are two
     247             : //    flavors of the function, one flavor returns the concatenated string,
     248             : //    another takes a pointer to the target string. In the latter case the
     249             : //    target string is cleared and overwritten.
     250             : // ----------------------------------------------------------------------
     251             : LIBPROTOBUF_EXPORT void JoinStrings(const vector<string>& components,
     252             :                                     const char* delim, string* result);
     253             : 
     254           0 : inline string JoinStrings(const vector<string>& components,
     255             :                           const char* delim) {
     256             :   string result;
     257           0 :   JoinStrings(components, delim, &result);
     258           0 :   return result;
     259             : }
     260             : 
     261             : // ----------------------------------------------------------------------
     262             : // UnescapeCEscapeSequences()
     263             : //    Copies "source" to "dest", rewriting C-style escape sequences
     264             : //    -- '\n', '\r', '\\', '\ooo', etc -- to their ASCII
     265             : //    equivalents.  "dest" must be sufficiently large to hold all
     266             : //    the characters in the rewritten string (i.e. at least as large
     267             : //    as strlen(source) + 1 should be safe, since the replacements
     268             : //    are always shorter than the original escaped sequences).  It's
     269             : //    safe for source and dest to be the same.  RETURNS the length
     270             : //    of dest.
     271             : //
     272             : //    It allows hex sequences \xhh, or generally \xhhhhh with an
     273             : //    arbitrary number of hex digits, but all of them together must
     274             : //    specify a value of a single byte (e.g. \x0045 is equivalent
     275             : //    to \x45, and \x1234 is erroneous).
     276             : //
     277             : //    It also allows escape sequences of the form \uhhhh (exactly four
     278             : //    hex digits, upper or lower case) or \Uhhhhhhhh (exactly eight
     279             : //    hex digits, upper or lower case) to specify a Unicode code
     280             : //    point. The dest array will contain the UTF8-encoded version of
     281             : //    that code-point (e.g., if source contains \u2019, then dest will
     282             : //    contain the three bytes 0xE2, 0x80, and 0x99).
     283             : //
     284             : //    Errors: In the first form of the call, errors are reported with
     285             : //    LOG(ERROR). The same is true for the second form of the call if
     286             : //    the pointer to the string vector is NULL; otherwise, error
     287             : //    messages are stored in the vector. In either case, the effect on
     288             : //    the dest array is not defined, but rest of the source will be
     289             : //    processed.
     290             : //    ----------------------------------------------------------------------
     291             : 
     292             : LIBPROTOBUF_EXPORT int UnescapeCEscapeSequences(const char* source, char* dest);
     293             : LIBPROTOBUF_EXPORT int UnescapeCEscapeSequences(const char* source, char* dest,
     294             :                                                 vector<string> *errors);
     295             : 
     296             : // ----------------------------------------------------------------------
     297             : // UnescapeCEscapeString()
     298             : //    This does the same thing as UnescapeCEscapeSequences, but creates
     299             : //    a new string. The caller does not need to worry about allocating
     300             : //    a dest buffer. This should be used for non performance critical
     301             : //    tasks such as printing debug messages. It is safe for src and dest
     302             : //    to be the same.
     303             : //
     304             : //    The second call stores its errors in a supplied string vector.
     305             : //    If the string vector pointer is NULL, it reports the errors with LOG().
     306             : //
     307             : //    In the first and second calls, the length of dest is returned. In the
     308             : //    the third call, the new string is returned.
     309             : // ----------------------------------------------------------------------
     310             : 
     311             : LIBPROTOBUF_EXPORT int UnescapeCEscapeString(const string& src, string* dest);
     312             : LIBPROTOBUF_EXPORT int UnescapeCEscapeString(const string& src, string* dest,
     313             :                                              vector<string> *errors);
     314             : LIBPROTOBUF_EXPORT string UnescapeCEscapeString(const string& src);
     315             : 
     316             : // ----------------------------------------------------------------------
     317             : // CEscapeString()
     318             : //    Copies 'src' to 'dest', escaping dangerous characters using
     319             : //    C-style escape sequences. This is very useful for preparing query
     320             : //    flags. 'src' and 'dest' should not overlap.
     321             : //    Returns the number of bytes written to 'dest' (not including the \0)
     322             : //    or -1 if there was insufficient space.
     323             : //
     324             : //    Currently only \n, \r, \t, ", ', \ and !isprint() chars are escaped.
     325             : // ----------------------------------------------------------------------
     326             : LIBPROTOBUF_EXPORT int CEscapeString(const char* src, int src_len,
     327             :                                      char* dest, int dest_len);
     328             : 
     329             : // ----------------------------------------------------------------------
     330             : // CEscape()
     331             : //    More convenient form of CEscapeString: returns result as a "string".
     332             : //    This version is slower than CEscapeString() because it does more
     333             : //    allocation.  However, it is much more convenient to use in
     334             : //    non-speed-critical code like logging messages etc.
     335             : // ----------------------------------------------------------------------
     336             : LIBPROTOBUF_EXPORT string CEscape(const string& src);
     337             : 
     338             : namespace strings {
     339             : // Like CEscape() but does not escape bytes with the upper bit set.
     340             : LIBPROTOBUF_EXPORT string Utf8SafeCEscape(const string& src);
     341             : 
     342             : // Like CEscape() but uses hex (\x) escapes instead of octals.
     343             : LIBPROTOBUF_EXPORT string CHexEscape(const string& src);
     344             : }  // namespace strings
     345             : 
     346             : // ----------------------------------------------------------------------
     347             : // strto32()
     348             : // strtou32()
     349             : // strto64()
     350             : // strtou64()
     351             : //    Architecture-neutral plug compatible replacements for strtol() and
     352             : //    strtoul().  Long's have different lengths on ILP-32 and LP-64
     353             : //    platforms, so using these is safer, from the point of view of
     354             : //    overflow behavior, than using the standard libc functions.
     355             : // ----------------------------------------------------------------------
     356             : LIBPROTOBUF_EXPORT int32 strto32_adaptor(const char *nptr, char **endptr,
     357             :                                          int base);
     358             : LIBPROTOBUF_EXPORT uint32 strtou32_adaptor(const char *nptr, char **endptr,
     359             :                                            int base);
     360             : 
     361             : inline int32 strto32(const char *nptr, char **endptr, int base) {
     362             :   if (sizeof(int32) == sizeof(long))
     363             :     return strtol(nptr, endptr, base);
     364             :   else
     365             :     return strto32_adaptor(nptr, endptr, base);
     366             : }
     367             : 
     368             : inline uint32 strtou32(const char *nptr, char **endptr, int base) {
     369             :   if (sizeof(uint32) == sizeof(unsigned long))
     370             :     return strtoul(nptr, endptr, base);
     371             :   else
     372             :     return strtou32_adaptor(nptr, endptr, base);
     373             : }
     374             : 
     375             : // For now, long long is 64-bit on all the platforms we care about, so these
     376             : // functions can simply pass the call to strto[u]ll.
     377             : inline int64 strto64(const char *nptr, char **endptr, int base) {
     378             :   GOOGLE_COMPILE_ASSERT(sizeof(int64) == sizeof(long long),
     379             :                         sizeof_int64_is_not_sizeof_long_long);
     380          17 :   return strtoll(nptr, endptr, base);
     381             : }
     382             : 
     383             : inline uint64 strtou64(const char *nptr, char **endptr, int base) {
     384             :   GOOGLE_COMPILE_ASSERT(sizeof(uint64) == sizeof(unsigned long long),
     385             :                         sizeof_uint64_is_not_sizeof_long_long);
     386          11 :   return strtoull(nptr, endptr, base);
     387             : }
     388             : 
     389             : // ----------------------------------------------------------------------
     390             : // safe_strtob()
     391             : // safe_strto32()
     392             : // safe_strtou32()
     393             : // safe_strto64()
     394             : // safe_strtou64()
     395             : // safe_strtof()
     396             : // safe_strtod()
     397             : // ----------------------------------------------------------------------
     398             : LIBPROTOBUF_EXPORT bool safe_strtob(StringPiece str, bool* value);
     399             : 
     400             : LIBPROTOBUF_EXPORT bool safe_strto32(const string& str, int32* value);
     401             : LIBPROTOBUF_EXPORT bool safe_strtou32(const string& str, uint32* value);
     402             : inline bool safe_strto32(const char* str, int32* value) {
     403             :   return safe_strto32(string(str), value);
     404             : }
     405             : inline bool safe_strto32(StringPiece str, int32* value) {
     406             :   return safe_strto32(str.ToString(), value);
     407             : }
     408             : inline bool safe_strtou32(const char* str, uint32* value) {
     409             :   return safe_strtou32(string(str), value);
     410             : }
     411             : inline bool safe_strtou32(StringPiece str, uint32* value) {
     412             :   return safe_strtou32(str.ToString(), value);
     413             : }
     414             : 
     415             : LIBPROTOBUF_EXPORT bool safe_strto64(const string& str, int64* value);
     416             : LIBPROTOBUF_EXPORT bool safe_strtou64(const string& str, uint64* value);
     417             : inline bool safe_strto64(const char* str, int64* value) {
     418             :   return safe_strto64(string(str), value);
     419             : }
     420             : inline bool safe_strto64(StringPiece str, int64* value) {
     421             :   return safe_strto64(str.ToString(), value);
     422             : }
     423             : inline bool safe_strtou64(const char* str, uint64* value) {
     424             :   return safe_strtou64(string(str), value);
     425             : }
     426             : inline bool safe_strtou64(StringPiece str, uint64* value) {
     427             :   return safe_strtou64(str.ToString(), value);
     428             : }
     429             : 
     430             : LIBPROTOBUF_EXPORT bool safe_strtof(const char* str, float* value);
     431             : LIBPROTOBUF_EXPORT bool safe_strtod(const char* str, double* value);
     432             : inline bool safe_strtof(const string& str, float* value) {
     433             :   return safe_strtof(str.c_str(), value);
     434             : }
     435             : inline bool safe_strtod(const string& str, double* value) {
     436             :   return safe_strtod(str.c_str(), value);
     437             : }
     438             : inline bool safe_strtof(StringPiece str, float* value) {
     439             :   return safe_strtof(str.ToString(), value);
     440             : }
     441             : inline bool safe_strtod(StringPiece str, double* value) {
     442             :   return safe_strtod(str.ToString(), value);
     443             : }
     444             : 
     445             : // ----------------------------------------------------------------------
     446             : // FastIntToBuffer()
     447             : // FastHexToBuffer()
     448             : // FastHex64ToBuffer()
     449             : // FastHex32ToBuffer()
     450             : // FastTimeToBuffer()
     451             : //    These are intended for speed.  FastIntToBuffer() assumes the
     452             : //    integer is non-negative.  FastHexToBuffer() puts output in
     453             : //    hex rather than decimal.  FastTimeToBuffer() puts the output
     454             : //    into RFC822 format.
     455             : //
     456             : //    FastHex64ToBuffer() puts a 64-bit unsigned value in hex-format,
     457             : //    padded to exactly 16 bytes (plus one byte for '\0')
     458             : //
     459             : //    FastHex32ToBuffer() puts a 32-bit unsigned value in hex-format,
     460             : //    padded to exactly 8 bytes (plus one byte for '\0')
     461             : //
     462             : //       All functions take the output buffer as an arg.
     463             : //    They all return a pointer to the beginning of the output,
     464             : //    which may not be the beginning of the input buffer.
     465             : // ----------------------------------------------------------------------
     466             : 
     467             : // Suggested buffer size for FastToBuffer functions.  Also works with
     468             : // DoubleToBuffer() and FloatToBuffer().
     469             : static const int kFastToBufferSize = 32;
     470             : 
     471             : LIBPROTOBUF_EXPORT char* FastInt32ToBuffer(int32 i, char* buffer);
     472             : LIBPROTOBUF_EXPORT char* FastInt64ToBuffer(int64 i, char* buffer);
     473             : char* FastUInt32ToBuffer(uint32 i, char* buffer);  // inline below
     474             : char* FastUInt64ToBuffer(uint64 i, char* buffer);  // inline below
     475             : LIBPROTOBUF_EXPORT char* FastHexToBuffer(int i, char* buffer);
     476             : LIBPROTOBUF_EXPORT char* FastHex64ToBuffer(uint64 i, char* buffer);
     477             : LIBPROTOBUF_EXPORT char* FastHex32ToBuffer(uint32 i, char* buffer);
     478             : 
     479             : // at least 22 bytes long
     480             : inline char* FastIntToBuffer(int i, char* buffer) {
     481             :   return (sizeof(i) == 4 ?
     482             :           FastInt32ToBuffer(i, buffer) : FastInt64ToBuffer(i, buffer));
     483             : }
     484             : inline char* FastUIntToBuffer(unsigned int i, char* buffer) {
     485             :   return (sizeof(i) == 4 ?
     486             :           FastUInt32ToBuffer(i, buffer) : FastUInt64ToBuffer(i, buffer));
     487             : }
     488             : inline char* FastLongToBuffer(long i, char* buffer) {
     489             :   return (sizeof(i) == 4 ?
     490             :           FastInt32ToBuffer(i, buffer) : FastInt64ToBuffer(i, buffer));
     491             : }
     492             : inline char* FastULongToBuffer(unsigned long i, char* buffer) {
     493             :   return (sizeof(i) == 4 ?
     494             :           FastUInt32ToBuffer(i, buffer) : FastUInt64ToBuffer(i, buffer));
     495             : }
     496             : 
     497             : // ----------------------------------------------------------------------
     498             : // FastInt32ToBufferLeft()
     499             : // FastUInt32ToBufferLeft()
     500             : // FastInt64ToBufferLeft()
     501             : // FastUInt64ToBufferLeft()
     502             : //
     503             : // Like the Fast*ToBuffer() functions above, these are intended for speed.
     504             : // Unlike the Fast*ToBuffer() functions, however, these functions write
     505             : // their output to the beginning of the buffer (hence the name, as the
     506             : // output is left-aligned).  The caller is responsible for ensuring that
     507             : // the buffer has enough space to hold the output.
     508             : //
     509             : // Returns a pointer to the end of the string (i.e. the null character
     510             : // terminating the string).
     511             : // ----------------------------------------------------------------------
     512             : 
     513             : LIBPROTOBUF_EXPORT char* FastInt32ToBufferLeft(int32 i, char* buffer);
     514             : LIBPROTOBUF_EXPORT char* FastUInt32ToBufferLeft(uint32 i, char* buffer);
     515             : LIBPROTOBUF_EXPORT char* FastInt64ToBufferLeft(int64 i, char* buffer);
     516             : LIBPROTOBUF_EXPORT char* FastUInt64ToBufferLeft(uint64 i, char* buffer);
     517             : 
     518             : // Just define these in terms of the above.
     519             : inline char* FastUInt32ToBuffer(uint32 i, char* buffer) {
     520             :   FastUInt32ToBufferLeft(i, buffer);
     521             :   return buffer;
     522             : }
     523             : inline char* FastUInt64ToBuffer(uint64 i, char* buffer) {
     524             :   FastUInt64ToBufferLeft(i, buffer);
     525             :   return buffer;
     526             : }
     527             : 
     528             : inline string SimpleBtoa(bool value) {
     529             :   return value ? "true" : "false";
     530             : }
     531             : 
     532             : // ----------------------------------------------------------------------
     533             : // SimpleItoa()
     534             : //    Description: converts an integer to a string.
     535             : //
     536             : //    Return value: string
     537             : // ----------------------------------------------------------------------
     538             : LIBPROTOBUF_EXPORT string SimpleItoa(int i);
     539             : LIBPROTOBUF_EXPORT string SimpleItoa(unsigned int i);
     540             : LIBPROTOBUF_EXPORT string SimpleItoa(long i);
     541             : LIBPROTOBUF_EXPORT string SimpleItoa(unsigned long i);
     542             : LIBPROTOBUF_EXPORT string SimpleItoa(long long i);
     543             : LIBPROTOBUF_EXPORT string SimpleItoa(unsigned long long i);
     544             : 
     545             : // ----------------------------------------------------------------------
     546             : // SimpleDtoa()
     547             : // SimpleFtoa()
     548             : // DoubleToBuffer()
     549             : // FloatToBuffer()
     550             : //    Description: converts a double or float to a string which, if
     551             : //    passed to NoLocaleStrtod(), will produce the exact same original double
     552             : //    (except in case of NaN; all NaNs are considered the same value).
     553             : //    We try to keep the string short but it's not guaranteed to be as
     554             : //    short as possible.
     555             : //
     556             : //    DoubleToBuffer() and FloatToBuffer() write the text to the given
     557             : //    buffer and return it.  The buffer must be at least
     558             : //    kDoubleToBufferSize bytes for doubles and kFloatToBufferSize
     559             : //    bytes for floats.  kFastToBufferSize is also guaranteed to be large
     560             : //    enough to hold either.
     561             : //
     562             : //    Return value: string
     563             : // ----------------------------------------------------------------------
     564             : LIBPROTOBUF_EXPORT string SimpleDtoa(double value);
     565             : LIBPROTOBUF_EXPORT string SimpleFtoa(float value);
     566             : 
     567             : LIBPROTOBUF_EXPORT char* DoubleToBuffer(double i, char* buffer);
     568             : LIBPROTOBUF_EXPORT char* FloatToBuffer(float i, char* buffer);
     569             : 
     570             : // In practice, doubles should never need more than 24 bytes and floats
     571             : // should never need more than 14 (including null terminators), but we
     572             : // overestimate to be safe.
     573             : static const int kDoubleToBufferSize = 32;
     574             : static const int kFloatToBufferSize = 24;
     575             : 
     576             : namespace strings {
     577             : 
     578             : enum PadSpec {
     579             :   NO_PAD = 1,
     580             :   ZERO_PAD_2,
     581             :   ZERO_PAD_3,
     582             :   ZERO_PAD_4,
     583             :   ZERO_PAD_5,
     584             :   ZERO_PAD_6,
     585             :   ZERO_PAD_7,
     586             :   ZERO_PAD_8,
     587             :   ZERO_PAD_9,
     588             :   ZERO_PAD_10,
     589             :   ZERO_PAD_11,
     590             :   ZERO_PAD_12,
     591             :   ZERO_PAD_13,
     592             :   ZERO_PAD_14,
     593             :   ZERO_PAD_15,
     594             :   ZERO_PAD_16,
     595             : };
     596             : 
     597             : struct Hex {
     598             :   uint64 value;
     599             :   enum PadSpec spec;
     600             :   template <class Int>
     601             :   explicit Hex(Int v, PadSpec s = NO_PAD)
     602       12771 :       : spec(s) {
     603             :     // Prevent sign-extension by casting integers to
     604             :     // their unsigned counterparts.
     605             : #ifdef LANG_CXX11
     606             :     static_assert(
     607             :         sizeof(v) == 1 || sizeof(v) == 2 || sizeof(v) == 4 || sizeof(v) == 8,
     608             :         "Unknown integer type");
     609             : #endif
     610       12771 :     value = sizeof(v) == 1 ? static_cast<uint8>(v)
     611             :           : sizeof(v) == 2 ? static_cast<uint16>(v)
     612             :           : sizeof(v) == 4 ? static_cast<uint32>(v)
     613             :           : static_cast<uint64>(v);
     614             :   }
     615             : };
     616             : 
     617             : struct LIBPROTOBUF_EXPORT AlphaNum {
     618             :   const char *piece_data_;  // move these to string_ref eventually
     619             :   size_t piece_size_;       // move these to string_ref eventually
     620             : 
     621             :   char digits[kFastToBufferSize];
     622             : 
     623             :   // No bool ctor -- bools convert to an integral type.
     624             :   // A bool ctor would also convert incoming pointers (bletch).
     625             : 
     626             :   AlphaNum(int32 i32)
     627             :       : piece_data_(digits),
     628           4 :         piece_size_(FastInt32ToBufferLeft(i32, digits) - &digits[0]) {}
     629             :   AlphaNum(uint32 u32)
     630             :       : piece_data_(digits),
     631             :         piece_size_(FastUInt32ToBufferLeft(u32, digits) - &digits[0]) {}
     632             :   AlphaNum(int64 i64)
     633             :       : piece_data_(digits),
     634             :         piece_size_(FastInt64ToBufferLeft(i64, digits) - &digits[0]) {}
     635             :   AlphaNum(uint64 u64)
     636             :       : piece_data_(digits),
     637             :         piece_size_(FastUInt64ToBufferLeft(u64, digits) - &digits[0]) {}
     638             : 
     639             :   AlphaNum(float f)
     640             :     : piece_data_(digits), piece_size_(strlen(FloatToBuffer(f, digits))) {}
     641             :   AlphaNum(double f)
     642             :     : piece_data_(digits), piece_size_(strlen(DoubleToBuffer(f, digits))) {}
     643             : 
     644             :   AlphaNum(Hex hex);
     645             : 
     646             :   AlphaNum(const char* c_str)
     647       30138 :       : piece_data_(c_str), piece_size_(strlen(c_str)) {}
     648             :   // TODO: Add a string_ref constructor, eventually
     649             :   // AlphaNum(const StringPiece &pc) : piece(pc) {}
     650             : 
     651             :   AlphaNum(const string& str)
     652       23032 :       : piece_data_(str.data()), piece_size_(str.size()) {}
     653             : 
     654             :   AlphaNum(StringPiece str)
     655             :       : piece_data_(str.data()), piece_size_(str.size()) {}
     656             : 
     657             :   size_t size() const { return piece_size_; }
     658             :   const char *data() const { return piece_data_; }
     659             : 
     660             :  private:
     661             :   // Use ":" not ':'
     662             :   AlphaNum(char c);  // NOLINT(runtime/explicit)
     663             : 
     664             :   // Disallow copy and assign.
     665             :   AlphaNum(const AlphaNum&);
     666             :   void operator=(const AlphaNum&);
     667             : };
     668             : 
     669             : }  // namespace strings
     670             : 
     671             : using strings::AlphaNum;
     672             : 
     673             : // ----------------------------------------------------------------------
     674             : // StrCat()
     675             : //    This merges the given strings or numbers, with no delimiter.  This
     676             : //    is designed to be the fastest possible way to construct a string out
     677             : //    of a mix of raw C strings, strings, bool values,
     678             : //    and numeric values.
     679             : //
     680             : //    Don't use this for user-visible strings.  The localization process
     681             : //    works poorly on strings built up out of fragments.
     682             : //
     683             : //    For clarity and performance, don't use StrCat when appending to a
     684             : //    string.  In particular, avoid using any of these (anti-)patterns:
     685             : //      str.append(StrCat(...)
     686             : //      str += StrCat(...)
     687             : //      str = StrCat(str, ...)
     688             : //    where the last is the worse, with the potential to change a loop
     689             : //    from a linear time operation with O(1) dynamic allocations into a
     690             : //    quadratic time operation with O(n) dynamic allocations.  StrAppend
     691             : //    is a better choice than any of the above, subject to the restriction
     692             : //    of StrAppend(&str, a, b, c, ...) that none of the a, b, c, ... may
     693             : //    be a reference into str.
     694             : // ----------------------------------------------------------------------
     695             : 
     696             : LIBPROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b);
     697             : LIBPROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b,
     698             :                                  const AlphaNum& c);
     699             : LIBPROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b,
     700             :                                  const AlphaNum& c, const AlphaNum& d);
     701             : LIBPROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b,
     702             :                                  const AlphaNum& c, const AlphaNum& d,
     703             :                                  const AlphaNum& e);
     704             : LIBPROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b,
     705             :                                  const AlphaNum& c, const AlphaNum& d,
     706             :                                  const AlphaNum& e, const AlphaNum& f);
     707             : LIBPROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b,
     708             :                                  const AlphaNum& c, const AlphaNum& d,
     709             :                                  const AlphaNum& e, const AlphaNum& f,
     710             :                                  const AlphaNum& g);
     711             : LIBPROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b,
     712             :                                  const AlphaNum& c, const AlphaNum& d,
     713             :                                  const AlphaNum& e, const AlphaNum& f,
     714             :                                  const AlphaNum& g, const AlphaNum& h);
     715             : LIBPROTOBUF_EXPORT string StrCat(const AlphaNum& a, const AlphaNum& b,
     716             :                                  const AlphaNum& c, const AlphaNum& d,
     717             :                                  const AlphaNum& e, const AlphaNum& f,
     718             :                                  const AlphaNum& g, const AlphaNum& h,
     719             :                                  const AlphaNum& i);
     720             : 
     721        2824 : inline string StrCat(const AlphaNum& a) { return string(a.data(), a.size()); }
     722             : 
     723             : // ----------------------------------------------------------------------
     724             : // StrAppend()
     725             : //    Same as above, but adds the output to the given string.
     726             : //    WARNING: For speed, StrAppend does not try to check each of its input
     727             : //    arguments to be sure that they are not a subset of the string being
     728             : //    appended to.  That is, while this will work:
     729             : //
     730             : //    string s = "foo";
     731             : //    s += s;
     732             : //
     733             : //    This will not (necessarily) work:
     734             : //
     735             : //    string s = "foo";
     736             : //    StrAppend(&s, s);
     737             : //
     738             : //    Note: while StrCat supports appending up to 9 arguments, StrAppend
     739             : //    is currently limited to 4.  That's rarely an issue except when
     740             : //    automatically transforming StrCat to StrAppend, and can easily be
     741             : //    worked around as consecutive calls to StrAppend are quite efficient.
     742             : // ----------------------------------------------------------------------
     743             : 
     744             : LIBPROTOBUF_EXPORT void StrAppend(string* dest, const AlphaNum& a);
     745             : LIBPROTOBUF_EXPORT void StrAppend(string* dest, const AlphaNum& a,
     746             :                                   const AlphaNum& b);
     747             : LIBPROTOBUF_EXPORT void StrAppend(string* dest, const AlphaNum& a,
     748             :                                   const AlphaNum& b, const AlphaNum& c);
     749             : LIBPROTOBUF_EXPORT void StrAppend(string* dest, const AlphaNum& a,
     750             :                                   const AlphaNum& b, const AlphaNum& c,
     751             :                                   const AlphaNum& d);
     752             : 
     753             : // ----------------------------------------------------------------------
     754             : // Join()
     755             : //    These methods concatenate a range of components into a C++ string, using
     756             : //    the C-string "delim" as a separator between components.
     757             : // ----------------------------------------------------------------------
     758             : template <typename Iterator>
     759        1174 : void Join(Iterator start, Iterator end,
     760             :           const char* delim, string* result) {
     761        3872 :   for (Iterator it = start; it != end; ++it) {
     762        1524 :     if (it != start) {
     763         367 :       result->append(delim);
     764             :     }
     765        3048 :     StrAppend(result, *it);
     766             :   }
     767        1174 : }
     768             : 
     769             : template <typename Range>
     770        3522 : string Join(const Range& components,
     771             :             const char* delim) {
     772             :   string result;
     773        3522 :   Join(components.begin(), components.end(), delim, &result);
     774        1174 :   return result;
     775             : }
     776             : 
     777             : // ----------------------------------------------------------------------
     778             : // ToHex()
     779             : //    Return a lower-case hex string representation of the given integer.
     780             : // ----------------------------------------------------------------------
     781             : LIBPROTOBUF_EXPORT string ToHex(uint64 num);
     782             : 
     783             : // ----------------------------------------------------------------------
     784             : // GlobalReplaceSubstring()
     785             : //    Replaces all instances of a substring in a string.  Does nothing
     786             : //    if 'substring' is empty.  Returns the number of replacements.
     787             : //
     788             : //    NOTE: The string pieces must not overlap s.
     789             : // ----------------------------------------------------------------------
     790             : LIBPROTOBUF_EXPORT int GlobalReplaceSubstring(const string& substring,
     791             :                                               const string& replacement,
     792             :                                               string* s);
     793             : 
     794             : // ----------------------------------------------------------------------
     795             : // Base64Unescape()
     796             : //    Converts "src" which is encoded in Base64 to its binary equivalent and
     797             : //    writes it to "dest". If src contains invalid characters, dest is cleared
     798             : //    and the function returns false. Returns true on success.
     799             : // ----------------------------------------------------------------------
     800             : LIBPROTOBUF_EXPORT bool Base64Unescape(StringPiece src, string* dest);
     801             : 
     802             : // ----------------------------------------------------------------------
     803             : // WebSafeBase64Unescape()
     804             : //    This is a variation of Base64Unescape which uses '-' instead of '+', and
     805             : //    '_' instead of '/'. src is not null terminated, instead specify len. I
     806             : //    recommend that slen<szdest, but we honor szdest anyway.
     807             : //    RETURNS the length of dest, or -1 if src contains invalid chars.
     808             : 
     809             : //    The variation that stores into a string clears the string first, and
     810             : //    returns false (with dest empty) if src contains invalid chars; for
     811             : //    this version src and dest must be different strings.
     812             : // ----------------------------------------------------------------------
     813             : LIBPROTOBUF_EXPORT int WebSafeBase64Unescape(const char* src, int slen,
     814             :                                              char* dest, int szdest);
     815             : LIBPROTOBUF_EXPORT bool WebSafeBase64Unescape(StringPiece src, string* dest);
     816             : 
     817             : // Return the length to use for the output buffer given to the base64 escape
     818             : // routines. Make sure to use the same value for do_padding in both.
     819             : // This function may return incorrect results if given input_len values that
     820             : // are extremely high, which should happen rarely.
     821             : LIBPROTOBUF_EXPORT int CalculateBase64EscapedLen(int input_len,
     822             :                                                  bool do_padding);
     823             : // Use this version when calling Base64Escape without a do_padding arg.
     824             : LIBPROTOBUF_EXPORT int CalculateBase64EscapedLen(int input_len);
     825             : 
     826             : // ----------------------------------------------------------------------
     827             : // Base64Escape()
     828             : // WebSafeBase64Escape()
     829             : //    Encode "src" to "dest" using base64 encoding.
     830             : //    src is not null terminated, instead specify len.
     831             : //    'dest' should have at least CalculateBase64EscapedLen() length.
     832             : //    RETURNS the length of dest.
     833             : //    The WebSafe variation use '-' instead of '+' and '_' instead of '/'
     834             : //    so that we can place the out in the URL or cookies without having
     835             : //    to escape them.  It also has an extra parameter "do_padding",
     836             : //    which when set to false will prevent padding with "=".
     837             : // ----------------------------------------------------------------------
     838             : LIBPROTOBUF_EXPORT int Base64Escape(const unsigned char* src, int slen,
     839             :                                     char* dest, int szdest);
     840             : LIBPROTOBUF_EXPORT int WebSafeBase64Escape(
     841             :     const unsigned char* src, int slen, char* dest,
     842             :     int szdest, bool do_padding);
     843             : // Encode src into dest with padding.
     844             : LIBPROTOBUF_EXPORT void Base64Escape(StringPiece src, string* dest);
     845             : // Encode src into dest web-safely without padding.
     846             : LIBPROTOBUF_EXPORT void WebSafeBase64Escape(StringPiece src, string* dest);
     847             : // Encode src into dest web-safely with padding.
     848             : LIBPROTOBUF_EXPORT void WebSafeBase64EscapeWithPadding(StringPiece src,
     849             :                                                        string* dest);
     850             : 
     851             : LIBPROTOBUF_EXPORT void Base64Escape(const unsigned char* src, int szsrc,
     852             :                                      string* dest, bool do_padding);
     853             : LIBPROTOBUF_EXPORT void WebSafeBase64Escape(const unsigned char* src, int szsrc,
     854             :                                             string* dest, bool do_padding);
     855             : 
     856             : static const int UTFmax = 4;
     857             : // ----------------------------------------------------------------------
     858             : // EncodeAsUTF8Char()
     859             : //  Helper to append a Unicode code point to a string as UTF8, without bringing
     860             : //  in any external dependencies. The output buffer must be as least 4 bytes
     861             : //  large.
     862             : // ----------------------------------------------------------------------
     863             : LIBPROTOBUF_EXPORT int EncodeAsUTF8Char(uint32 code_point, char* output);
     864             : 
     865             : // ----------------------------------------------------------------------
     866             : // UTF8FirstLetterNumBytes()
     867             : //   Length of the first UTF-8 character.
     868             : // ----------------------------------------------------------------------
     869             : LIBPROTOBUF_EXPORT int UTF8FirstLetterNumBytes(const char* src, int len);
     870             : 
     871             : }  // namespace protobuf
     872             : }  // namespace google
     873             : 
     874             : #endif  // GOOGLE_PROTOBUF_STUBS_STRUTIL_H__

Generated by: LCOV version 1.10