1//===-- A simple implementation of string stream class ----------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_LIBC_SRC___SUPPORT_CPP_STRINGSTREAM_H
10#define LLVM_LIBC_SRC___SUPPORT_CPP_STRINGSTREAM_H
11
12#include "span.h"
13#include "string_view.h"
14#include "type_traits.h"
15
16#include "src/__support/integer_to_string.h"
17
18namespace LIBC_NAMESPACE {
19namespace cpp {
20
21// This class is to be used to write simple strings into a user provided buffer
22// without any dynamic memory allocation. There is no requirement to mimic the
23// C++ standard library class std::stringstream.
24class StringStream {
25 span<char> data;
26 size_t write_ptr = 0; // The current write pointer
27 bool err = false; // If an error occurs while writing
28
29 void write(const char *bytes, size_t size) {
30 size_t i = 0;
31 const size_t data_size = data.size();
32 for (; write_ptr < data_size && i < size; ++i, ++write_ptr)
33 data[write_ptr] = bytes[i];
34 if (i < size) {
35 // If some of the characters couldn't be written, set error.
36 err = true;
37 }
38 }
39
40public:
41 static constexpr char ENDS = '\0';
42
43 // Create a string stream which will write into |buf|.
44 constexpr StringStream(const span<char> &buf) : data(buf) {}
45
46 // Return a string_view to the current characters in the stream. If a
47 // null terminator was not explicitly written, then the return value
48 // will not include one. In order to produce a string_view to a null
49 // terminated string, write ENDS explicitly.
50 string_view str() const { return string_view(data.data(), write_ptr); }
51
52 // Write the characters from |str| to the stream.
53 StringStream &operator<<(string_view str) {
54 write(bytes: str.data(), size: str.size());
55 return *this;
56 }
57
58 // Write the |val| as string.
59 template <typename T, enable_if_t<is_integral_v<T>, int> = 0>
60 StringStream &operator<<(T val) {
61 const IntegerToString<T> buffer(val);
62 return *this << buffer.view();
63 }
64
65 template <typename T, enable_if_t<is_floating_point_v<T>, int> = 0>
66 StringStream &operator<<(T) {
67 // If this specialization gets activated, then the static_assert will
68 // trigger a compile error about missing floating point number support.
69 static_assert(!is_floating_point_v<T>,
70 "Writing floating point numbers is not yet supported");
71 return *this;
72 }
73
74 // Write a null-terminated string. The terminating null character is not
75 // written to allow stremaing to continue.
76 StringStream &operator<<(const char *str) {
77 return operator<<(str: string_view(str));
78 }
79
80 // Write a single character.
81 StringStream &operator<<(char a) {
82 write(bytes: &a, size: 1);
83 return *this;
84 }
85
86 // Return true if any write operation(s) failed due to insufficient size.
87 bool overflow() const { return err; }
88
89 size_t bufsize() const { return data.size(); }
90};
91
92} // namespace cpp
93} // namespace LIBC_NAMESPACE
94
95#endif // LLVM_LIBC_SRC___SUPPORT_CPP_STRINGSTREAM_H
96

source code of libc/src/__support/CPP/stringstream.h