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