15ffd83dbSDimitry Andric //===-- StreamString.cpp --------------------------------------------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric 
90b57cec5SDimitry Andric #include "lldb/Utility/StreamString.h"
100b57cec5SDimitry Andric 
110b57cec5SDimitry Andric using namespace lldb;
120b57cec5SDimitry Andric using namespace lldb_private;
130b57cec5SDimitry Andric 
StreamString(bool colors)14297eecfbSDimitry Andric StreamString::StreamString(bool colors) : Stream(0, 4, eByteOrderBig, colors) {}
150b57cec5SDimitry Andric 
StreamString(uint32_t flags,uint32_t addr_size,ByteOrder byte_order)160b57cec5SDimitry Andric StreamString::StreamString(uint32_t flags, uint32_t addr_size,
170b57cec5SDimitry Andric                            ByteOrder byte_order)
180b57cec5SDimitry Andric     : Stream(flags, addr_size, byte_order), m_packet() {}
190b57cec5SDimitry Andric 
20fe6060f1SDimitry Andric StreamString::~StreamString() = default;
210b57cec5SDimitry Andric 
Flush()220b57cec5SDimitry Andric void StreamString::Flush() {
230b57cec5SDimitry Andric   // Nothing to do when flushing a buffer based stream...
240b57cec5SDimitry Andric }
250b57cec5SDimitry Andric 
WriteImpl(const void * s,size_t length)260b57cec5SDimitry Andric size_t StreamString::WriteImpl(const void *s, size_t length) {
27480093f4SDimitry Andric   m_packet.append(static_cast<const char *>(s), length);
280b57cec5SDimitry Andric   return length;
290b57cec5SDimitry Andric }
300b57cec5SDimitry Andric 
Clear()310b57cec5SDimitry Andric void StreamString::Clear() {
320b57cec5SDimitry Andric   m_packet.clear();
330b57cec5SDimitry Andric   m_bytes_written = 0;
340b57cec5SDimitry Andric }
350b57cec5SDimitry Andric 
Empty() const360b57cec5SDimitry Andric bool StreamString::Empty() const { return GetSize() == 0; }
370b57cec5SDimitry Andric 
GetSize() const380b57cec5SDimitry Andric size_t StreamString::GetSize() const { return m_packet.size(); }
390b57cec5SDimitry Andric 
GetSizeOfLastLine() const400b57cec5SDimitry Andric size_t StreamString::GetSizeOfLastLine() const {
410b57cec5SDimitry Andric   const size_t length = m_packet.size();
420b57cec5SDimitry Andric   size_t last_line_begin_pos = m_packet.find_last_of("\r\n");
430b57cec5SDimitry Andric   if (last_line_begin_pos == std::string::npos) {
440b57cec5SDimitry Andric     return length;
450b57cec5SDimitry Andric   } else {
460b57cec5SDimitry Andric     ++last_line_begin_pos;
470b57cec5SDimitry Andric     return length - last_line_begin_pos;
480b57cec5SDimitry Andric   }
490b57cec5SDimitry Andric }
500b57cec5SDimitry Andric 
GetString() const510b57cec5SDimitry Andric llvm::StringRef StreamString::GetString() const { return m_packet; }
520b57cec5SDimitry Andric 
FillLastLineToColumn(uint32_t column,char fill_char)530b57cec5SDimitry Andric void StreamString::FillLastLineToColumn(uint32_t column, char fill_char) {
540b57cec5SDimitry Andric   const size_t length = m_packet.size();
550b57cec5SDimitry Andric   size_t last_line_begin_pos = m_packet.find_last_of("\r\n");
560b57cec5SDimitry Andric   if (last_line_begin_pos == std::string::npos) {
570b57cec5SDimitry Andric     last_line_begin_pos = 0;
580b57cec5SDimitry Andric   } else {
590b57cec5SDimitry Andric     ++last_line_begin_pos;
600b57cec5SDimitry Andric   }
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric   const size_t line_columns = length - last_line_begin_pos;
630b57cec5SDimitry Andric   if (column > line_columns) {
640b57cec5SDimitry Andric     m_packet.append(column - line_columns, fill_char);
650b57cec5SDimitry Andric   }
660b57cec5SDimitry Andric }
67