substitute.cc 5.6 KB

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