You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

141 lines
4.2 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * Copyright 2018 The WebRTC Project Authors. All rights reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "rtc_base/strings/string_builder.h"
  11. #include <stdarg.h>
  12. #include <cstdio>
  13. #include <cstring>
  14. #include "rtc_base/checks.h"
  15. #include "rtc_base/numerics/safe_minmax.h"
  16. namespace rtc {
  17. SimpleStringBuilder::SimpleStringBuilder(rtc::ArrayView<char> buffer)
  18. : buffer_(buffer) {
  19. buffer_[0] = '\0';
  20. RTC_DCHECK(IsConsistent());
  21. }
  22. SimpleStringBuilder& SimpleStringBuilder::operator<<(const char* str) {
  23. return Append(str, strlen(str));
  24. }
  25. SimpleStringBuilder& SimpleStringBuilder::operator<<(char ch) {
  26. return Append(&ch, 1);
  27. }
  28. SimpleStringBuilder& SimpleStringBuilder::operator<<(const std::string& str) {
  29. return Append(str.c_str(), str.length());
  30. }
  31. // Numeric conversion routines.
  32. //
  33. // We use std::[v]snprintf instead of std::to_string because:
  34. // * std::to_string relies on the current locale for formatting purposes,
  35. // and therefore concurrent calls to std::to_string from multiple threads
  36. // may result in partial serialization of calls
  37. // * snprintf allows us to print the number directly into our buffer.
  38. // * avoid allocating a std::string (potential heap alloc).
  39. // TODO(tommi): Switch to std::to_chars in C++17.
  40. SimpleStringBuilder& SimpleStringBuilder::operator<<(int i) {
  41. return AppendFormat("%d", i);
  42. }
  43. SimpleStringBuilder& SimpleStringBuilder::operator<<(unsigned i) {
  44. return AppendFormat("%u", i);
  45. }
  46. SimpleStringBuilder& SimpleStringBuilder::operator<<(long i) { // NOLINT
  47. return AppendFormat("%ld", i);
  48. }
  49. SimpleStringBuilder& SimpleStringBuilder::operator<<(long long i) { // NOLINT
  50. return AppendFormat("%lld", i);
  51. }
  52. SimpleStringBuilder& SimpleStringBuilder::operator<<(
  53. unsigned long i) { // NOLINT
  54. return AppendFormat("%lu", i);
  55. }
  56. SimpleStringBuilder& SimpleStringBuilder::operator<<(
  57. unsigned long long i) { // NOLINT
  58. return AppendFormat("%llu", i);
  59. }
  60. SimpleStringBuilder& SimpleStringBuilder::operator<<(float f) {
  61. return AppendFormat("%g", f);
  62. }
  63. SimpleStringBuilder& SimpleStringBuilder::operator<<(double f) {
  64. return AppendFormat("%g", f);
  65. }
  66. SimpleStringBuilder& SimpleStringBuilder::operator<<(long double f) {
  67. return AppendFormat("%Lg", f);
  68. }
  69. SimpleStringBuilder& SimpleStringBuilder::AppendFormat(const char* fmt, ...) {
  70. va_list args;
  71. va_start(args, fmt);
  72. const int len =
  73. std::vsnprintf(&buffer_[size_], buffer_.size() - size_, fmt, args);
  74. if (len >= 0) {
  75. const size_t chars_added = rtc::SafeMin(len, buffer_.size() - 1 - size_);
  76. size_ += chars_added;
  77. RTC_DCHECK_EQ(len, chars_added) << "Buffer size was insufficient";
  78. } else {
  79. // This should never happen, but we're paranoid, so re-write the
  80. // terminator in case vsnprintf() overwrote it.
  81. RTC_NOTREACHED();
  82. buffer_[size_] = '\0';
  83. }
  84. va_end(args);
  85. RTC_DCHECK(IsConsistent());
  86. return *this;
  87. }
  88. SimpleStringBuilder& SimpleStringBuilder::Append(const char* str,
  89. size_t length) {
  90. RTC_DCHECK_LT(size_ + length, buffer_.size())
  91. << "Buffer size was insufficient";
  92. const size_t chars_added = rtc::SafeMin(length, buffer_.size() - size_ - 1);
  93. memcpy(&buffer_[size_], str, chars_added);
  94. size_ += chars_added;
  95. buffer_[size_] = '\0';
  96. RTC_DCHECK(IsConsistent());
  97. return *this;
  98. }
  99. StringBuilder& StringBuilder::AppendFormat(const char* fmt, ...) {
  100. va_list args, copy;
  101. va_start(args, fmt);
  102. va_copy(copy, args);
  103. const int predicted_length = std::vsnprintf(nullptr, 0, fmt, copy);
  104. va_end(copy);
  105. RTC_DCHECK_GE(predicted_length, 0);
  106. if (predicted_length > 0) {
  107. const size_t size = str_.size();
  108. str_.resize(size + predicted_length);
  109. // Pass "+ 1" to vsnprintf to include space for the '\0'.
  110. const int actual_length =
  111. std::vsnprintf(&str_[size], predicted_length + 1, fmt, args);
  112. RTC_DCHECK_GE(actual_length, 0);
  113. }
  114. va_end(args);
  115. return *this;
  116. }
  117. } // namespace rtc