OsmAnd
jni/protobuf/google/protobuf/stubs/strutil.h
Go to the documentation of this file.
00001 // Protocol Buffers - Google's data interchange format
00002 // Copyright 2008 Google Inc.  All rights reserved.
00003 // http://code.google.com/p/protobuf/
00004 //
00005 // Redistribution and use in source and binary forms, with or without
00006 // modification, are permitted provided that the following conditions are
00007 // met:
00008 //
00009 //     * Redistributions of source code must retain the above copyright
00010 // notice, this list of conditions and the following disclaimer.
00011 //     * Redistributions in binary form must reproduce the above
00012 // copyright notice, this list of conditions and the following disclaimer
00013 // in the documentation and/or other materials provided with the
00014 // distribution.
00015 //     * Neither the name of Google Inc. nor the names of its
00016 // contributors may be used to endorse or promote products derived from
00017 // this software without specific prior written permission.
00018 //
00019 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00022 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00023 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00024 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00025 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00026 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00027 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00028 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00029 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 
00031 // from google3/strings/strutil.h
00032 
00033 #ifndef GOOGLE_PROTOBUF_STUBS_STRUTIL_H__
00034 #define GOOGLE_PROTOBUF_STUBS_STRUTIL_H__
00035 
00036 #include <stdlib.h>
00037 #include <vector>
00038 #include <google/protobuf/stubs/common.h>
00039 
00040 namespace google {
00041 namespace protobuf {
00042 
00043 #ifdef _MSC_VER
00044 #define strtoll  _strtoi64
00045 #define strtoull _strtoui64
00046 #elif defined(__DECCXX) && defined(__osf__)
00047 // HP C++ on Tru64 does not have strtoll, but strtol is already 64-bit.
00048 #define strtoll strtol
00049 #define strtoull strtoul
00050 #endif
00051 
00052 // ----------------------------------------------------------------------
00053 // ascii_isalnum()
00054 //    Check if an ASCII character is alphanumeric.  We can't use ctype's
00055 //    isalnum() because it is affected by locale.  This function is applied
00056 //    to identifiers in the protocol buffer language, not to natural-language
00057 //    strings, so locale should not be taken into account.
00058 // ascii_isdigit()
00059 //    Like above, but only accepts digits.
00060 // ----------------------------------------------------------------------
00061 
00062 inline bool ascii_isalnum(char c) {
00063   return ('a' <= c && c <= 'z') ||
00064          ('A' <= c && c <= 'Z') ||
00065          ('0' <= c && c <= '9');
00066 }
00067 
00068 inline bool ascii_isdigit(char c) {
00069   return ('0' <= c && c <= '9');
00070 }
00071 
00072 // ----------------------------------------------------------------------
00073 // HasPrefixString()
00074 //    Check if a string begins with a given prefix.
00075 // StripPrefixString()
00076 //    Given a string and a putative prefix, returns the string minus the
00077 //    prefix string if the prefix matches, otherwise the original
00078 //    string.
00079 // ----------------------------------------------------------------------
00080 inline bool HasPrefixString(const string& str,
00081                             const string& prefix) {
00082   return str.size() >= prefix.size() &&
00083          str.compare(0, prefix.size(), prefix) == 0;
00084 }
00085 
00086 inline string StripPrefixString(const string& str, const string& prefix) {
00087   if (HasPrefixString(str, prefix)) {
00088     return str.substr(prefix.size());
00089   } else {
00090     return str;
00091   }
00092 }
00093 
00094 // ----------------------------------------------------------------------
00095 // HasSuffixString()
00096 //    Return true if str ends in suffix.
00097 // StripSuffixString()
00098 //    Given a string and a putative suffix, returns the string minus the
00099 //    suffix string if the suffix matches, otherwise the original
00100 //    string.
00101 // ----------------------------------------------------------------------
00102 inline bool HasSuffixString(const string& str,
00103                             const string& suffix) {
00104   return str.size() >= suffix.size() &&
00105          str.compare(str.size() - suffix.size(), suffix.size(), suffix) == 0;
00106 }
00107 
00108 inline string StripSuffixString(const string& str, const string& suffix) {
00109   if (HasSuffixString(str, suffix)) {
00110     return str.substr(0, str.size() - suffix.size());
00111   } else {
00112     return str;
00113   }
00114 }
00115 
00116 // ----------------------------------------------------------------------
00117 // StripString
00118 //    Replaces any occurrence of the character 'remove' (or the characters
00119 //    in 'remove') with the character 'replacewith'.
00120 //    Good for keeping html characters or protocol characters (\t) out
00121 //    of places where they might cause a problem.
00122 // ----------------------------------------------------------------------
00123 LIBPROTOBUF_EXPORT void StripString(string* s, const char* remove,
00124                                     char replacewith);
00125 
00126 // ----------------------------------------------------------------------
00127 // LowerString()
00128 // UpperString()
00129 //    Convert the characters in "s" to lowercase or uppercase.  ASCII-only:
00130 //    these functions intentionally ignore locale because they are applied to
00131 //    identifiers used in the Protocol Buffer language, not to natural-language
00132 //    strings.
00133 // ----------------------------------------------------------------------
00134 
00135 inline void LowerString(string * s) {
00136   string::iterator end = s->end();
00137   for (string::iterator i = s->begin(); i != end; ++i) {
00138     // tolower() changes based on locale.  We don't want this!
00139     if ('A' <= *i && *i <= 'Z') *i += 'a' - 'A';
00140   }
00141 }
00142 
00143 inline void UpperString(string * s) {
00144   string::iterator end = s->end();
00145   for (string::iterator i = s->begin(); i != end; ++i) {
00146     // toupper() changes based on locale.  We don't want this!
00147     if ('a' <= *i && *i <= 'z') *i += 'A' - 'a';
00148   }
00149 }
00150 
00151 // ----------------------------------------------------------------------
00152 // StringReplace()
00153 //    Give me a string and two patterns "old" and "new", and I replace
00154 //    the first instance of "old" in the string with "new", if it
00155 //    exists.  RETURN a new string, regardless of whether the replacement
00156 //    happened or not.
00157 // ----------------------------------------------------------------------
00158 
00159 LIBPROTOBUF_EXPORT string StringReplace(const string& s, const string& oldsub,
00160                                         const string& newsub, bool replace_all);
00161 
00162 // ----------------------------------------------------------------------
00163 // SplitStringUsing()
00164 //    Split a string using a character delimiter. Append the components
00165 //    to 'result'.  If there are consecutive delimiters, this function skips
00166 //    over all of them.
00167 // ----------------------------------------------------------------------
00168 LIBPROTOBUF_EXPORT void SplitStringUsing(const string& full, const char* delim,
00169                                          vector<string>* res);
00170 
00171 // ----------------------------------------------------------------------
00172 // JoinStrings()
00173 //    These methods concatenate a vector of strings into a C++ string, using
00174 //    the C-string "delim" as a separator between components. There are two
00175 //    flavors of the function, one flavor returns the concatenated string,
00176 //    another takes a pointer to the target string. In the latter case the
00177 //    target string is cleared and overwritten.
00178 // ----------------------------------------------------------------------
00179 LIBPROTOBUF_EXPORT void JoinStrings(const vector<string>& components,
00180                                     const char* delim, string* result);
00181 
00182 inline string JoinStrings(const vector<string>& components,
00183                           const char* delim) {
00184   string result;
00185   JoinStrings(components, delim, &result);
00186   return result;
00187 }
00188 
00189 // ----------------------------------------------------------------------
00190 // UnescapeCEscapeSequences()
00191 //    Copies "source" to "dest", rewriting C-style escape sequences
00192 //    -- '\n', '\r', '\\', '\ooo', etc -- to their ASCII
00193 //    equivalents.  "dest" must be sufficiently large to hold all
00194 //    the characters in the rewritten string (i.e. at least as large
00195 //    as strlen(source) + 1 should be safe, since the replacements
00196 //    are always shorter than the original escaped sequences).  It's
00197 //    safe for source and dest to be the same.  RETURNS the length
00198 //    of dest.
00199 //
00200 //    It allows hex sequences \xhh, or generally \xhhhhh with an
00201 //    arbitrary number of hex digits, but all of them together must
00202 //    specify a value of a single byte (e.g. \x0045 is equivalent
00203 //    to \x45, and \x1234 is erroneous).
00204 //
00205 //    It also allows escape sequences of the form \uhhhh (exactly four
00206 //    hex digits, upper or lower case) or \Uhhhhhhhh (exactly eight
00207 //    hex digits, upper or lower case) to specify a Unicode code
00208 //    point. The dest array will contain the UTF8-encoded version of
00209 //    that code-point (e.g., if source contains \u2019, then dest will
00210 //    contain the three bytes 0xE2, 0x80, and 0x99). For the inverse
00211 //    transformation, use UniLib::UTF8EscapeString
00212 //    (util/utf8/unilib.h), not CEscapeString.
00213 //
00214 //    Errors: In the first form of the call, errors are reported with
00215 //    LOG(ERROR). The same is true for the second form of the call if
00216 //    the pointer to the string vector is NULL; otherwise, error
00217 //    messages are stored in the vector. In either case, the effect on
00218 //    the dest array is not defined, but rest of the source will be
00219 //    processed.
00220 //    ----------------------------------------------------------------------
00221 
00222 LIBPROTOBUF_EXPORT int UnescapeCEscapeSequences(const char* source, char* dest);
00223 LIBPROTOBUF_EXPORT int UnescapeCEscapeSequences(const char* source, char* dest,
00224                                                 vector<string> *errors);
00225 
00226 // ----------------------------------------------------------------------
00227 // UnescapeCEscapeString()
00228 //    This does the same thing as UnescapeCEscapeSequences, but creates
00229 //    a new string. The caller does not need to worry about allocating
00230 //    a dest buffer. This should be used for non performance critical
00231 //    tasks such as printing debug messages. It is safe for src and dest
00232 //    to be the same.
00233 //
00234 //    The second call stores its errors in a supplied string vector.
00235 //    If the string vector pointer is NULL, it reports the errors with LOG().
00236 //
00237 //    In the first and second calls, the length of dest is returned. In the
00238 //    the third call, the new string is returned.
00239 // ----------------------------------------------------------------------
00240 
00241 LIBPROTOBUF_EXPORT int UnescapeCEscapeString(const string& src, string* dest);
00242 LIBPROTOBUF_EXPORT int UnescapeCEscapeString(const string& src, string* dest,
00243                                              vector<string> *errors);
00244 LIBPROTOBUF_EXPORT string UnescapeCEscapeString(const string& src);
00245 
00246 // ----------------------------------------------------------------------
00247 // CEscapeString()
00248 //    Copies 'src' to 'dest', escaping dangerous characters using
00249 //    C-style escape sequences. This is very useful for preparing query
00250 //    flags. 'src' and 'dest' should not overlap.
00251 //    Returns the number of bytes written to 'dest' (not including the \0)
00252 //    or -1 if there was insufficient space.
00253 //
00254 //    Currently only \n, \r, \t, ", ', \ and !isprint() chars are escaped.
00255 // ----------------------------------------------------------------------
00256 LIBPROTOBUF_EXPORT int CEscapeString(const char* src, int src_len,
00257                                      char* dest, int dest_len);
00258 
00259 // ----------------------------------------------------------------------
00260 // CEscape()
00261 //    More convenient form of CEscapeString: returns result as a "string".
00262 //    This version is slower than CEscapeString() because it does more
00263 //    allocation.  However, it is much more convenient to use in
00264 //    non-speed-critical code like logging messages etc.
00265 // ----------------------------------------------------------------------
00266 LIBPROTOBUF_EXPORT string CEscape(const string& src);
00267 
00268 namespace strings {
00269 // Like CEscape() but does not escape bytes with the upper bit set.
00270 LIBPROTOBUF_EXPORT string Utf8SafeCEscape(const string& src);
00271 
00272 // Like CEscape() but uses hex (\x) escapes instead of octals.
00273 LIBPROTOBUF_EXPORT string CHexEscape(const string& src);
00274 }  // namespace strings
00275 
00276 // ----------------------------------------------------------------------
00277 // strto32()
00278 // strtou32()
00279 // strto64()
00280 // strtou64()
00281 //    Architecture-neutral plug compatible replacements for strtol() and
00282 //    strtoul().  Long's have different lengths on ILP-32 and LP-64
00283 //    platforms, so using these is safer, from the point of view of
00284 //    overflow behavior, than using the standard libc functions.
00285 // ----------------------------------------------------------------------
00286 LIBPROTOBUF_EXPORT int32 strto32_adaptor(const char *nptr, char **endptr,
00287                                          int base);
00288 LIBPROTOBUF_EXPORT uint32 strtou32_adaptor(const char *nptr, char **endptr,
00289                                            int base);
00290 
00291 inline int32 strto32(const char *nptr, char **endptr, int base) {
00292   if (sizeof(int32) == sizeof(long))
00293     return strtol(nptr, endptr, base);
00294   else
00295     return strto32_adaptor(nptr, endptr, base);
00296 }
00297 
00298 inline uint32 strtou32(const char *nptr, char **endptr, int base) {
00299   if (sizeof(uint32) == sizeof(unsigned long))
00300     return strtoul(nptr, endptr, base);
00301   else
00302     return strtou32_adaptor(nptr, endptr, base);
00303 }
00304 
00305 // For now, long long is 64-bit on all the platforms we care about, so these
00306 // functions can simply pass the call to strto[u]ll.
00307 inline int64 strto64(const char *nptr, char **endptr, int base) {
00308   GOOGLE_COMPILE_ASSERT(sizeof(int64) == sizeof(long long),
00309                         sizeof_int64_is_not_sizeof_long_long);
00310   return strtoll(nptr, endptr, base);
00311 }
00312 
00313 inline uint64 strtou64(const char *nptr, char **endptr, int base) {
00314   GOOGLE_COMPILE_ASSERT(sizeof(uint64) == sizeof(unsigned long long),
00315                         sizeof_uint64_is_not_sizeof_long_long);
00316   return strtoull(nptr, endptr, base);
00317 }
00318 
00319 // ----------------------------------------------------------------------
00320 // FastIntToBuffer()
00321 // FastHexToBuffer()
00322 // FastHex64ToBuffer()
00323 // FastHex32ToBuffer()
00324 // FastTimeToBuffer()
00325 //    These are intended for speed.  FastIntToBuffer() assumes the
00326 //    integer is non-negative.  FastHexToBuffer() puts output in
00327 //    hex rather than decimal.  FastTimeToBuffer() puts the output
00328 //    into RFC822 format.
00329 //
00330 //    FastHex64ToBuffer() puts a 64-bit unsigned value in hex-format,
00331 //    padded to exactly 16 bytes (plus one byte for '\0')
00332 //
00333 //    FastHex32ToBuffer() puts a 32-bit unsigned value in hex-format,
00334 //    padded to exactly 8 bytes (plus one byte for '\0')
00335 //
00336 //       All functions take the output buffer as an arg.
00337 //    They all return a pointer to the beginning of the output,
00338 //    which may not be the beginning of the input buffer.
00339 // ----------------------------------------------------------------------
00340 
00341 // Suggested buffer size for FastToBuffer functions.  Also works with
00342 // DoubleToBuffer() and FloatToBuffer().
00343 static const int kFastToBufferSize = 32;
00344 
00345 LIBPROTOBUF_EXPORT char* FastInt32ToBuffer(int32 i, char* buffer);
00346 LIBPROTOBUF_EXPORT char* FastInt64ToBuffer(int64 i, char* buffer);
00347 char* FastUInt32ToBuffer(uint32 i, char* buffer);  // inline below
00348 char* FastUInt64ToBuffer(uint64 i, char* buffer);  // inline below
00349 LIBPROTOBUF_EXPORT char* FastHexToBuffer(int i, char* buffer);
00350 LIBPROTOBUF_EXPORT char* FastHex64ToBuffer(uint64 i, char* buffer);
00351 LIBPROTOBUF_EXPORT char* FastHex32ToBuffer(uint32 i, char* buffer);
00352 
00353 // at least 22 bytes long
00354 inline char* FastIntToBuffer(int i, char* buffer) {
00355   return (sizeof(i) == 4 ?
00356           FastInt32ToBuffer(i, buffer) : FastInt64ToBuffer(i, buffer));
00357 }
00358 inline char* FastUIntToBuffer(unsigned int i, char* buffer) {
00359   return (sizeof(i) == 4 ?
00360           FastUInt32ToBuffer(i, buffer) : FastUInt64ToBuffer(i, buffer));
00361 }
00362 inline char* FastLongToBuffer(long i, char* buffer) {
00363   return (sizeof(i) == 4 ?
00364           FastInt32ToBuffer(i, buffer) : FastInt64ToBuffer(i, buffer));
00365 }
00366 inline char* FastULongToBuffer(unsigned long i, char* buffer) {
00367   return (sizeof(i) == 4 ?
00368           FastUInt32ToBuffer(i, buffer) : FastUInt64ToBuffer(i, buffer));
00369 }
00370 
00371 // ----------------------------------------------------------------------
00372 // FastInt32ToBufferLeft()
00373 // FastUInt32ToBufferLeft()
00374 // FastInt64ToBufferLeft()
00375 // FastUInt64ToBufferLeft()
00376 //
00377 // Like the Fast*ToBuffer() functions above, these are intended for speed.
00378 // Unlike the Fast*ToBuffer() functions, however, these functions write
00379 // their output to the beginning of the buffer (hence the name, as the
00380 // output is left-aligned).  The caller is responsible for ensuring that
00381 // the buffer has enough space to hold the output.
00382 //
00383 // Returns a pointer to the end of the string (i.e. the null character
00384 // terminating the string).
00385 // ----------------------------------------------------------------------
00386 
00387 LIBPROTOBUF_EXPORT char* FastInt32ToBufferLeft(int32 i, char* buffer);
00388 LIBPROTOBUF_EXPORT char* FastUInt32ToBufferLeft(uint32 i, char* buffer);
00389 LIBPROTOBUF_EXPORT char* FastInt64ToBufferLeft(int64 i, char* buffer);
00390 LIBPROTOBUF_EXPORT char* FastUInt64ToBufferLeft(uint64 i, char* buffer);
00391 
00392 // Just define these in terms of the above.
00393 inline char* FastUInt32ToBuffer(uint32 i, char* buffer) {
00394   FastUInt32ToBufferLeft(i, buffer);
00395   return buffer;
00396 }
00397 inline char* FastUInt64ToBuffer(uint64 i, char* buffer) {
00398   FastUInt64ToBufferLeft(i, buffer);
00399   return buffer;
00400 }
00401 
00402 // ----------------------------------------------------------------------
00403 // SimpleItoa()
00404 //    Description: converts an integer to a string.
00405 //
00406 //    Return value: string
00407 // ----------------------------------------------------------------------
00408 LIBPROTOBUF_EXPORT string SimpleItoa(int i);
00409 LIBPROTOBUF_EXPORT string SimpleItoa(unsigned int i);
00410 LIBPROTOBUF_EXPORT string SimpleItoa(long i);
00411 LIBPROTOBUF_EXPORT string SimpleItoa(unsigned long i);
00412 LIBPROTOBUF_EXPORT string SimpleItoa(long long i);
00413 LIBPROTOBUF_EXPORT string SimpleItoa(unsigned long long i);
00414 
00415 // ----------------------------------------------------------------------
00416 // SimpleDtoa()
00417 // SimpleFtoa()
00418 // DoubleToBuffer()
00419 // FloatToBuffer()
00420 //    Description: converts a double or float to a string which, if
00421 //    passed to NoLocaleStrtod(), will produce the exact same original double
00422 //    (except in case of NaN; all NaNs are considered the same value).
00423 //    We try to keep the string short but it's not guaranteed to be as
00424 //    short as possible.
00425 //
00426 //    DoubleToBuffer() and FloatToBuffer() write the text to the given
00427 //    buffer and return it.  The buffer must be at least
00428 //    kDoubleToBufferSize bytes for doubles and kFloatToBufferSize
00429 //    bytes for floats.  kFastToBufferSize is also guaranteed to be large
00430 //    enough to hold either.
00431 //
00432 //    Return value: string
00433 // ----------------------------------------------------------------------
00434 LIBPROTOBUF_EXPORT string SimpleDtoa(double value);
00435 LIBPROTOBUF_EXPORT string SimpleFtoa(float value);
00436 
00437 LIBPROTOBUF_EXPORT char* DoubleToBuffer(double i, char* buffer);
00438 LIBPROTOBUF_EXPORT char* FloatToBuffer(float i, char* buffer);
00439 
00440 // In practice, doubles should never need more than 24 bytes and floats
00441 // should never need more than 14 (including null terminators), but we
00442 // overestimate to be safe.
00443 static const int kDoubleToBufferSize = 32;
00444 static const int kFloatToBufferSize = 24;
00445 
00446 // ----------------------------------------------------------------------
00447 // NoLocaleStrtod()
00448 //   Exactly like strtod(), except it always behaves as if in the "C"
00449 //   locale (i.e. decimal points must be '.'s).
00450 // ----------------------------------------------------------------------
00451 
00452 LIBPROTOBUF_EXPORT double NoLocaleStrtod(const char* text, char** endptr);
00453 
00454 }  // namespace protobuf
00455 }  // namespace google
00456 
00457 #endif  // GOOGLE_PROTOBUF_STUBS_STRUTIL_H__
00458 
00459 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines