Index: include/lldb/Utility/Stream.h =================================================================== --- include/lldb/Utility/Stream.h +++ include/lldb/Utility/Stream.h @@ -38,6 +38,25 @@ /// string mode. }; + /// Utility class for counting the bytes that were written to a stream in a + /// certain time span. + /// @example + /// ByteDelta delta(*this); + /// WriteDataToStream("foo"); + /// return *delta; + /// @endcode + class ByteDelta { + Stream *m_stream; + /// Bytes we have written so far when ByteDelta was created. + size_t m_start; + + public: + ByteDelta(Stream &s) : m_stream(&s), m_start(s.GetWrittenBytes()) {} + /// Returns the number of bytes written to the given Stream since this + /// ByteDelta object was created. + size_t operator*() const { return m_stream->GetWrittenBytes() - m_start; } + }; + //------------------------------------------------------------------ /// Construct with flags and address size and byte order. /// @@ -550,7 +569,7 @@ int m_indent_level; ///< Indention level. std::size_t m_bytes_written = 0; ///< Number of bytes written so far. - size_t _PutHex8(uint8_t uvalue, bool add_prefix); + void _PutHex8(uint8_t uvalue, bool add_prefix); //------------------------------------------------------------------ /// Output character bytes to the stream. Index: source/Utility/Stream.cpp =================================================================== --- source/Utility/Stream.cpp +++ source/Utility/Stream.cpp @@ -313,26 +313,25 @@ llvm::SmallString<1024> buf; VASprintf(buf, format, args); - size_t length = 0; + ByteDelta delta(*this); for (char C : buf) - length += _PutHex8(C, false); + _PutHex8(C, false); va_end(args); - return length; + return *delta; } size_t Stream::PutNHex8(size_t n, uint8_t uvalue) { - size_t bytes_written = 0; + ByteDelta delta(*this); for (size_t i = 0; i < n; ++i) - bytes_written += _PutHex8(uvalue, false); - return bytes_written; + _PutHex8(uvalue, false); + return *delta; } -size_t Stream::_PutHex8(uint8_t uvalue, bool add_prefix) { - size_t bytes_written = 0; +void Stream::_PutHex8(uint8_t uvalue, bool add_prefix) { if (m_flags.Test(eBinary)) { - bytes_written = Write(&uvalue, 1); + Write(&uvalue, 1); } else { if (add_prefix) PutCString("0x"); @@ -343,56 +342,62 @@ char nibble_chars[2]; nibble_chars[0] = g_hex_to_ascii_hex_char[(uvalue >> 4) & 0xf]; nibble_chars[1] = g_hex_to_ascii_hex_char[(uvalue >> 0) & 0xf]; - bytes_written = Write(nibble_chars, sizeof(nibble_chars)); + Write(nibble_chars, sizeof(nibble_chars)); } - return bytes_written; } -size_t Stream::PutHex8(uint8_t uvalue) { return _PutHex8(uvalue, false); } +size_t Stream::PutHex8(uint8_t uvalue) { + ByteDelta delta(*this); + _PutHex8(uvalue, false); + return *delta; +} size_t Stream::PutHex16(uint16_t uvalue, ByteOrder byte_order) { + ByteDelta delta(*this); + if (byte_order == eByteOrderInvalid) byte_order = m_byte_order; - size_t bytes_written = 0; if (byte_order == eByteOrderLittle) { for (size_t byte = 0; byte < sizeof(uvalue); ++byte) - bytes_written += _PutHex8((uint8_t)(uvalue >> (byte * 8)), false); + _PutHex8((uint8_t)(uvalue >> (byte * 8)), false); } else { for (size_t byte = sizeof(uvalue) - 1; byte < sizeof(uvalue); --byte) - bytes_written += _PutHex8((uint8_t)(uvalue >> (byte * 8)), false); + _PutHex8((uint8_t)(uvalue >> (byte * 8)), false); } - return bytes_written; + return *delta; } size_t Stream::PutHex32(uint32_t uvalue, ByteOrder byte_order) { + ByteDelta delta(*this); + if (byte_order == eByteOrderInvalid) byte_order = m_byte_order; - size_t bytes_written = 0; if (byte_order == eByteOrderLittle) { for (size_t byte = 0; byte < sizeof(uvalue); ++byte) - bytes_written += _PutHex8((uint8_t)(uvalue >> (byte * 8)), false); + _PutHex8((uint8_t)(uvalue >> (byte * 8)), false); } else { for (size_t byte = sizeof(uvalue) - 1; byte < sizeof(uvalue); --byte) - bytes_written += _PutHex8((uint8_t)(uvalue >> (byte * 8)), false); + _PutHex8((uint8_t)(uvalue >> (byte * 8)), false); } - return bytes_written; + return *delta; } size_t Stream::PutHex64(uint64_t uvalue, ByteOrder byte_order) { + ByteDelta delta(*this); + if (byte_order == eByteOrderInvalid) byte_order = m_byte_order; - size_t bytes_written = 0; if (byte_order == eByteOrderLittle) { for (size_t byte = 0; byte < sizeof(uvalue); ++byte) - bytes_written += _PutHex8((uint8_t)(uvalue >> (byte * 8)), false); + _PutHex8((uint8_t)(uvalue >> (byte * 8)), false); } else { for (size_t byte = sizeof(uvalue) - 1; byte < sizeof(uvalue); --byte) - bytes_written += _PutHex8((uint8_t)(uvalue >> (byte * 8)), false); + _PutHex8((uint8_t)(uvalue >> (byte * 8)), false); } - return bytes_written; + return *delta; } size_t Stream::PutMaxHex64(uint64_t uvalue, size_t byte_size, @@ -438,65 +443,66 @@ size_t Stream::PutRawBytes(const void *s, size_t src_len, ByteOrder src_byte_order, ByteOrder dst_byte_order) { + ByteDelta delta(*this); + if (src_byte_order == eByteOrderInvalid) src_byte_order = m_byte_order; if (dst_byte_order == eByteOrderInvalid) dst_byte_order = m_byte_order; - size_t bytes_written = 0; const uint8_t *src = (const uint8_t *)s; bool binary_was_set = m_flags.Test(eBinary); if (!binary_was_set) m_flags.Set(eBinary); if (src_byte_order == dst_byte_order) { for (size_t i = 0; i < src_len; ++i) - bytes_written += _PutHex8(src[i], false); + _PutHex8(src[i], false); } else { for (size_t i = src_len - 1; i < src_len; --i) - bytes_written += _PutHex8(src[i], false); + _PutHex8(src[i], false); } if (!binary_was_set) m_flags.Clear(eBinary); - return bytes_written; + return *delta; } size_t Stream::PutBytesAsRawHex8(const void *s, size_t src_len, ByteOrder src_byte_order, ByteOrder dst_byte_order) { + ByteDelta delta(*this); if (src_byte_order == eByteOrderInvalid) src_byte_order = m_byte_order; if (dst_byte_order == eByteOrderInvalid) dst_byte_order = m_byte_order; - size_t bytes_written = 0; const uint8_t *src = (const uint8_t *)s; bool binary_is_set = m_flags.Test(eBinary); m_flags.Clear(eBinary); if (src_byte_order == dst_byte_order) { for (size_t i = 0; i < src_len; ++i) - bytes_written += _PutHex8(src[i], false); + _PutHex8(src[i], false); } else { for (size_t i = src_len - 1; i < src_len; --i) - bytes_written += _PutHex8(src[i], false); + _PutHex8(src[i], false); } if (binary_is_set) m_flags.Set(eBinary); - return bytes_written; + return *delta; } size_t Stream::PutCStringAsRawHex8(const char *s) { - size_t bytes_written = 0; + ByteDelta delta(*this); bool binary_is_set = m_flags.Test(eBinary); m_flags.Clear(eBinary); while(*s) { - bytes_written += _PutHex8(*s, false); + _PutHex8(*s, false); ++s; } if (binary_is_set) m_flags.Set(eBinary); - return bytes_written; + return *delta; }