substitute.cc 5.5 KB

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