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 #include <cassert>
19 #include <cstddef>
20 #include <cstdint>
21 #include <string>
22
23 #include "absl/base/config.h"
24 #include "absl/base/internal/raw_logging.h"
25 #include "absl/base/nullability.h"
26 #include "absl/strings/ascii.h"
27 #include "absl/strings/escaping.h"
28 #include "absl/strings/internal/resize_uninitialized.h"
29 #include "absl/strings/numbers.h"
30 #include "absl/strings/str_cat.h"
31 #include "absl/strings/string_view.h"
32
33 namespace absl {
34 ABSL_NAMESPACE_BEGIN
35 namespace substitute_internal {
36
SubstituteAndAppendArray(absl::Nonnull<std::string * > output,absl::string_view format,absl::Nullable<const absl::string_view * > args_array,size_t num_args)37 void SubstituteAndAppendArray(
38 absl::Nonnull<std::string*> output, absl::string_view format,
39 absl::Nullable<const absl::string_view*> args_array, size_t num_args) {
40 // Determine total size needed.
41 size_t size = 0;
42 for (size_t i = 0; i < format.size(); i++) {
43 if (format[i] == '$') {
44 if (i + 1 >= format.size()) {
45 #ifndef NDEBUG
46 ABSL_RAW_LOG(FATAL,
47 "Invalid absl::Substitute() format string: \"%s\".",
48 absl::CEscape(format).c_str());
49 #endif
50 return;
51 } else if (absl::ascii_isdigit(
52 static_cast<unsigned char>(format[i + 1]))) {
53 int index = format[i + 1] - '0';
54 if (static_cast<size_t>(index) >= num_args) {
55 #ifndef NDEBUG
56 ABSL_RAW_LOG(
57 FATAL,
58 "Invalid absl::Substitute() format string: asked for \"$"
59 "%d\", but only %d args were given. Full format string was: "
60 "\"%s\".",
61 index, static_cast<int>(num_args), absl::CEscape(format).c_str());
62 #endif
63 return;
64 }
65 size += args_array[index].size();
66 ++i; // Skip next char.
67 } else if (format[i + 1] == '$') {
68 ++size;
69 ++i; // Skip next char.
70 } else {
71 #ifndef NDEBUG
72 ABSL_RAW_LOG(FATAL,
73 "Invalid absl::Substitute() format string: \"%s\".",
74 absl::CEscape(format).c_str());
75 #endif
76 return;
77 }
78 } else {
79 ++size;
80 }
81 }
82
83 if (size == 0) return;
84
85 // Build the string.
86 size_t original_size = output->size();
87 strings_internal::STLStringResizeUninitializedAmortized(output,
88 original_size + size);
89 char* target = &(*output)[original_size];
90 for (size_t i = 0; i < format.size(); i++) {
91 if (format[i] == '$') {
92 if (absl::ascii_isdigit(static_cast<unsigned char>(format[i + 1]))) {
93 const absl::string_view src = args_array[format[i + 1] - '0'];
94 target = std::copy(src.begin(), src.end(), target);
95 ++i; // Skip next char.
96 } else if (format[i + 1] == '$') {
97 *target++ = '$';
98 ++i; // Skip next char.
99 }
100 } else {
101 *target++ = format[i];
102 }
103 }
104
105 assert(target == output->data() + output->size());
106 }
107
Arg(absl::Nullable<const void * > value)108 Arg::Arg(absl::Nullable<const void*> value) {
109 static_assert(sizeof(scratch_) >= sizeof(value) * 2 + 2,
110 "fix sizeof(scratch_)");
111 if (value == nullptr) {
112 piece_ = "NULL";
113 } else {
114 char* ptr = scratch_ + sizeof(scratch_);
115 uintptr_t num = reinterpret_cast<uintptr_t>(value);
116 do {
117 *--ptr = absl::numbers_internal::kHexChar[num & 0xf];
118 num >>= 4;
119 } while (num != 0);
120 *--ptr = 'x';
121 *--ptr = '0';
122 piece_ = absl::string_view(
123 ptr, static_cast<size_t>(scratch_ + sizeof(scratch_) - ptr));
124 }
125 }
126
127 // TODO(jorg): Don't duplicate so much code between here and str_cat.cc
Arg(Hex hex)128 Arg::Arg(Hex hex) {
129 char* const end = &scratch_[numbers_internal::kFastToBufferSize];
130 char* writer = end;
131 uint64_t value = hex.value;
132 do {
133 *--writer = absl::numbers_internal::kHexChar[value & 0xF];
134 value >>= 4;
135 } while (value != 0);
136
137 char* beg;
138 if (end - writer < hex.width) {
139 beg = end - hex.width;
140 std::fill_n(beg, writer - beg, hex.fill);
141 } else {
142 beg = writer;
143 }
144
145 piece_ = absl::string_view(beg, static_cast<size_t>(end - beg));
146 }
147
148 // TODO(jorg): Don't duplicate so much code between here and str_cat.cc
Arg(Dec dec)149 Arg::Arg(Dec dec) {
150 assert(dec.width <= numbers_internal::kFastToBufferSize);
151 char* const end = &scratch_[numbers_internal::kFastToBufferSize];
152 char* const minfill = end - dec.width;
153 char* writer = end;
154 uint64_t value = dec.value;
155 bool neg = dec.neg;
156 while (value > 9) {
157 *--writer = '0' + (value % 10);
158 value /= 10;
159 }
160 *--writer = '0' + static_cast<char>(value);
161 if (neg) *--writer = '-';
162
163 ptrdiff_t fillers = writer - minfill;
164 if (fillers > 0) {
165 // Tricky: if the fill character is ' ', then it's <fill><+/-><digits>
166 // But...: if the fill character is '0', then it's <+/-><fill><digits>
167 bool add_sign_again = false;
168 if (neg && dec.fill == '0') { // If filling with '0',
169 ++writer; // ignore the sign we just added
170 add_sign_again = true; // and re-add the sign later.
171 }
172 writer -= fillers;
173 std::fill_n(writer, fillers, dec.fill);
174 if (add_sign_again) *--writer = '-';
175 }
176
177 piece_ = absl::string_view(writer, static_cast<size_t>(end - writer));
178 }
179
180 } // namespace substitute_internal
181 ABSL_NAMESPACE_END
182 } // namespace absl
183