substitute.cc 5.8 KB

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