• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
11 #include "rtc_base/strings/string_builder.h"
12 
13 #include <stdarg.h>
14 
15 #include <cstdio>
16 #include <cstring>
17 
18 #include "rtc_base/checks.h"
19 #include "rtc_base/numerics/safe_minmax.h"
20 
21 namespace rtc {
22 
SimpleStringBuilder(rtc::ArrayView<char> buffer)23 SimpleStringBuilder::SimpleStringBuilder(rtc::ArrayView<char> buffer)
24     : buffer_(buffer) {
25   buffer_[0] = '\0';
26   RTC_DCHECK(IsConsistent());
27 }
28 
operator <<(const char * str)29 SimpleStringBuilder& SimpleStringBuilder::operator<<(const char* str) {
30   return Append(str, strlen(str));
31 }
32 
operator <<(char ch)33 SimpleStringBuilder& SimpleStringBuilder::operator<<(char ch) {
34   return Append(&ch, 1);
35 }
36 
operator <<(const std::string & str)37 SimpleStringBuilder& SimpleStringBuilder::operator<<(const std::string& str) {
38   return Append(str.c_str(), str.length());
39 }
40 
41 // Numeric conversion routines.
42 //
43 // We use std::[v]snprintf instead of std::to_string because:
44 // * std::to_string relies on the current locale for formatting purposes,
45 //   and therefore concurrent calls to std::to_string from multiple threads
46 //   may result in partial serialization of calls
47 // * snprintf allows us to print the number directly into our buffer.
48 // * avoid allocating a std::string (potential heap alloc).
49 // TODO(tommi): Switch to std::to_chars in C++17.
50 
operator <<(int i)51 SimpleStringBuilder& SimpleStringBuilder::operator<<(int i) {
52   return AppendFormat("%d", i);
53 }
54 
operator <<(unsigned i)55 SimpleStringBuilder& SimpleStringBuilder::operator<<(unsigned i) {
56   return AppendFormat("%u", i);
57 }
58 
operator <<(long i)59 SimpleStringBuilder& SimpleStringBuilder::operator<<(long i) {  // NOLINT
60   return AppendFormat("%ld", i);
61 }
62 
operator <<(long long i)63 SimpleStringBuilder& SimpleStringBuilder::operator<<(long long i) {  // NOLINT
64   return AppendFormat("%lld", i);
65 }
66 
operator <<(unsigned long i)67 SimpleStringBuilder& SimpleStringBuilder::operator<<(
68     unsigned long i) {  // NOLINT
69   return AppendFormat("%lu", i);
70 }
71 
operator <<(unsigned long long i)72 SimpleStringBuilder& SimpleStringBuilder::operator<<(
73     unsigned long long i) {  // NOLINT
74   return AppendFormat("%llu", i);
75 }
76 
operator <<(float f)77 SimpleStringBuilder& SimpleStringBuilder::operator<<(float f) {
78   return AppendFormat("%g", f);
79 }
80 
operator <<(double f)81 SimpleStringBuilder& SimpleStringBuilder::operator<<(double f) {
82   return AppendFormat("%g", f);
83 }
84 
operator <<(long double f)85 SimpleStringBuilder& SimpleStringBuilder::operator<<(long double f) {
86   return AppendFormat("%Lg", f);
87 }
88 
AppendFormat(const char * fmt,...)89 SimpleStringBuilder& SimpleStringBuilder::AppendFormat(const char* fmt, ...) {
90   va_list args;
91   va_start(args, fmt);
92   const int len =
93       std::vsnprintf(&buffer_[size_], buffer_.size() - size_, fmt, args);
94   if (len >= 0) {
95     const size_t chars_added = rtc::SafeMin(len, buffer_.size() - 1 - size_);
96     size_ += chars_added;
97     RTC_DCHECK_EQ(len, chars_added) << "Buffer size was insufficient";
98   } else {
99     // This should never happen, but we're paranoid, so re-write the
100     // terminator in case vsnprintf() overwrote it.
101     RTC_NOTREACHED();
102     buffer_[size_] = '\0';
103   }
104   va_end(args);
105   RTC_DCHECK(IsConsistent());
106   return *this;
107 }
108 
Append(const char * str,size_t length)109 SimpleStringBuilder& SimpleStringBuilder::Append(const char* str,
110                                                  size_t length) {
111   RTC_DCHECK_LT(size_ + length, buffer_.size())
112       << "Buffer size was insufficient";
113   const size_t chars_added = rtc::SafeMin(length, buffer_.size() - size_ - 1);
114   memcpy(&buffer_[size_], str, chars_added);
115   size_ += chars_added;
116   buffer_[size_] = '\0';
117   RTC_DCHECK(IsConsistent());
118   return *this;
119 }
120 
AppendFormat(const char * fmt,...)121 StringBuilder& StringBuilder::AppendFormat(const char* fmt, ...) {
122   va_list args, copy;
123   va_start(args, fmt);
124   va_copy(copy, args);
125   const int predicted_length = std::vsnprintf(nullptr, 0, fmt, copy);
126   va_end(copy);
127 
128   RTC_DCHECK_GE(predicted_length, 0);
129   if (predicted_length > 0) {
130     const size_t size = str_.size();
131     str_.resize(size + predicted_length);
132     // Pass "+ 1" to vsnprintf to include space for the '\0'.
133     const int actual_length =
134         std::vsnprintf(&str_[size], predicted_length + 1, fmt, args);
135     RTC_DCHECK_GE(actual_length, 0);
136   }
137   va_end(args);
138   return *this;
139 }
140 
141 }  // namespace rtc
142