xref: /src/contrib/llvm-project/lldb/source/Utility/StreamString.cpp (revision 297eecfb02bb25902531dbb5c3b9a88caf8adf29)
1cfca06d7SDimitry Andric //===-- StreamString.cpp --------------------------------------------------===//
2f034231aSEd Maste //
35f29bb8aSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
45f29bb8aSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
55f29bb8aSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6f034231aSEd Maste //
7f034231aSEd Maste //===----------------------------------------------------------------------===//
8f034231aSEd Maste 
974a628f7SDimitry Andric #include "lldb/Utility/StreamString.h"
10f034231aSEd Maste 
11f034231aSEd Maste using namespace lldb;
12f034231aSEd Maste using namespace lldb_private;
13f034231aSEd Maste 
StreamString(bool colors)14950076cdSDimitry Andric StreamString::StreamString(bool colors) : Stream(0, 4, eByteOrderBig, colors) {}
15f034231aSEd Maste 
StreamString(uint32_t flags,uint32_t addr_size,ByteOrder byte_order)1614f1b3e8SDimitry Andric StreamString::StreamString(uint32_t flags, uint32_t addr_size,
1714f1b3e8SDimitry Andric                            ByteOrder byte_order)
1814f1b3e8SDimitry Andric     : Stream(flags, addr_size, byte_order), m_packet() {}
19f034231aSEd Maste 
20344a3780SDimitry Andric StreamString::~StreamString() = default;
21f034231aSEd Maste 
Flush()2214f1b3e8SDimitry Andric void StreamString::Flush() {
23f034231aSEd Maste   // Nothing to do when flushing a buffer based stream...
24f034231aSEd Maste }
25f034231aSEd Maste 
WriteImpl(const void * s,size_t length)2694994d37SDimitry Andric size_t StreamString::WriteImpl(const void *s, size_t length) {
27706b4fc4SDimitry Andric   m_packet.append(static_cast<const char *>(s), length);
28f034231aSEd Maste   return length;
29f034231aSEd Maste }
30f034231aSEd Maste 
Clear()3194994d37SDimitry Andric void StreamString::Clear() {
3294994d37SDimitry Andric   m_packet.clear();
3394994d37SDimitry Andric   m_bytes_written = 0;
3494994d37SDimitry Andric }
35f034231aSEd Maste 
Empty() const3614f1b3e8SDimitry Andric bool StreamString::Empty() const { return GetSize() == 0; }
37f034231aSEd Maste 
GetSize() const3814f1b3e8SDimitry Andric size_t StreamString::GetSize() const { return m_packet.size(); }
39f034231aSEd Maste 
GetSizeOfLastLine() const4014f1b3e8SDimitry Andric size_t StreamString::GetSizeOfLastLine() const {
41205afe67SEd Maste   const size_t length = m_packet.size();
42205afe67SEd Maste   size_t last_line_begin_pos = m_packet.find_last_of("\r\n");
4314f1b3e8SDimitry Andric   if (last_line_begin_pos == std::string::npos) {
44205afe67SEd Maste     return length;
4514f1b3e8SDimitry Andric   } else {
46205afe67SEd Maste     ++last_line_begin_pos;
47205afe67SEd Maste     return length - last_line_begin_pos;
48205afe67SEd Maste   }
49205afe67SEd Maste }
50205afe67SEd Maste 
GetString() const5114f1b3e8SDimitry Andric llvm::StringRef StreamString::GetString() const { return m_packet; }
52f034231aSEd Maste 
FillLastLineToColumn(uint32_t column,char fill_char)5314f1b3e8SDimitry Andric void StreamString::FillLastLineToColumn(uint32_t column, char fill_char) {
54f034231aSEd Maste   const size_t length = m_packet.size();
55f034231aSEd Maste   size_t last_line_begin_pos = m_packet.find_last_of("\r\n");
5614f1b3e8SDimitry Andric   if (last_line_begin_pos == std::string::npos) {
57f034231aSEd Maste     last_line_begin_pos = 0;
5814f1b3e8SDimitry Andric   } else {
59f034231aSEd Maste     ++last_line_begin_pos;
60f034231aSEd Maste   }
61f034231aSEd Maste 
62f034231aSEd Maste   const size_t line_columns = length - last_line_begin_pos;
6314f1b3e8SDimitry Andric   if (column > line_columns) {
64f034231aSEd Maste     m_packet.append(column - line_columns, fill_char);
65f034231aSEd Maste   }
66f034231aSEd Maste }
67