llvm-project/lldb/source/Utility/StreamString.cpp

68 lines
1.9 KiB
C++
Raw Normal View History

//===-- StreamString.cpp ----------------------------------------*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "lldb/Utility/StreamString.h"
using namespace lldb;
using namespace lldb_private;
StreamString::StreamString() : Stream(0, 4, eByteOrderBig) {}
StreamString::StreamString(uint32_t flags, uint32_t addr_size,
ByteOrder byte_order)
: Stream(flags, addr_size, byte_order), m_packet() {}
StreamString::~StreamString() {}
void StreamString::Flush() {
// Nothing to do when flushing a buffer based stream...
}
Add byte counting mechanism to LLDB's Stream class. Summary: This patch allows LLDB's Stream class to count the bytes it has written to so far. There are two major motivations for this patch: The first one is that this will allow us to get rid of all the handwritten byte counting code we have in LLDB so far. Examples for this are pretty much all functions in LLDB that take a Stream to write to and return a size_t, which usually represents the bytes written. By moving to this centralized byte counting mechanism, we hopefully can avoid some tricky errors that happen when some code forgets to count the written bytes while writing something to a stream. The second motivation is that this is needed for the migration away from LLDB's `Stream` and towards LLVM's `raw_ostream`. My current plan is to start offering a fake raw_ostream class that just forwards to a LLDB Stream. However, for this raw_ostream wrapper we need to fulfill the raw_ostream interface with LLDB's Stream, which currently lacks the ability to count the bytes written so far (which raw_ostream exposes by it's `tell()` method). By adding this functionality it is trivial to start rolling out our raw_ostream wrapper (and then eventually completely move to raw_ostream). Also, once this fake raw_ostream is available, we can start replacing our own code writing to LLDB's Stream by LLVM code writing to raw_ostream. The best example for this is the LEB128 encoding we currently ship, which can be replaced with by LLVM's version which accepts an raw_ostream. From the point of view of the pure source changes this test does, we essentially just renamed the Write implementation in Stream to `WriteImpl` while the `Write` method everyone is using to write its raw bytes is now just forwarding and counting the written bytes. Reviewers: labath, davide Reviewed By: labath Subscribers: JDevlieghere, lldb-commits Differential Revision: https://reviews.llvm.org/D50159 llvm-svn: 338733
2018-08-03 00:38:34 +08:00
size_t StreamString::WriteImpl(const void *s, size_t length) {
m_packet.append(reinterpret_cast<const char *>(s), length);
return length;
}
Add byte counting mechanism to LLDB's Stream class. Summary: This patch allows LLDB's Stream class to count the bytes it has written to so far. There are two major motivations for this patch: The first one is that this will allow us to get rid of all the handwritten byte counting code we have in LLDB so far. Examples for this are pretty much all functions in LLDB that take a Stream to write to and return a size_t, which usually represents the bytes written. By moving to this centralized byte counting mechanism, we hopefully can avoid some tricky errors that happen when some code forgets to count the written bytes while writing something to a stream. The second motivation is that this is needed for the migration away from LLDB's `Stream` and towards LLVM's `raw_ostream`. My current plan is to start offering a fake raw_ostream class that just forwards to a LLDB Stream. However, for this raw_ostream wrapper we need to fulfill the raw_ostream interface with LLDB's Stream, which currently lacks the ability to count the bytes written so far (which raw_ostream exposes by it's `tell()` method). By adding this functionality it is trivial to start rolling out our raw_ostream wrapper (and then eventually completely move to raw_ostream). Also, once this fake raw_ostream is available, we can start replacing our own code writing to LLDB's Stream by LLVM code writing to raw_ostream. The best example for this is the LEB128 encoding we currently ship, which can be replaced with by LLVM's version which accepts an raw_ostream. From the point of view of the pure source changes this test does, we essentially just renamed the Write implementation in Stream to `WriteImpl` while the `Write` method everyone is using to write its raw bytes is now just forwarding and counting the written bytes. Reviewers: labath, davide Reviewed By: labath Subscribers: JDevlieghere, lldb-commits Differential Revision: https://reviews.llvm.org/D50159 llvm-svn: 338733
2018-08-03 00:38:34 +08:00
void StreamString::Clear() {
m_packet.clear();
m_bytes_written = 0;
}
bool StreamString::Empty() const { return GetSize() == 0; }
size_t StreamString::GetSize() const { return m_packet.size(); }
size_t StreamString::GetSizeOfLastLine() const {
const size_t length = m_packet.size();
size_t last_line_begin_pos = m_packet.find_last_of("\r\n");
if (last_line_begin_pos == std::string::npos) {
return length;
} else {
++last_line_begin_pos;
return length - last_line_begin_pos;
}
}
llvm::StringRef StreamString::GetString() const { return m_packet; }
void StreamString::FillLastLineToColumn(uint32_t column, char fill_char) {
const size_t length = m_packet.size();
size_t last_line_begin_pos = m_packet.find_last_of("\r\n");
if (last_line_begin_pos == std::string::npos) {
last_line_begin_pos = 0;
} else {
++last_line_begin_pos;
}
const size_t line_columns = length - last_line_begin_pos;
if (column > line_columns) {
m_packet.append(column - line_columns, fill_char);
}
}