1 //===-- A simple implementation of string stream class ----------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 #ifndef LLVM_LIBC_SRC_SUPPORT_CPP_STRINGSTREAM_H
10 #define LLVM_LIBC_SRC_SUPPORT_CPP_STRINGSTREAM_H
12 #include "string_view.h"
14 #include "type_traits.h"
16 #include "src/__support/integer_to_string.h"
18 namespace __llvm_libc
{
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.
26 size_t write_ptr
= 0; // The current write pointer
27 bool err
= false; // If an error occurs while writing
29 void write(const char *bytes
, size_t size
) {
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
];
35 // If some of the characters couldn't be written, set error.
41 static constexpr char ENDS
= '\0';
43 // Create a string stream which will write into |buf|.
44 constexpr StringStream(const span
<char> &buf
) : data(buf
) {}
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
); }
52 // Write the characters from |str| to the stream.
53 StringStream
&operator<<(string_view str
) {
54 write(str
.data(), str
.size());
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 char buffer
[IntegerToString::dec_bufsize
<T
>()];
62 auto int_to_str
= IntegerToString::dec(val
, buffer
);
64 return operator<<(*int_to_str
);
68 template <typename T
, enable_if_t
<is_floating_point_v
<T
>, int> = 0>
69 StringStream
&operator<<(T
) {
70 // If this specialization gets activated, then the static_assert will
71 // trigger a compile error about missing floating point number support.
72 static_assert(!is_floating_point_v
<T
>,
73 "Writing floating point numbers is not yet supported");
77 // Write a null-terminated string. The terminating null character is not
78 // written to allow stremaing to continue.
79 StringStream
&operator<<(const char *str
) {
80 return operator<<(string_view(str
));
83 // Write a single character.
84 StringStream
&operator<<(char a
) {
89 // Return true if any write operation(s) failed due to insufficient size.
90 bool overflow() const { return err
; }
92 size_t bufsize() const { return data
.size(); }
96 } // namespace __llvm_libc
98 #endif // LLVM_LIBC_SRC_SUPPORT_CPP_STRINGSTREAM_H