blob: 093954b3619f817114af15bff0b6ef2d6f4a0543 [file] [log] [blame]
Tommifef05002018-02-27 13:51:08 +01001/*
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#ifndef RTC_BASE_STRINGS_STRING_BUILDER_H_
12#define RTC_BASE_STRINGS_STRING_BUILDER_H_
13
14#include <cstdio>
Karl Wiberg881f1682018-03-08 15:03:23 +010015#include <cstring>
Tommifef05002018-02-27 13:51:08 +010016#include <string>
17
Karl Wiberg881f1682018-03-08 15:03:23 +010018#include "api/array_view.h"
Tommifef05002018-02-27 13:51:08 +010019#include "rtc_base/checks.h"
Karl Wiberg881f1682018-03-08 15:03:23 +010020#include "rtc_base/numerics/safe_minmax.h"
Tommifef05002018-02-27 13:51:08 +010021#include "rtc_base/stringutils.h"
22
23namespace rtc {
24
Karl Wiberg881f1682018-03-08 15:03:23 +010025// This is a minimalistic string builder class meant to cover the most cases of
26// when you might otherwise be tempted to use a stringstream (discouraged for
27// anything except logging). It uses a fixed-size buffer provided by the caller
28// and concatenates strings and numbers into it, allowing the results to be
29// read via |str()|.
Tommifef05002018-02-27 13:51:08 +010030class SimpleStringBuilder {
31 public:
Karl Wiberg881f1682018-03-08 15:03:23 +010032 explicit SimpleStringBuilder(rtc::ArrayView<char> buffer);
Tommifef05002018-02-27 13:51:08 +010033 SimpleStringBuilder(const SimpleStringBuilder&) = delete;
34 SimpleStringBuilder& operator=(const SimpleStringBuilder&) = delete;
35
Karl Wibergabff7dd2018-03-09 15:26:24 +010036 SimpleStringBuilder& operator<<(const char* str);
37 SimpleStringBuilder& operator<<(char ch);
38 SimpleStringBuilder& operator<<(const std::string& str);
39 SimpleStringBuilder& operator<<(int i);
40 SimpleStringBuilder& operator<<(unsigned i);
41 SimpleStringBuilder& operator<<(long i); // NOLINT
42 SimpleStringBuilder& operator<<(long long i); // NOLINT
43 SimpleStringBuilder& operator<<(unsigned long i); // NOLINT
44 SimpleStringBuilder& operator<<(unsigned long long i); // NOLINT
45 SimpleStringBuilder& operator<<(float f);
46 SimpleStringBuilder& operator<<(double f);
47 SimpleStringBuilder& operator<<(long double f);
Tommifef05002018-02-27 13:51:08 +010048
49 // Returns a pointer to the built string. The name |str()| is borrowed for
50 // compatibility reasons as we replace usage of stringstream throughout the
51 // code base.
Karl Wiberg881f1682018-03-08 15:03:23 +010052 const char* str() const { return buffer_.data(); }
Tommifef05002018-02-27 13:51:08 +010053
54 // Returns the length of the string. The name |size()| is picked for STL
55 // compatibility reasons.
56 size_t size() const { return size_; }
57
Yves Gerey665174f2018-06-19 15:03:05 +020058// Allows appending a printf style formatted string.
Karl Wiberg881f1682018-03-08 15:03:23 +010059#if defined(__GNUC__)
60 __attribute__((__format__(__printf__, 2, 3)))
61#endif
62 SimpleStringBuilder&
Karl Wibergabff7dd2018-03-09 15:26:24 +010063 AppendFormat(const char* fmt, ...);
Tommifef05002018-02-27 13:51:08 +010064
65 // An alternate way from operator<<() to append a string. This variant is
66 // slightly more efficient when the length of the string to append, is known.
Karl Wibergabff7dd2018-03-09 15:26:24 +010067 SimpleStringBuilder& Append(const char* str, size_t length = SIZE_UNKNOWN);
Tommifef05002018-02-27 13:51:08 +010068
69 private:
Karl Wiberg881f1682018-03-08 15:03:23 +010070 bool IsConsistent() const {
71 return size_ <= buffer_.size() - 1 && buffer_[size_] == '\0';
Tommifef05002018-02-27 13:51:08 +010072 }
73
Karl Wiberg881f1682018-03-08 15:03:23 +010074 // An always-zero-terminated fixed-size buffer that we write to. The fixed
75 // size allows the buffer to be stack allocated, which helps performance.
Tommifef05002018-02-27 13:51:08 +010076 // Having a fixed size is furthermore useful to avoid unnecessary resizing
77 // while building it.
Karl Wiberg881f1682018-03-08 15:03:23 +010078 const rtc::ArrayView<char> buffer_;
Tommifef05002018-02-27 13:51:08 +010079
80 // Represents the number of characters written to the buffer.
81 // This does not include the terminating '\0'.
82 size_t size_ = 0;
83};
84
85} // namespace rtc
86
87#endif // RTC_BASE_STRINGS_STRING_BUILDER_H_