protobuf/src/google/protobuf/stubs/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 
33 #include <google/protobuf/stubs/substitute.h>
34 
35 #include <google/protobuf/stubs/logging.h>
36 #include <google/protobuf/stubs/strutil.h>
37 #include <google/protobuf/stubs/stl_util.h>
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 
56  const SubstituteArg& arg1, const SubstituteArg& arg2,
57  const SubstituteArg& arg3, const SubstituteArg& arg4,
58  const SubstituteArg& arg5, const SubstituteArg& arg6,
59  const SubstituteArg& arg7, const SubstituteArg& arg8,
60  const SubstituteArg& arg9) {
62  SubstituteAndAppend(&result, format.c_str(), arg0, arg1, arg2, arg3, arg4,
63  arg5, arg6, arg7, arg8, arg9);
64  return result;
65 }
66 
68  const SubstituteArg& arg0, const SubstituteArg& arg1,
69  const SubstituteArg& arg2, const SubstituteArg& arg3,
70  const SubstituteArg& arg4, const SubstituteArg& arg5,
71  const SubstituteArg& arg6, const SubstituteArg& arg7,
72  const SubstituteArg& arg8, const SubstituteArg& arg9) {
73  const SubstituteArg* const args_array[] = {
74  &arg0, &arg1, &arg2, &arg3, &arg4, &arg5, &arg6, &arg7, &arg8, &arg9, nullptr
75  };
76 
77  // Determine total size needed.
78  int size = 0;
79  for (int i = 0; format[i] != '\0'; i++) {
80  if (format[i] == '$') {
81  if (ascii_isdigit(format[i+1])) {
82  int index = format[i+1] - '0';
83  if (args_array[index]->size() == -1) {
84  GOOGLE_LOG(DFATAL)
85  << "strings::Substitute format string invalid: asked for \"$"
86  << index << "\", but only " << CountSubstituteArgs(args_array)
87  << " args were given. Full format string was: \""
88  << CEscape(format) << "\".";
89  return;
90  }
91  size += args_array[index]->size();
92  ++i; // Skip next char.
93  } else if (format[i+1] == '$') {
94  ++size;
95  ++i; // Skip next char.
96  } else {
97  GOOGLE_LOG(DFATAL)
98  << "Invalid strings::Substitute() format string: \""
99  << CEscape(format) << "\".";
100  return;
101  }
102  } else {
103  ++size;
104  }
105  }
106 
107  if (size == 0) return;
108 
109  // Build the string.
110  int original_size = output->size();
111  STLStringResizeUninitialized(output, original_size + size);
112  char* target = string_as_array(output) + original_size;
113  for (int i = 0; format[i] != '\0'; i++) {
114  if (format[i] == '$') {
115  if (ascii_isdigit(format[i+1])) {
116  unsigned int index = format[i+1] - '0';
117  assert(index < 10);
118  const SubstituteArg* src = args_array[index];
119  memcpy(target, src->data(), src->size());
120  target += src->size();
121  ++i; // Skip next char.
122  } else if (format[i+1] == '$') {
123  *target++ = '$';
124  ++i; // Skip next char.
125  }
126  } else {
127  *target++ = format[i];
128  }
129  }
130 
131  GOOGLE_DCHECK_EQ(target - output->data(), output->size());
132 }
133 
134 } // namespace strings
135 } // namespace protobuf
136 } // namespace google
_gevent_test_main.result
result
Definition: _gevent_test_main.py:96
http2_test_server.format
format
Definition: http2_test_server.py:118
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
google::protobuf::strings::internal::SubstituteArg
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/substitute.h:89
google::protobuf::CEscape
string CEscape(const string &src)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/strutil.cc:615
google::protobuf
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:12
google::protobuf::strings::internal::SubstituteArg::size
int size() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/substitute.h:132
memcpy
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
google::protobuf::strings::CountSubstituteArgs
static int CountSubstituteArgs(const SubstituteArg *const *args_array)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/substitute.cc:47
gmock_output_test.output
output
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
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: bloaty/third_party/protobuf/src/google/protobuf/stubs/substitute.cc:55
google::protobuf::string_as_array
char * string_as_array(string *str)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/stl_util.h:63
count
int * count
Definition: bloaty/third_party/googletest/googlemock/test/gmock_stress_test.cc:96
index
int index
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:1184
google::protobuf::ascii_isdigit
bool ascii_isdigit(char c)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/strutil.h:73
GOOGLE_DCHECK_EQ
#define GOOGLE_DCHECK_EQ
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/logging.h:196
google::protobuf::strings::internal::SubstituteArg::data
const char * data() const
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/substitute.h:131
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
GOOGLE_LOG
#define GOOGLE_LOG(LEVEL)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/logging.h:146
setup.target
target
Definition: third_party/bloaty/third_party/protobuf/python/setup.py:179
google::protobuf::STLStringResizeUninitialized
void STLStringResizeUninitialized(string *s, size_t new_size)
Definition: bloaty/third_party/protobuf/src/google/protobuf/stubs/stl_util.h:47
google
Definition: bloaty/third_party/protobuf/benchmarks/util/data_proto2_to_proto3_util.h:11
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
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: bloaty/third_party/protobuf/src/google/protobuf/stubs/substitute.cc:68


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:23