1//===-- StreamString.cpp --------------------------------------------------===//
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#include "lldb/Utility/StreamString.h"
10
11using namespace lldb;
12using namespace lldb_private;
13
14StreamString::StreamString(bool colors) : Stream(0, 4, eByteOrderBig, colors) {}
15
16StreamString::StreamString(uint32_t flags, uint32_t addr_size,
17 ByteOrder byte_order)
18 : Stream(flags, addr_size, byte_order), m_packet() {}
19
20StreamString::~StreamString() = default;
21
22void StreamString::Flush() {
23 // Nothing to do when flushing a buffer based stream...
24}
25
26size_t StreamString::WriteImpl(const void *s, size_t length) {
27 m_packet.append(s: static_cast<const char *>(s), n: length);
28 return length;
29}
30
31void StreamString::Clear() {
32 m_packet.clear();
33 m_bytes_written = 0;
34}
35
36bool StreamString::Empty() const { return GetSize() == 0; }
37
38size_t StreamString::GetSize() const { return m_packet.size(); }
39
40size_t StreamString::GetSizeOfLastLine() const {
41 const size_t length = m_packet.size();
42 size_t last_line_begin_pos = m_packet.find_last_of(s: "\r\n");
43 if (last_line_begin_pos == std::string::npos) {
44 return length;
45 } else {
46 ++last_line_begin_pos;
47 return length - last_line_begin_pos;
48 }
49}
50
51llvm::StringRef StreamString::GetString() const { return m_packet; }
52
53void StreamString::FillLastLineToColumn(uint32_t column, char fill_char) {
54 const size_t length = m_packet.size();
55 size_t last_line_begin_pos = m_packet.find_last_of(s: "\r\n");
56 if (last_line_begin_pos == std::string::npos) {
57 last_line_begin_pos = 0;
58 } else {
59 ++last_line_begin_pos;
60 }
61
62 const size_t line_columns = length - last_line_begin_pos;
63 if (column > line_columns) {
64 m_packet.append(n: column - line_columns, c: fill_char);
65 }
66}
67

source code of lldb/source/Utility/StreamString.cpp