1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
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
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.
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.
31 // Author: kenton@google.com (Kenton Varda)
32 // from google3/strings/substitute.h
35 #include <google/protobuf/stubs/common.h>
36 #include <google/protobuf/stubs/strutil.h>
38 #ifndef GOOGLE_PROTOBUF_STUBS_SUBSTITUTE_H_
39 #define GOOGLE_PROTOBUF_STUBS_SUBSTITUTE_H_
41 #include <google/protobuf/port_def.inc>
47 // ----------------------------------------------------------------------
48 // strings::Substitute()
49 // strings::SubstituteAndAppend()
50 // Kind of like StringPrintf, but different.
53 // string GetMessage(string first_name, string last_name, int age) {
54 // return strings::Substitute("My name is $0 $1 and I am $2 years old.",
55 // first_name, last_name, age);
58 // Differences from StringPrintf:
59 // * The format string does not identify the types of arguments.
60 // Instead, the magic of C++ deals with this for us. See below
61 // for a list of accepted types.
62 // * Substitutions in the format string are identified by a '$'
63 // followed by a digit. So, you can use arguments out-of-order and
64 // use the same argument multiple times.
65 // * It's much faster than StringPrintf.
68 // * Strings (const char*, const string&)
69 // * Note that this means you do not have to add .c_str() to all of
70 // your strings. In fact, you shouldn't; it will be slower.
71 // * int32, int64, uint32, uint64: Formatted using SimpleItoa().
72 // * float, double: Formatted using SimpleFtoa() and SimpleDtoa().
73 // * bool: Printed as "true" or "false".
75 // SubstituteAndAppend() is like Substitute() but appends the result to
79 // strings::SubstituteAndAppend(&str,
80 // "My name is $0 $1 and I am $2 years old.",
81 // first_name, last_name, age);
83 // Substitute() is significantly faster than StringPrintf(). For very
84 // large strings, it may be orders of magnitude faster.
85 // ----------------------------------------------------------------------
87 namespace internal { // Implementation details.
91 inline SubstituteArg(const char* value)
92 : text_(value), size_(strlen(text_)) {}
93 inline SubstituteArg(const string& value)
94 : text_(value.data()), size_(value.size()) {}
96 // Indicates that no argument was given.
97 inline explicit SubstituteArg()
98 : text_(nullptr), size_(-1) {}
101 // We don't overload for signed and unsigned char because if people are
102 // explicitly declaring their chars as signed or unsigned then they are
103 // probably actually using them as 8-bit integers and would probably
104 // prefer an integer representation. But, we don't really know. So, we
105 // make the caller decide what to do.
106 inline SubstituteArg(char value)
107 : text_(scratch_), size_(1) { scratch_[0] = value; }
108 inline SubstituteArg(short value)
109 : text_(FastInt32ToBuffer(value, scratch_)), size_(strlen(text_)) {}
110 inline SubstituteArg(unsigned short value)
111 : text_(FastUInt32ToBuffer(value, scratch_)), size_(strlen(text_)) {}
112 inline SubstituteArg(int value)
113 : text_(FastInt32ToBuffer(value, scratch_)), size_(strlen(text_)) {}
114 inline SubstituteArg(unsigned int value)
115 : text_(FastUInt32ToBuffer(value, scratch_)), size_(strlen(text_)) {}
116 inline SubstituteArg(long value)
117 : text_(FastLongToBuffer(value, scratch_)), size_(strlen(text_)) {}
118 inline SubstituteArg(unsigned long value)
119 : text_(FastULongToBuffer(value, scratch_)), size_(strlen(text_)) {}
120 inline SubstituteArg(long long value)
121 : text_(FastInt64ToBuffer(value, scratch_)), size_(strlen(text_)) {}
122 inline SubstituteArg(unsigned long long value)
123 : text_(FastUInt64ToBuffer(value, scratch_)), size_(strlen(text_)) {}
124 inline SubstituteArg(float value)
125 : text_(FloatToBuffer(value, scratch_)), size_(strlen(text_)) {}
126 inline SubstituteArg(double value)
127 : text_(DoubleToBuffer(value, scratch_)), size_(strlen(text_)) {}
128 inline SubstituteArg(bool value)
129 : text_(value ? "true" : "false"), size_(strlen(text_)) {}
131 inline const char* data() const { return text_; }
132 inline int size() const { return size_; }
137 char scratch_[kFastToBufferSize];
140 } // namespace internal
142 PROTOBUF_EXPORT string
143 Substitute(const char* format,
144 const internal::SubstituteArg& arg0 = internal::SubstituteArg(),
145 const internal::SubstituteArg& arg1 = internal::SubstituteArg(),
146 const internal::SubstituteArg& arg2 = internal::SubstituteArg(),
147 const internal::SubstituteArg& arg3 = internal::SubstituteArg(),
148 const internal::SubstituteArg& arg4 = internal::SubstituteArg(),
149 const internal::SubstituteArg& arg5 = internal::SubstituteArg(),
150 const internal::SubstituteArg& arg6 = internal::SubstituteArg(),
151 const internal::SubstituteArg& arg7 = internal::SubstituteArg(),
152 const internal::SubstituteArg& arg8 = internal::SubstituteArg(),
153 const internal::SubstituteArg& arg9 = internal::SubstituteArg());
155 PROTOBUF_EXPORT void SubstituteAndAppend(
156 string* output, const char* format,
157 const internal::SubstituteArg& arg0 = internal::SubstituteArg(),
158 const internal::SubstituteArg& arg1 = internal::SubstituteArg(),
159 const internal::SubstituteArg& arg2 = internal::SubstituteArg(),
160 const internal::SubstituteArg& arg3 = internal::SubstituteArg(),
161 const internal::SubstituteArg& arg4 = internal::SubstituteArg(),
162 const internal::SubstituteArg& arg5 = internal::SubstituteArg(),
163 const internal::SubstituteArg& arg6 = internal::SubstituteArg(),
164 const internal::SubstituteArg& arg7 = internal::SubstituteArg(),
165 const internal::SubstituteArg& arg8 = internal::SubstituteArg(),
166 const internal::SubstituteArg& arg9 = internal::SubstituteArg());
168 } // namespace strings
169 } // namespace protobuf
170 } // namespace google
172 #include <google/protobuf/port_undef.inc>
174 #endif // GOOGLE_PROTOBUF_STUBS_SUBSTITUTE_H_