substitute.cc 5.3 KB

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