substitute.cc
Go to the documentation of this file.
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
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 
31 // Author: kenton@google.com (Kenton Varda)
32 
34 
38 
39 namespace google {
40 namespace protobuf {
41 namespace strings {
42 
43 using internal::SubstituteArg;
44 
45 // Returns the number of args in arg_array which were passed explicitly
46 // to Substitute().
47 static int CountSubstituteArgs(const SubstituteArg* const* args_array) {
48  int count = 0;
49  while (args_array[count] != nullptr && args_array[count]->size() != -1) {
50  ++count;
51  }
52  return count;
53 }
54 
55 string Substitute(
56  const char* format,
57  const SubstituteArg& arg0, const SubstituteArg& arg1,
58  const SubstituteArg& arg2, const SubstituteArg& arg3,
59  const SubstituteArg& arg4, const SubstituteArg& arg5,
60  const SubstituteArg& arg6, const SubstituteArg& arg7,
61  const SubstituteArg& arg8, const SubstituteArg& arg9) {
62  string result;
63  SubstituteAndAppend(&result, format, arg0, arg1, arg2, arg3, arg4,
64  arg5, arg6, arg7, arg8, arg9);
65  return result;
66 }
67 
69  string* output, const char* format,
70  const SubstituteArg& arg0, const SubstituteArg& arg1,
71  const SubstituteArg& arg2, const SubstituteArg& arg3,
72  const SubstituteArg& arg4, const SubstituteArg& arg5,
73  const SubstituteArg& arg6, const SubstituteArg& arg7,
74  const SubstituteArg& arg8, const SubstituteArg& arg9) {
75  const SubstituteArg* const args_array[] = {
76  &arg0, &arg1, &arg2, &arg3, &arg4, &arg5, &arg6, &arg7, &arg8, &arg9, nullptr
77  };
78 
79  // Determine total size needed.
80  int size = 0;
81  for (int i = 0; format[i] != '\0'; i++) {
82  if (format[i] == '$') {
83  if (ascii_isdigit(format[i+1])) {
84  int index = format[i+1] - '0';
85  if (args_array[index]->size() == -1) {
86  GOOGLE_LOG(DFATAL)
87  << "strings::Substitute format string invalid: asked for \"$"
88  << index << "\", but only " << CountSubstituteArgs(args_array)
89  << " args were given. Full format string was: \""
90  << CEscape(format) << "\".";
91  return;
92  }
93  size += args_array[index]->size();
94  ++i; // Skip next char.
95  } else if (format[i+1] == '$') {
96  ++size;
97  ++i; // Skip next char.
98  } else {
99  GOOGLE_LOG(DFATAL)
100  << "Invalid strings::Substitute() format string: \""
101  << CEscape(format) << "\".";
102  return;
103  }
104  } else {
105  ++size;
106  }
107  }
108 
109  if (size == 0) return;
110 
111  // Build the string.
112  int original_size = output->size();
113  STLStringResizeUninitialized(output, original_size + size);
114  char* target = string_as_array(output) + original_size;
115  for (int i = 0; format[i] != '\0'; i++) {
116  if (format[i] == '$') {
117  if (ascii_isdigit(format[i+1])) {
118  unsigned int index = format[i+1] - '0';
119  assert(index < 10);
120  const SubstituteArg* src = args_array[index];
121  memcpy(target, src->data(), src->size());
122  target += src->size();
123  ++i; // Skip next char.
124  } else if (format[i+1] == '$') {
125  *target++ = '$';
126  ++i; // Skip next char.
127  }
128  } else {
129  *target++ = format[i];
130  }
131  }
132 
133  GOOGLE_DCHECK_EQ(target - output->data(), output->size());
134 }
135 
136 } // namespace strings
137 } // namespace protobuf
138 } // namespace google
src
GLenum src
Definition: glcorearb.h:3364
strings
GLsizei const GLchar *const * strings
Definition: glcorearb.h:4046
google::protobuf::strings::internal::SubstituteArg
Definition: substitute.h:89
google::protobuf::CEscape
string CEscape(const string &src)
Definition: strutil.cc:615
target
GLenum target
Definition: glcorearb.h:3739
google::protobuf::strings::internal::SubstituteArg::size
int size() const
Definition: substitute.h:132
google::protobuf::strings::CountSubstituteArgs
static int CountSubstituteArgs(const SubstituteArg *const *args_array)
Definition: substitute.cc:47
strutil.h
format
GLint GLint GLsizei GLint GLenum format
Definition: glcorearb.h:2773
GOOGLE_LOG
#define GOOGLE_LOG(LEVEL)
Definition: logging.h:146
size
#define size
Definition: glcorearb.h:2944
google::protobuf::strings::Substitute
string Substitute(const char *format, const SubstituteArg &arg0, const SubstituteArg &arg1, const SubstituteArg &arg2, const SubstituteArg &arg3, const SubstituteArg &arg4, const SubstituteArg &arg5, const SubstituteArg &arg6, const SubstituteArg &arg7, const SubstituteArg &arg8, const SubstituteArg &arg9)
Definition: substitute.cc:55
google::protobuf::string_as_array
char * string_as_array(string *str)
Definition: stl_util.h:83
i
int i
Definition: gmock-matchers_test.cc:764
size
GLsizeiptr size
Definition: glcorearb.h:2943
stl_util.h
google::protobuf::ascii_isdigit
bool ascii_isdigit(char c)
Definition: strutil.h:73
logging.h
substitute.h
output
const upb_json_parsermethod const upb_symtab upb_sink * output
Definition: ruby/ext/google/protobuf_c/upb.h:10503
count
GLint GLsizei count
Definition: glcorearb.h:2830
index
GLuint index
Definition: glcorearb.h:3055
google::protobuf::STLStringResizeUninitialized
void STLStringResizeUninitialized(string *s, size_t new_size)
Definition: stl_util.h:67
google
Definition: data_proto2_to_proto3_util.h:11
google::protobuf::strings::SubstituteAndAppend
void SubstituteAndAppend(string *output, const char *format, const SubstituteArg &arg0, const SubstituteArg &arg1, const SubstituteArg &arg2, const SubstituteArg &arg3, const SubstituteArg &arg4, const SubstituteArg &arg5, const SubstituteArg &arg6, const SubstituteArg &arg7, const SubstituteArg &arg8, const SubstituteArg &arg9)
Definition: substitute.cc:68
GOOGLE_DCHECK_EQ
#define GOOGLE_DCHECK_EQ
Definition: logging.h:196


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:06:59