diff options
author | jieluo@google.com <jieluo@google.com@630680e5-0e50-0410-840e-4b1c322b438d> | 2014-07-18 00:47:59 +0000 |
---|---|---|
committer | jieluo@google.com <jieluo@google.com@630680e5-0e50-0410-840e-4b1c322b438d> | 2014-07-18 00:47:59 +0000 |
commit | 4de8f55113007fdc8e34107950e605fc0209d465 (patch) | |
tree | 92b7da8757a7740d9e1f2d3ead233542947d8c8c /src/google/protobuf/stubs/strutil.h | |
parent | c5553a3d18f80132b9079c5504bc0aa1f7f950a0 (diff) | |
download | protobuf-4de8f55113007fdc8e34107950e605fc0209d465.tar.gz protobuf-4de8f55113007fdc8e34107950e605fc0209d465.tar.bz2 protobuf-4de8f55113007fdc8e34107950e605fc0209d465.zip |
down integrate to svn
Diffstat (limited to 'src/google/protobuf/stubs/strutil.h')
-rw-r--r-- | src/google/protobuf/stubs/strutil.h | 103 |
1 files changed, 99 insertions, 4 deletions
diff --git a/src/google/protobuf/stubs/strutil.h b/src/google/protobuf/stubs/strutil.h index a401c63a..20cea2d2 100644 --- a/src/google/protobuf/stubs/strutil.h +++ b/src/google/protobuf/stubs/strutil.h @@ -126,6 +126,7 @@ LIBPROTOBUF_EXPORT void StripString(string* s, const char* remove, // ---------------------------------------------------------------------- // LowerString() // UpperString() +// ToUpper() // Convert the characters in "s" to lowercase or uppercase. ASCII-only: // these functions intentionally ignore locale because they are applied to // identifiers used in the Protocol Buffer language, not to natural-language @@ -148,6 +149,12 @@ inline void UpperString(string * s) { } } +inline string ToUpper(const string& s) { + string out = s; + UpperString(&out); + return out; +} + // ---------------------------------------------------------------------- // StringReplace() // Give me a string and two patterns "old" and "new", and I replace @@ -181,6 +188,21 @@ LIBPROTOBUF_EXPORT void SplitStringAllowEmpty(const string& full, vector<string>* result); // ---------------------------------------------------------------------- +// Split() +// Split a string using a character delimiter. +// ---------------------------------------------------------------------- +inline vector<string> Split( + const string& full, const char* delim, bool skip_empty = true) { + vector<string> result; + if (skip_empty) { + SplitStringUsing(full, delim, &result); + } else { + SplitStringAllowEmpty(full, delim, &result); + } + return result; +} + +// ---------------------------------------------------------------------- // JoinStrings() // These methods concatenate a vector of strings into a C++ string, using // the C-string "delim" as a separator between components. There are two @@ -327,6 +349,15 @@ inline uint64 strtou64(const char *nptr, char **endptr, int base) { } // ---------------------------------------------------------------------- +// safe_strto32() +// ---------------------------------------------------------------------- +LIBPROTOBUF_EXPORT bool safe_int(string text, int32* value_p); + +inline bool safe_strto32(string text, int32* value) { + return safe_int(text, value); +} + +// ---------------------------------------------------------------------- // FastIntToBuffer() // FastHexToBuffer() // FastHex64ToBuffer() @@ -454,12 +485,76 @@ static const int kDoubleToBufferSize = 32; static const int kFloatToBufferSize = 24; // ---------------------------------------------------------------------- -// NoLocaleStrtod() -// Exactly like strtod(), except it always behaves as if in the "C" -// locale (i.e. decimal points must be '.'s). +// ToString() are internal help methods used in StrCat() and Join() +// ---------------------------------------------------------------------- +namespace internal { +inline string ToString(int i) { + return SimpleItoa(i); +} + +inline string ToString(string a) { + return a; +} +} // namespace internal + +// ---------------------------------------------------------------------- +// StrCat() +// These methods join some strings together. // ---------------------------------------------------------------------- +template <typename T1, typename T2, typename T3, typename T4, typename T5> +string StrCat( + const T1& a, const T2& b, const T3& c, const T4& d, const T5& e) { + return internal::ToString(a) + internal::ToString(b) + + internal::ToString(c) + internal::ToString(d) + internal::ToString(e); +} + +template <typename T1, typename T2, typename T3, typename T4> +string StrCat( + const T1& a, const T2& b, const T3& c, const T4& d) { + return internal::ToString(a) + internal::ToString(b) + + internal::ToString(c) + internal::ToString(d); +} + +template <typename T1, typename T2, typename T3> +string StrCat(const T1& a, const T2& b, const T3& c) { + return internal::ToString(a) + internal::ToString(b) + + internal::ToString(c); +} + +template <typename T1, typename T2> +string StrCat(const T1& a, const T2& b) { + return internal::ToString(a) + internal::ToString(b); +} -LIBPROTOBUF_EXPORT double NoLocaleStrtod(const char* text, char** endptr); +// ---------------------------------------------------------------------- +// Join() +// These methods concatenate a range of components into a C++ string, using +// the C-string "delim" as a separator between components. +// ---------------------------------------------------------------------- +template <typename Iterator> +void Join(Iterator start, Iterator end, + const char* delim, string* result) { + for (Iterator it = start; it != end; ++it) { + if (it != start) { + result->append(delim); + } + result->append(internal::ToString(*it)); + } +} + +template <typename Range> +string Join(const Range& components, + const char* delim) { + string result; + Join(components.begin(), components.end(), delim, &result); + return result; +} + +// ---------------------------------------------------------------------- +// ToHex() +// Return a lower-case hex string representation of the given integer. +// ---------------------------------------------------------------------- +LIBPROTOBUF_EXPORT string ToHex(uint64 num); } // namespace protobuf } // namespace google |