bloaty/third_party/abseil-cpp/absl/strings/substitute.cc
Go to the documentation of this file.
1 // Copyright 2017 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "absl/strings/substitute.h"
16 
17 #include <algorithm>
18 
19 #include "absl/base/internal/raw_logging.h"
20 #include "absl/strings/ascii.h"
21 #include "absl/strings/escaping.h"
22 #include "absl/strings/internal/resize_uninitialized.h"
23 #include "absl/strings/string_view.h"
24 
25 namespace absl {
27 namespace substitute_internal {
28 
30  const absl::string_view* args_array,
31  size_t num_args) {
32  // Determine total size needed.
33  size_t size = 0;
34  for (size_t i = 0; i < format.size(); i++) {
35  if (format[i] == '$') {
36  if (i + 1 >= format.size()) {
37 #ifndef NDEBUG
39  "Invalid absl::Substitute() format string: \"%s\".",
41 #endif
42  return;
43  } else if (absl::ascii_isdigit(format[i + 1])) {
44  int index = format[i + 1] - '0';
45  if (static_cast<size_t>(index) >= num_args) {
46 #ifndef NDEBUG
48  FATAL,
49  "Invalid absl::Substitute() format string: asked for \"$"
50  "%d\", but only %d args were given. Full format string was: "
51  "\"%s\".",
52  index, static_cast<int>(num_args), absl::CEscape(format).c_str());
53 #endif
54  return;
55  }
56  size += args_array[index].size();
57  ++i; // Skip next char.
58  } else if (format[i + 1] == '$') {
59  ++size;
60  ++i; // Skip next char.
61  } else {
62 #ifndef NDEBUG
64  "Invalid absl::Substitute() format string: \"%s\".",
66 #endif
67  return;
68  }
69  } else {
70  ++size;
71  }
72  }
73 
74  if (size == 0) return;
75 
76  // Build the string.
77  size_t original_size = output->size();
79  char* target = &(*output)[original_size];
80  for (size_t i = 0; i < format.size(); i++) {
81  if (format[i] == '$') {
82  if (absl::ascii_isdigit(format[i + 1])) {
83  const absl::string_view src = args_array[format[i + 1] - '0'];
84  target = std::copy(src.begin(), src.end(), target);
85  ++i; // Skip next char.
86  } else if (format[i + 1] == '$') {
87  *target++ = '$';
88  ++i; // Skip next char.
89  }
90  } else {
91  *target++ = format[i];
92  }
93  }
94 
95  assert(target == output->data() + output->size());
96 }
97 
98 Arg::Arg(const void* value) {
99  static_assert(sizeof(scratch_) >= sizeof(value) * 2 + 2,
100  "fix sizeof(scratch_)");
101  if (value == nullptr) {
102  piece_ = "NULL";
103  } else {
104  char* ptr = scratch_ + sizeof(scratch_);
105  uintptr_t num = reinterpret_cast<uintptr_t>(value);
106  do {
108  num >>= 4;
109  } while (num != 0);
110  *--ptr = 'x';
111  *--ptr = '0';
113  }
114 }
115 
116 // TODO(jorg): Don't duplicate so much code between here and str_cat.cc
117 Arg::Arg(Hex hex) {
119  char* writer = end;
120  uint64_t value = hex.value;
121  do {
123  value >>= 4;
124  } while (value != 0);
125 
126  char* beg;
127  if (end - writer < hex.width) {
128  beg = end - hex.width;
129  std::fill_n(beg, writer - beg, hex.fill);
130  } else {
131  beg = writer;
132  }
133 
134  piece_ = absl::string_view(beg, end - beg);
135 }
136 
137 // TODO(jorg): Don't duplicate so much code between here and str_cat.cc
138 Arg::Arg(Dec dec) {
139  assert(dec.width <= numbers_internal::kFastToBufferSize);
141  char* const minfill = end - dec.width;
142  char* writer = end;
143  uint64_t value = dec.value;
144  bool neg = dec.neg;
145  while (value > 9) {
146  *--writer = '0' + (value % 10);
147  value /= 10;
148  }
149  *--writer = '0' + value;
150  if (neg) *--writer = '-';
151 
152  ptrdiff_t fillers = writer - minfill;
153  if (fillers > 0) {
154  // Tricky: if the fill character is ' ', then it's <fill><+/-><digits>
155  // But...: if the fill character is '0', then it's <+/-><fill><digits>
156  bool add_sign_again = false;
157  if (neg && dec.fill == '0') { // If filling with '0',
158  ++writer; // ignore the sign we just added
159  add_sign_again = true; // and re-add the sign later.
160  }
161  writer -= fillers;
162  std::fill_n(writer, fillers, dec.fill);
163  if (add_sign_again) *--writer = '-';
164  }
165 
167 }
168 
169 } // namespace substitute_internal
171 } // namespace absl
ptr
char * ptr
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:45
http2_test_server.format
format
Definition: http2_test_server.py:118
copy
static int copy(grpc_slice_buffer *input, grpc_slice_buffer *output)
Definition: message_compress.cc:145
absl::string_view
Definition: abseil-cpp/absl/strings/string_view.h:167
testing::internal::string
::std::string string
Definition: bloaty/third_party/protobuf/third_party/googletest/googletest/include/gtest/internal/gtest-port.h:881
absl::CEscape
std::string CEscape(absl::string_view src)
Definition: abseil-cpp/absl/strings/escaping.cc:854
ABSL_NAMESPACE_END
#define ABSL_NAMESPACE_END
Definition: third_party/abseil-cpp/absl/base/config.h:171
absl::substitute_internal::Arg::Arg
Arg(const char *value)
Definition: abseil-cpp/absl/strings/substitute.h:105
ABSL_NAMESPACE_BEGIN
#define ABSL_NAMESPACE_BEGIN
Definition: third_party/abseil-cpp/absl/base/config.h:170
end
char * end
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1008
absl::string_view::size
constexpr size_type size() const noexcept
Definition: abseil-cpp/absl/strings/string_view.h:277
gen_stats_data.c_str
def c_str(s, encoding='ascii')
Definition: gen_stats_data.py:38
gmock_output_test.output
output
Definition: bloaty/third_party/googletest/googlemock/test/gmock_output_test.py:175
absl::substitute_internal::Arg::piece_
absl::string_view piece_
Definition: abseil-cpp/absl/strings/substitute.h:191
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
absl::string_view::begin
constexpr const_iterator begin() const noexcept
Definition: abseil-cpp/absl/strings/string_view.h:220
absl::substitute_internal::SubstituteAndAppendArray
void SubstituteAndAppendArray(std::string *output, absl::string_view format, const absl::string_view *args_array, size_t num_args)
Definition: abseil-cpp/absl/strings/substitute.cc:29
absl::substitute_internal::Arg::scratch_
char scratch_[numbers_internal::kFastToBufferSize]
Definition: abseil-cpp/absl/strings/substitute.h:192
uintptr_t
_W64 unsigned int uintptr_t
Definition: stdint-msvc2008.h:119
writer
void writer(void *n)
Definition: libuv/docs/code/locks/main.c:22
value
const char * value
Definition: hpack_parser_table.cc:165
FATAL
#define FATAL(msg)
Definition: task.h:88
absl::chars_format::hex
@ hex
absl::strings_internal::STLStringResizeUninitialized
void STLStringResizeUninitialized(string_type *s, size_t new_size)
Definition: abseil-cpp/absl/strings/internal/resize_uninitialized.h:67
index
int index
Definition: bloaty/third_party/protobuf/php/ext/google/protobuf/protobuf.h:1184
string_view
absl::string_view string_view
Definition: attr.cc:22
xds_manager.num
num
Definition: xds_manager.py:56
absl::numbers_internal::kHexChar
ABSL_CONST_INIT const ABSL_DLL char kHexChar[]
Definition: abseil-cpp/absl/strings/numbers.cc:1027
absl::numbers_internal::kFastToBufferSize
static const int kFastToBufferSize
Definition: abseil-cpp/absl/strings/numbers.h:153
absl
Definition: abseil-cpp/absl/algorithm/algorithm.h:31
absl::ascii_isdigit
bool ascii_isdigit(unsigned char c)
Definition: abseil-cpp/absl/strings/ascii.h:132
size
voidpf void uLong size
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
absl::string_view::end
constexpr const_iterator end() const noexcept
Definition: abseil-cpp/absl/strings/string_view.h:227
ABSL_RAW_LOG
#define ABSL_RAW_LOG(severity,...)
Definition: abseil-cpp/absl/base/internal/raw_logging.h:44
setup.target
target
Definition: third_party/bloaty/third_party/protobuf/python/setup.py:179
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230


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