Path: blob/main/contrib/llvm-project/libc/src/__support/CPP/stringstream.h
213799 views
//===-- A simple implementation of string stream class ----------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLVM_LIBC_SRC___SUPPORT_CPP_STRINGSTREAM_H9#define LLVM_LIBC_SRC___SUPPORT_CPP_STRINGSTREAM_H1011#include "span.h"12#include "src/__support/macros/config.h"13#include "string_view.h"14#include "type_traits.h"1516#include "src/__support/integer_to_string.h"1718namespace LIBC_NAMESPACE_DECL {19namespace cpp {2021// This class is to be used to write simple strings into a user provided buffer22// without any dynamic memory allocation. There is no requirement to mimic the23// C++ standard library class std::stringstream.24class StringStream {25span<char> data;26size_t write_ptr = 0; // The current write pointer27bool err = false; // If an error occurs while writing2829void write(const char *bytes, size_t size) {30size_t i = 0;31const size_t data_size = data.size();32for (; write_ptr < data_size && i < size; ++i, ++write_ptr)33data[write_ptr] = bytes[i];34if (i < size) {35// If some of the characters couldn't be written, set error.36err = true;37}38}3940public:41static constexpr char ENDS = '\0';4243// Create a string stream which will write into |buf|.44constexpr StringStream(const span<char> &buf) : data(buf) {}4546// Return a string_view to the current characters in the stream. If a47// null terminator was not explicitly written, then the return value48// will not include one. In order to produce a string_view to a null49// terminated string, write ENDS explicitly.50string_view str() const { return string_view(data.data(), write_ptr); }5152// Write the characters from |str| to the stream.53StringStream &operator<<(string_view str) {54write(str.data(), str.size());55return *this;56}5758// Write the |val| as string.59template <typename T, enable_if_t<is_integral_v<T>, int> = 0>60StringStream &operator<<(T val) {61const IntegerToString<T> buffer(val);62return *this << buffer.view();63}6465template <typename T, enable_if_t<is_floating_point_v<T>, int> = 0>66StringStream &operator<<(T) {67// If this specialization gets activated, then the static_assert will68// trigger a compile error about missing floating point number support.69static_assert(!is_floating_point_v<T>,70"Writing floating point numbers is not yet supported");71return *this;72}7374// Write a null-terminated string. The terminating null character is not75// written to allow stremaing to continue.76StringStream &operator<<(const char *str) {77return operator<<(string_view(str));78}7980// Write a single character.81StringStream &operator<<(char a) {82write(&a, 1);83return *this;84}8586// Return true if any write operation(s) failed due to insufficient size.87bool overflow() const { return err; }8889size_t bufsize() const { return data.size(); }90};9192} // namespace cpp93} // namespace LIBC_NAMESPACE_DECL9495#endif // LLVM_LIBC_SRC___SUPPORT_CPP_STRINGSTREAM_H969798