/thirdparty/breakpad/third_party/protobuf/protobuf/src/google/protobuf/stubs/substitute.h

http://github.com/tomahawk-player/tomahawk · C++ Header · 170 lines · 77 code · 16 blank · 77 comment · 0 complexity · 31313a006d9f1e6af6cad4dc4e48d412 MD5 · raw file

  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  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. // Author: kenton@google.com (Kenton Varda)
  31. // from google3/strings/substitute.h
  32. #include <string>
  33. #include <google/protobuf/stubs/common.h>
  34. #include <google/protobuf/stubs/strutil.h>
  35. #ifndef GOOGLE_PROTOBUF_STUBS_SUBSTITUTE_H_
  36. #define GOOGLE_PROTOBUF_STUBS_SUBSTITUTE_H_
  37. namespace google {
  38. namespace protobuf {
  39. namespace strings {
  40. // ----------------------------------------------------------------------
  41. // strings::Substitute()
  42. // strings::SubstituteAndAppend()
  43. // Kind of like StringPrintf, but different.
  44. //
  45. // Example:
  46. // string GetMessage(string first_name, string last_name, int age) {
  47. // return strings::Substitute("My name is $0 $1 and I am $2 years old.",
  48. // first_name, last_name, age);
  49. // }
  50. //
  51. // Differences from StringPrintf:
  52. // * The format string does not identify the types of arguments.
  53. // Instead, the magic of C++ deals with this for us. See below
  54. // for a list of accepted types.
  55. // * Substitutions in the format string are identified by a '$'
  56. // followed by a digit. So, you can use arguments out-of-order and
  57. // use the same argument multiple times.
  58. // * It's much faster than StringPrintf.
  59. //
  60. // Supported types:
  61. // * Strings (const char*, const string&)
  62. // * Note that this means you do not have to add .c_str() to all of
  63. // your strings. In fact, you shouldn't; it will be slower.
  64. // * int32, int64, uint32, uint64: Formatted using SimpleItoa().
  65. // * float, double: Formatted using SimpleFtoa() and SimpleDtoa().
  66. // * bool: Printed as "true" or "false".
  67. //
  68. // SubstituteAndAppend() is like Substitute() but appends the result to
  69. // *output. Example:
  70. //
  71. // string str;
  72. // strings::SubstituteAndAppend(&str,
  73. // "My name is $0 $1 and I am $2 years old.",
  74. // first_name, last_name, age);
  75. //
  76. // Substitute() is significantly faster than StringPrintf(). For very
  77. // large strings, it may be orders of magnitude faster.
  78. // ----------------------------------------------------------------------
  79. namespace internal { // Implementation details.
  80. class SubstituteArg {
  81. public:
  82. inline SubstituteArg(const char* value)
  83. : text_(value), size_(strlen(text_)) {}
  84. inline SubstituteArg(const string& value)
  85. : text_(value.data()), size_(value.size()) {}
  86. // Indicates that no argument was given.
  87. inline explicit SubstituteArg()
  88. : text_(NULL), size_(-1) {}
  89. // Primitives
  90. // We don't overload for signed and unsigned char because if people are
  91. // explicitly declaring their chars as signed or unsigned then they are
  92. // probably actually using them as 8-bit integers and would probably
  93. // prefer an integer representation. But, we don't really know. So, we
  94. // make the caller decide what to do.
  95. inline SubstituteArg(char value)
  96. : text_(scratch_), size_(1) { scratch_[0] = value; }
  97. inline SubstituteArg(short value)
  98. : text_(FastInt32ToBuffer(value, scratch_)), size_(strlen(text_)) {}
  99. inline SubstituteArg(unsigned short value)
  100. : text_(FastUInt32ToBuffer(value, scratch_)), size_(strlen(text_)) {}
  101. inline SubstituteArg(int value)
  102. : text_(FastInt32ToBuffer(value, scratch_)), size_(strlen(text_)) {}
  103. inline SubstituteArg(unsigned int value)
  104. : text_(FastUInt32ToBuffer(value, scratch_)), size_(strlen(text_)) {}
  105. inline SubstituteArg(long value)
  106. : text_(FastLongToBuffer(value, scratch_)), size_(strlen(text_)) {}
  107. inline SubstituteArg(unsigned long value)
  108. : text_(FastULongToBuffer(value, scratch_)), size_(strlen(text_)) {}
  109. inline SubstituteArg(long long value)
  110. : text_(FastInt64ToBuffer(value, scratch_)), size_(strlen(text_)) {}
  111. inline SubstituteArg(unsigned long long value)
  112. : text_(FastUInt64ToBuffer(value, scratch_)), size_(strlen(text_)) {}
  113. inline SubstituteArg(float value)
  114. : text_(FloatToBuffer(value, scratch_)), size_(strlen(text_)) {}
  115. inline SubstituteArg(double value)
  116. : text_(DoubleToBuffer(value, scratch_)), size_(strlen(text_)) {}
  117. inline SubstituteArg(bool value)
  118. : text_(value ? "true" : "false"), size_(strlen(text_)) {}
  119. inline const char* data() const { return text_; }
  120. inline int size() const { return size_; }
  121. private:
  122. const char* text_;
  123. int size_;
  124. char scratch_[kFastToBufferSize];
  125. };
  126. } // namespace internal
  127. LIBPROTOBUF_EXPORT string Substitute(
  128. const char* format,
  129. const internal::SubstituteArg& arg0 = internal::SubstituteArg(),
  130. const internal::SubstituteArg& arg1 = internal::SubstituteArg(),
  131. const internal::SubstituteArg& arg2 = internal::SubstituteArg(),
  132. const internal::SubstituteArg& arg3 = internal::SubstituteArg(),
  133. const internal::SubstituteArg& arg4 = internal::SubstituteArg(),
  134. const internal::SubstituteArg& arg5 = internal::SubstituteArg(),
  135. const internal::SubstituteArg& arg6 = internal::SubstituteArg(),
  136. const internal::SubstituteArg& arg7 = internal::SubstituteArg(),
  137. const internal::SubstituteArg& arg8 = internal::SubstituteArg(),
  138. const internal::SubstituteArg& arg9 = internal::SubstituteArg());
  139. LIBPROTOBUF_EXPORT void SubstituteAndAppend(
  140. string* output, const char* format,
  141. const internal::SubstituteArg& arg0 = internal::SubstituteArg(),
  142. const internal::SubstituteArg& arg1 = internal::SubstituteArg(),
  143. const internal::SubstituteArg& arg2 = internal::SubstituteArg(),
  144. const internal::SubstituteArg& arg3 = internal::SubstituteArg(),
  145. const internal::SubstituteArg& arg4 = internal::SubstituteArg(),
  146. const internal::SubstituteArg& arg5 = internal::SubstituteArg(),
  147. const internal::SubstituteArg& arg6 = internal::SubstituteArg(),
  148. const internal::SubstituteArg& arg7 = internal::SubstituteArg(),
  149. const internal::SubstituteArg& arg8 = internal::SubstituteArg(),
  150. const internal::SubstituteArg& arg9 = internal::SubstituteArg());
  151. } // namespace strings
  152. } // namespace protobuf
  153. } // namespace google
  154. #endif // GOOGLE_PROTOBUF_STUBS_SUBSTITUTE_H_