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