OsmAnd
|
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 // Author: kenton@google.com (Kenton Varda) 00032 // from google3/strings/substitute.h 00033 00034 #include <string> 00035 #include <google/protobuf/stubs/common.h> 00036 #include <google/protobuf/stubs/strutil.h> 00037 00038 #ifndef GOOGLE_PROTOBUF_STUBS_SUBSTITUTE_H_ 00039 #define GOOGLE_PROTOBUF_STUBS_SUBSTITUTE_H_ 00040 00041 namespace google { 00042 namespace protobuf { 00043 namespace strings { 00044 00045 // ---------------------------------------------------------------------- 00046 // strings::Substitute() 00047 // strings::SubstituteAndAppend() 00048 // Kind of like StringPrintf, but different. 00049 // 00050 // Example: 00051 // string GetMessage(string first_name, string last_name, int age) { 00052 // return strings::Substitute("My name is $0 $1 and I am $2 years old.", 00053 // first_name, last_name, age); 00054 // } 00055 // 00056 // Differences from StringPrintf: 00057 // * The format string does not identify the types of arguments. 00058 // Instead, the magic of C++ deals with this for us. See below 00059 // for a list of accepted types. 00060 // * Substitutions in the format string are identified by a '$' 00061 // followed by a digit. So, you can use arguments out-of-order and 00062 // use the same argument multiple times. 00063 // * It's much faster than StringPrintf. 00064 // 00065 // Supported types: 00066 // * Strings (const char*, const string&) 00067 // * Note that this means you do not have to add .c_str() to all of 00068 // your strings. In fact, you shouldn't; it will be slower. 00069 // * int32, int64, uint32, uint64: Formatted using SimpleItoa(). 00070 // * float, double: Formatted using SimpleFtoa() and SimpleDtoa(). 00071 // * bool: Printed as "true" or "false". 00072 // 00073 // SubstituteAndAppend() is like Substitute() but appends the result to 00074 // *output. Example: 00075 // 00076 // string str; 00077 // strings::SubstituteAndAppend(&str, 00078 // "My name is $0 $1 and I am $2 years old.", 00079 // first_name, last_name, age); 00080 // 00081 // Substitute() is significantly faster than StringPrintf(). For very 00082 // large strings, it may be orders of magnitude faster. 00083 // ---------------------------------------------------------------------- 00084 00085 namespace internal { // Implementation details. 00086 00087 class SubstituteArg { 00088 public: 00089 inline SubstituteArg(const char* value) 00090 : text_(value), size_(strlen(text_)) {} 00091 inline SubstituteArg(const string& value) 00092 : text_(value.data()), size_(value.size()) {} 00093 00094 // Indicates that no argument was given. 00095 inline explicit SubstituteArg() 00096 : text_(NULL), size_(-1) {} 00097 00098 // Primitives 00099 // We don't overload for signed and unsigned char because if people are 00100 // explicitly declaring their chars as signed or unsigned then they are 00101 // probably actually using them as 8-bit integers and would probably 00102 // prefer an integer representation. But, we don't really know. So, we 00103 // make the caller decide what to do. 00104 inline SubstituteArg(char value) 00105 : text_(scratch_), size_(1) { scratch_[0] = value; } 00106 inline SubstituteArg(short value) 00107 : text_(FastInt32ToBuffer(value, scratch_)), size_(strlen(text_)) {} 00108 inline SubstituteArg(unsigned short value) 00109 : text_(FastUInt32ToBuffer(value, scratch_)), size_(strlen(text_)) {} 00110 inline SubstituteArg(int value) 00111 : text_(FastInt32ToBuffer(value, scratch_)), size_(strlen(text_)) {} 00112 inline SubstituteArg(unsigned int value) 00113 : text_(FastUInt32ToBuffer(value, scratch_)), size_(strlen(text_)) {} 00114 inline SubstituteArg(long value) 00115 : text_(FastLongToBuffer(value, scratch_)), size_(strlen(text_)) {} 00116 inline SubstituteArg(unsigned long value) 00117 : text_(FastULongToBuffer(value, scratch_)), size_(strlen(text_)) {} 00118 inline SubstituteArg(long long value) 00119 : text_(FastInt64ToBuffer(value, scratch_)), size_(strlen(text_)) {} 00120 inline SubstituteArg(unsigned long long value) 00121 : text_(FastUInt64ToBuffer(value, scratch_)), size_(strlen(text_)) {} 00122 inline SubstituteArg(float value) 00123 : text_(FloatToBuffer(value, scratch_)), size_(strlen(text_)) {} 00124 inline SubstituteArg(double value) 00125 : text_(DoubleToBuffer(value, scratch_)), size_(strlen(text_)) {} 00126 inline SubstituteArg(bool value) 00127 : text_(value ? "true" : "false"), size_(strlen(text_)) {} 00128 00129 inline const char* data() const { return text_; } 00130 inline int size() const { return size_; } 00131 00132 private: 00133 const char* text_; 00134 int size_; 00135 char scratch_[kFastToBufferSize]; 00136 }; 00137 00138 } // namespace internal 00139 00140 LIBPROTOBUF_EXPORT string Substitute( 00141 const char* format, 00142 const internal::SubstituteArg& arg0 = internal::SubstituteArg(), 00143 const internal::SubstituteArg& arg1 = internal::SubstituteArg(), 00144 const internal::SubstituteArg& arg2 = internal::SubstituteArg(), 00145 const internal::SubstituteArg& arg3 = internal::SubstituteArg(), 00146 const internal::SubstituteArg& arg4 = internal::SubstituteArg(), 00147 const internal::SubstituteArg& arg5 = internal::SubstituteArg(), 00148 const internal::SubstituteArg& arg6 = internal::SubstituteArg(), 00149 const internal::SubstituteArg& arg7 = internal::SubstituteArg(), 00150 const internal::SubstituteArg& arg8 = internal::SubstituteArg(), 00151 const internal::SubstituteArg& arg9 = internal::SubstituteArg()); 00152 00153 LIBPROTOBUF_EXPORT void SubstituteAndAppend( 00154 string* output, const char* format, 00155 const internal::SubstituteArg& arg0 = internal::SubstituteArg(), 00156 const internal::SubstituteArg& arg1 = internal::SubstituteArg(), 00157 const internal::SubstituteArg& arg2 = internal::SubstituteArg(), 00158 const internal::SubstituteArg& arg3 = internal::SubstituteArg(), 00159 const internal::SubstituteArg& arg4 = internal::SubstituteArg(), 00160 const internal::SubstituteArg& arg5 = internal::SubstituteArg(), 00161 const internal::SubstituteArg& arg6 = internal::SubstituteArg(), 00162 const internal::SubstituteArg& arg7 = internal::SubstituteArg(), 00163 const internal::SubstituteArg& arg8 = internal::SubstituteArg(), 00164 const internal::SubstituteArg& arg9 = internal::SubstituteArg()); 00165 00166 } // namespace strings 00167 } // namespace protobuf 00168 } // namespace google 00169 00170 #endif // GOOGLE_PROTOBUF_STUBS_SUBSTITUTE_H_