xref: /src/contrib/llvm-project/lldb/source/Utility/Stream.cpp (revision 7a6dacaca14b62ca4b74406814becb87a3fefac0)
1cfca06d7SDimitry Andric //===-- Stream.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/Stream.h"
1074a628f7SDimitry Andric 
11b1c73532SDimitry Andric #include "lldb/Utility/AnsiTerminal.h"
1274a628f7SDimitry Andric #include "lldb/Utility/Endian.h"
1374a628f7SDimitry Andric #include "lldb/Utility/VASPrintf.h"
1494994d37SDimitry Andric #include "llvm/ADT/SmallString.h"
15706b4fc4SDimitry Andric #include "llvm/Support/Format.h"
1694994d37SDimitry Andric #include "llvm/Support/LEB128.h"
17b1c73532SDimitry Andric #include "llvm/Support/Regex.h"
1874a628f7SDimitry Andric 
1974a628f7SDimitry Andric #include <string>
20f034231aSEd Maste 
21344a3780SDimitry Andric #include <cinttypes>
22344a3780SDimitry Andric #include <cstddef>
23f034231aSEd Maste 
24f034231aSEd Maste using namespace lldb;
25f034231aSEd Maste using namespace lldb_private;
26f034231aSEd Maste 
Stream(uint32_t flags,uint32_t addr_size,ByteOrder byte_order,bool colors)27cfca06d7SDimitry Andric Stream::Stream(uint32_t flags, uint32_t addr_size, ByteOrder byte_order,
28cfca06d7SDimitry Andric                bool colors)
2914f1b3e8SDimitry Andric     : m_flags(flags), m_addr_size(addr_size), m_byte_order(byte_order),
30145449b1SDimitry Andric       m_forwarder(*this, colors) {}
31f034231aSEd Maste 
Stream(bool colors)32cfca06d7SDimitry Andric Stream::Stream(bool colors)
33344a3780SDimitry Andric     : m_flags(0), m_byte_order(endian::InlHostByteOrder()),
34344a3780SDimitry Andric       m_forwarder(*this, colors) {}
35f034231aSEd Maste 
36f034231aSEd Maste // Destructor
37344a3780SDimitry Andric Stream::~Stream() = default;
38f034231aSEd Maste 
SetByteOrder(ByteOrder byte_order)3914f1b3e8SDimitry Andric ByteOrder Stream::SetByteOrder(ByteOrder byte_order) {
40f034231aSEd Maste   ByteOrder old_byte_order = m_byte_order;
41f034231aSEd Maste   m_byte_order = byte_order;
42f034231aSEd Maste   return old_byte_order;
43f034231aSEd Maste }
44f034231aSEd Maste 
45f73363f1SDimitry Andric // Put an offset "uval" out to the stream using the printf format in "format".
Offset(uint32_t uval,const char * format)4614f1b3e8SDimitry Andric void Stream::Offset(uint32_t uval, const char *format) { Printf(format, uval); }
47f034231aSEd Maste 
48f73363f1SDimitry Andric // Put an SLEB128 "uval" out to the stream using the printf format in "format".
PutSLEB128(int64_t sval)4914f1b3e8SDimitry Andric size_t Stream::PutSLEB128(int64_t sval) {
5094994d37SDimitry Andric   if (m_flags.Test(eBinary))
5194994d37SDimitry Andric     return llvm::encodeSLEB128(sval, m_forwarder);
52f034231aSEd Maste   else
5394994d37SDimitry Andric     return Printf("0x%" PRIi64, sval);
54f034231aSEd Maste }
55f034231aSEd Maste 
56f73363f1SDimitry Andric // Put an ULEB128 "uval" out to the stream using the printf format in "format".
PutULEB128(uint64_t uval)5714f1b3e8SDimitry Andric size_t Stream::PutULEB128(uint64_t uval) {
5894994d37SDimitry Andric   if (m_flags.Test(eBinary))
5994994d37SDimitry Andric     return llvm::encodeULEB128(uval, m_forwarder);
6094994d37SDimitry Andric   else
6194994d37SDimitry Andric     return Printf("0x%" PRIx64, uval);
62f034231aSEd Maste }
63f034231aSEd Maste 
64f034231aSEd Maste // Print a raw NULL terminated C string to the stream.
PutCString(llvm::StringRef str)6514f1b3e8SDimitry Andric size_t Stream::PutCString(llvm::StringRef str) {
6614f1b3e8SDimitry Andric   size_t bytes_written = 0;
6714f1b3e8SDimitry Andric   bytes_written = Write(str.data(), str.size());
6814f1b3e8SDimitry Andric 
69f034231aSEd Maste   // when in binary mode, emit the NULL terminator
70f034231aSEd Maste   if (m_flags.Test(eBinary))
7114f1b3e8SDimitry Andric     bytes_written += PutChar('\0');
7214f1b3e8SDimitry Andric   return bytes_written;
73f034231aSEd Maste }
74f034231aSEd Maste 
PutCStringColorHighlighted(llvm::StringRef text,std::optional<HighlightSettings> pattern_info)754df029ccSDimitry Andric void Stream::PutCStringColorHighlighted(
764df029ccSDimitry Andric     llvm::StringRef text, std::optional<HighlightSettings> pattern_info) {
774df029ccSDimitry Andric   // Only apply color formatting when a pattern information is specified.
784df029ccSDimitry Andric   // Otherwise, output the text without color formatting.
794df029ccSDimitry Andric   if (!pattern_info.has_value()) {
80b1c73532SDimitry Andric     PutCString(text);
81b1c73532SDimitry Andric     return;
82b1c73532SDimitry Andric   }
83b1c73532SDimitry Andric 
844df029ccSDimitry Andric   llvm::Regex reg_pattern(pattern_info->pattern);
85b1c73532SDimitry Andric   llvm::SmallVector<llvm::StringRef, 1> matches;
86b1c73532SDimitry Andric   llvm::StringRef remaining = text;
87b1c73532SDimitry Andric   std::string format_str = lldb_private::ansi::FormatAnsiTerminalCodes(
884df029ccSDimitry Andric       pattern_info->prefix.str() + "%.*s" + pattern_info->suffix.str());
89b1c73532SDimitry Andric   while (reg_pattern.match(remaining, &matches)) {
90b1c73532SDimitry Andric     llvm::StringRef match = matches[0];
91b1c73532SDimitry Andric     size_t match_start_pos = match.data() - remaining.data();
92b1c73532SDimitry Andric     PutCString(remaining.take_front(match_start_pos));
93b1c73532SDimitry Andric     Printf(format_str.c_str(), match.size(), match.data());
94b1c73532SDimitry Andric     remaining = remaining.drop_front(match_start_pos + match.size());
95b1c73532SDimitry Andric   }
96b1c73532SDimitry Andric   if (remaining.size())
97b1c73532SDimitry Andric     PutCString(remaining);
98b1c73532SDimitry Andric }
99b1c73532SDimitry Andric 
100f73363f1SDimitry Andric // Print a double quoted NULL terminated C string to the stream using the
101f73363f1SDimitry Andric // printf format in "format".
QuotedCString(const char * cstr,const char * format)10214f1b3e8SDimitry Andric void Stream::QuotedCString(const char *cstr, const char *format) {
103f034231aSEd Maste   Printf(format, cstr);
104f034231aSEd Maste }
105f034231aSEd Maste 
106f73363f1SDimitry Andric // Put an address "addr" out to the stream with optional prefix and suffix
107f73363f1SDimitry Andric // strings.
DumpAddress(llvm::raw_ostream & s,uint64_t addr,uint32_t addr_size,const char * prefix,const char * suffix)108706b4fc4SDimitry Andric void lldb_private::DumpAddress(llvm::raw_ostream &s, uint64_t addr,
109706b4fc4SDimitry Andric                                uint32_t addr_size, const char *prefix,
11014f1b3e8SDimitry Andric                                const char *suffix) {
11194994d37SDimitry Andric   if (prefix == nullptr)
112f034231aSEd Maste     prefix = "";
11394994d37SDimitry Andric   if (suffix == nullptr)
114f034231aSEd Maste     suffix = "";
115706b4fc4SDimitry Andric   s << prefix << llvm::format_hex(addr, 2 + 2 * addr_size) << suffix;
116f034231aSEd Maste }
117f034231aSEd Maste 
118f73363f1SDimitry Andric // Put an address range out to the stream with optional prefix and suffix
119f73363f1SDimitry Andric // strings.
DumpAddressRange(llvm::raw_ostream & s,uint64_t lo_addr,uint64_t hi_addr,uint32_t addr_size,const char * prefix,const char * suffix)120706b4fc4SDimitry Andric void lldb_private::DumpAddressRange(llvm::raw_ostream &s, uint64_t lo_addr,
121706b4fc4SDimitry Andric                                     uint64_t hi_addr, uint32_t addr_size,
122706b4fc4SDimitry Andric                                     const char *prefix, const char *suffix) {
123f034231aSEd Maste   if (prefix && prefix[0])
124706b4fc4SDimitry Andric     s << prefix;
125706b4fc4SDimitry Andric   DumpAddress(s, lo_addr, addr_size, "[");
126706b4fc4SDimitry Andric   DumpAddress(s, hi_addr, addr_size, "-", ")");
127f034231aSEd Maste   if (suffix && suffix[0])
128706b4fc4SDimitry Andric     s << suffix;
129f034231aSEd Maste }
130f034231aSEd Maste 
PutChar(char ch)13114f1b3e8SDimitry Andric size_t Stream::PutChar(char ch) { return Write(&ch, 1); }
132f034231aSEd Maste 
133f034231aSEd Maste // Print some formatted output to the stream.
Printf(const char * format,...)13414f1b3e8SDimitry Andric size_t Stream::Printf(const char *format, ...) {
135f034231aSEd Maste   va_list args;
136f034231aSEd Maste   va_start(args, format);
137f034231aSEd Maste   size_t result = PrintfVarArg(format, args);
138f034231aSEd Maste   va_end(args);
139f034231aSEd Maste   return result;
140f034231aSEd Maste }
141f034231aSEd Maste 
142f034231aSEd Maste // Print some formatted output to the stream.
PrintfVarArg(const char * format,va_list args)14314f1b3e8SDimitry Andric size_t Stream::PrintfVarArg(const char *format, va_list args) {
14474a628f7SDimitry Andric   llvm::SmallString<1024> buf;
14574a628f7SDimitry Andric   VASprintf(buf, format, args);
146f034231aSEd Maste 
147f034231aSEd Maste   // Include the NULL termination byte for binary output
14874a628f7SDimitry Andric   size_t length = buf.size();
149f034231aSEd Maste   if (m_flags.Test(eBinary))
15074a628f7SDimitry Andric     ++length;
15174a628f7SDimitry Andric   return Write(buf.c_str(), length);
152f034231aSEd Maste }
153f034231aSEd Maste 
154f034231aSEd Maste // Print and End of Line character to the stream
EOL()15514f1b3e8SDimitry Andric size_t Stream::EOL() { return PutChar('\n'); }
156f034231aSEd Maste 
Indent(llvm::StringRef str)15714f1b3e8SDimitry Andric size_t Stream::Indent(llvm::StringRef str) {
158cfca06d7SDimitry Andric   const size_t ind_length = PutCString(std::string(m_indent_level, ' '));
159cfca06d7SDimitry Andric   const size_t str_length = PutCString(str);
160cfca06d7SDimitry Andric   return ind_length + str_length;
16114f1b3e8SDimitry Andric }
16214f1b3e8SDimitry Andric 
163f034231aSEd Maste // Stream a character "ch" out to this stream.
operator <<(char ch)16414f1b3e8SDimitry Andric Stream &Stream::operator<<(char ch) {
165f034231aSEd Maste   PutChar(ch);
166f034231aSEd Maste   return *this;
167f034231aSEd Maste }
168f034231aSEd Maste 
169f034231aSEd Maste // Stream the NULL terminated C string out to this stream.
operator <<(const char * s)17014f1b3e8SDimitry Andric Stream &Stream::operator<<(const char *s) {
171f034231aSEd Maste   Printf("%s", s);
172f034231aSEd Maste   return *this;
173f034231aSEd Maste }
174f034231aSEd Maste 
operator <<(llvm::StringRef str)17514f1b3e8SDimitry Andric Stream &Stream::operator<<(llvm::StringRef str) {
17614f1b3e8SDimitry Andric   Write(str.data(), str.size());
17714f1b3e8SDimitry Andric   return *this;
17814f1b3e8SDimitry Andric }
17914f1b3e8SDimitry Andric 
180f034231aSEd Maste // Stream the pointer value out to this stream.
operator <<(const void * p)18114f1b3e8SDimitry Andric Stream &Stream::operator<<(const void *p) {
1825f29bb8aSDimitry Andric   Printf("0x%.*tx", static_cast<int>(sizeof(const void *)) * 2, (ptrdiff_t)p);
183f034231aSEd Maste   return *this;
184f034231aSEd Maste }
185f034231aSEd Maste 
186f034231aSEd Maste // Get the current indentation level
GetIndentLevel() const187706b4fc4SDimitry Andric unsigned Stream::GetIndentLevel() const { return m_indent_level; }
188f034231aSEd Maste 
189f034231aSEd Maste // Set the current indentation level
SetIndentLevel(unsigned indent_level)190706b4fc4SDimitry Andric void Stream::SetIndentLevel(unsigned indent_level) {
191706b4fc4SDimitry Andric   m_indent_level = indent_level;
192706b4fc4SDimitry Andric }
193f034231aSEd Maste 
194f034231aSEd Maste // Increment the current indentation level
IndentMore(unsigned amount)195706b4fc4SDimitry Andric void Stream::IndentMore(unsigned amount) { m_indent_level += amount; }
196f034231aSEd Maste 
197f034231aSEd Maste // Decrement the current indentation level
IndentLess(unsigned amount)198706b4fc4SDimitry Andric void Stream::IndentLess(unsigned amount) {
199f034231aSEd Maste   if (m_indent_level >= amount)
200f034231aSEd Maste     m_indent_level -= amount;
201f034231aSEd Maste   else
202f034231aSEd Maste     m_indent_level = 0;
203f034231aSEd Maste }
204f034231aSEd Maste 
205f034231aSEd Maste // Get the address size in bytes
GetAddressByteSize() const20614f1b3e8SDimitry Andric uint32_t Stream::GetAddressByteSize() const { return m_addr_size; }
207f034231aSEd Maste 
208f034231aSEd Maste // Set the address size in bytes
SetAddressByteSize(uint32_t addr_size)20914f1b3e8SDimitry Andric void Stream::SetAddressByteSize(uint32_t addr_size) { m_addr_size = addr_size; }
210f034231aSEd Maste 
211f034231aSEd Maste // The flags get accessor
GetFlags()21214f1b3e8SDimitry Andric Flags &Stream::GetFlags() { return m_flags; }
213f034231aSEd Maste 
214f034231aSEd Maste // The flags const get accessor
GetFlags() const21514f1b3e8SDimitry Andric const Flags &Stream::GetFlags() const { return m_flags; }
216f034231aSEd Maste 
217f034231aSEd Maste // The byte order get accessor
218f034231aSEd Maste 
GetByteOrder() const21914f1b3e8SDimitry Andric lldb::ByteOrder Stream::GetByteOrder() const { return m_byte_order; }
220f034231aSEd Maste 
PrintfAsRawHex8(const char * format,...)22114f1b3e8SDimitry Andric size_t Stream::PrintfAsRawHex8(const char *format, ...) {
222f034231aSEd Maste   va_list args;
223f034231aSEd Maste   va_start(args, format);
224f034231aSEd Maste 
22574a628f7SDimitry Andric   llvm::SmallString<1024> buf;
22674a628f7SDimitry Andric   VASprintf(buf, format, args);
22774a628f7SDimitry Andric 
22894994d37SDimitry Andric   ByteDelta delta(*this);
22974a628f7SDimitry Andric   for (char C : buf)
23094994d37SDimitry Andric     _PutHex8(C, false);
23174a628f7SDimitry Andric 
232f034231aSEd Maste   va_end(args);
233f034231aSEd Maste 
23494994d37SDimitry Andric   return *delta;
235f034231aSEd Maste }
236f034231aSEd Maste 
PutNHex8(size_t n,uint8_t uvalue)23714f1b3e8SDimitry Andric size_t Stream::PutNHex8(size_t n, uint8_t uvalue) {
23894994d37SDimitry Andric   ByteDelta delta(*this);
239f034231aSEd Maste   for (size_t i = 0; i < n; ++i)
24094994d37SDimitry Andric     _PutHex8(uvalue, false);
24194994d37SDimitry Andric   return *delta;
242f034231aSEd Maste }
243f034231aSEd Maste 
_PutHex8(uint8_t uvalue,bool add_prefix)24494994d37SDimitry Andric void Stream::_PutHex8(uint8_t uvalue, bool add_prefix) {
24514f1b3e8SDimitry Andric   if (m_flags.Test(eBinary)) {
24694994d37SDimitry Andric     Write(&uvalue, 1);
24714f1b3e8SDimitry Andric   } else {
248f034231aSEd Maste     if (add_prefix)
249f034231aSEd Maste       PutCString("0x");
250f034231aSEd Maste 
25114f1b3e8SDimitry Andric     static char g_hex_to_ascii_hex_char[16] = {'0', '1', '2', '3', '4', '5',
25214f1b3e8SDimitry Andric                                                '6', '7', '8', '9', 'a', 'b',
25314f1b3e8SDimitry Andric                                                'c', 'd', 'e', 'f'};
254f034231aSEd Maste     char nibble_chars[2];
255f034231aSEd Maste     nibble_chars[0] = g_hex_to_ascii_hex_char[(uvalue >> 4) & 0xf];
256f034231aSEd Maste     nibble_chars[1] = g_hex_to_ascii_hex_char[(uvalue >> 0) & 0xf];
25794994d37SDimitry Andric     Write(nibble_chars, sizeof(nibble_chars));
258f034231aSEd Maste   }
259f034231aSEd Maste }
260f034231aSEd Maste 
PutHex8(uint8_t uvalue)26194994d37SDimitry Andric size_t Stream::PutHex8(uint8_t uvalue) {
26294994d37SDimitry Andric   ByteDelta delta(*this);
26394994d37SDimitry Andric   _PutHex8(uvalue, false);
26494994d37SDimitry Andric   return *delta;
26594994d37SDimitry Andric }
266f034231aSEd Maste 
PutHex16(uint16_t uvalue,ByteOrder byte_order)26714f1b3e8SDimitry Andric size_t Stream::PutHex16(uint16_t uvalue, ByteOrder byte_order) {
26894994d37SDimitry Andric   ByteDelta delta(*this);
26994994d37SDimitry Andric 
270f034231aSEd Maste   if (byte_order == eByteOrderInvalid)
271f034231aSEd Maste     byte_order = m_byte_order;
272f034231aSEd Maste 
27314f1b3e8SDimitry Andric   if (byte_order == eByteOrderLittle) {
27474a628f7SDimitry Andric     for (size_t byte = 0; byte < sizeof(uvalue); ++byte)
2755f29bb8aSDimitry Andric       _PutHex8(static_cast<uint8_t>(uvalue >> (byte * 8)), false);
27614f1b3e8SDimitry Andric   } else {
27774a628f7SDimitry Andric     for (size_t byte = sizeof(uvalue) - 1; byte < sizeof(uvalue); --byte)
2785f29bb8aSDimitry Andric       _PutHex8(static_cast<uint8_t>(uvalue >> (byte * 8)), false);
279f034231aSEd Maste   }
28094994d37SDimitry Andric   return *delta;
281f034231aSEd Maste }
282f034231aSEd Maste 
PutHex32(uint32_t uvalue,ByteOrder byte_order)28314f1b3e8SDimitry Andric size_t Stream::PutHex32(uint32_t uvalue, ByteOrder byte_order) {
28494994d37SDimitry Andric   ByteDelta delta(*this);
28594994d37SDimitry Andric 
286f034231aSEd Maste   if (byte_order == eByteOrderInvalid)
287f034231aSEd Maste     byte_order = m_byte_order;
288f034231aSEd Maste 
28914f1b3e8SDimitry Andric   if (byte_order == eByteOrderLittle) {
29074a628f7SDimitry Andric     for (size_t byte = 0; byte < sizeof(uvalue); ++byte)
2915f29bb8aSDimitry Andric       _PutHex8(static_cast<uint8_t>(uvalue >> (byte * 8)), false);
29214f1b3e8SDimitry Andric   } else {
29374a628f7SDimitry Andric     for (size_t byte = sizeof(uvalue) - 1; byte < sizeof(uvalue); --byte)
2945f29bb8aSDimitry Andric       _PutHex8(static_cast<uint8_t>(uvalue >> (byte * 8)), false);
295f034231aSEd Maste   }
29694994d37SDimitry Andric   return *delta;
297f034231aSEd Maste }
298f034231aSEd Maste 
PutHex64(uint64_t uvalue,ByteOrder byte_order)29914f1b3e8SDimitry Andric size_t Stream::PutHex64(uint64_t uvalue, ByteOrder byte_order) {
30094994d37SDimitry Andric   ByteDelta delta(*this);
30194994d37SDimitry Andric 
302f034231aSEd Maste   if (byte_order == eByteOrderInvalid)
303f034231aSEd Maste     byte_order = m_byte_order;
304f034231aSEd Maste 
30514f1b3e8SDimitry Andric   if (byte_order == eByteOrderLittle) {
30674a628f7SDimitry Andric     for (size_t byte = 0; byte < sizeof(uvalue); ++byte)
3075f29bb8aSDimitry Andric       _PutHex8(static_cast<uint8_t>(uvalue >> (byte * 8)), false);
30814f1b3e8SDimitry Andric   } else {
30974a628f7SDimitry Andric     for (size_t byte = sizeof(uvalue) - 1; byte < sizeof(uvalue); --byte)
3105f29bb8aSDimitry Andric       _PutHex8(static_cast<uint8_t>(uvalue >> (byte * 8)), false);
311f034231aSEd Maste   }
31294994d37SDimitry Andric   return *delta;
313f034231aSEd Maste }
314f034231aSEd Maste 
PutMaxHex64(uint64_t uvalue,size_t byte_size,lldb::ByteOrder byte_order)31514f1b3e8SDimitry Andric size_t Stream::PutMaxHex64(uint64_t uvalue, size_t byte_size,
31614f1b3e8SDimitry Andric                            lldb::ByteOrder byte_order) {
31714f1b3e8SDimitry Andric   switch (byte_size) {
31814f1b3e8SDimitry Andric   case 1:
3195f29bb8aSDimitry Andric     return PutHex8(static_cast<uint8_t>(uvalue));
32014f1b3e8SDimitry Andric   case 2:
3215f29bb8aSDimitry Andric     return PutHex16(static_cast<uint16_t>(uvalue), byte_order);
32214f1b3e8SDimitry Andric   case 4:
3235f29bb8aSDimitry Andric     return PutHex32(static_cast<uint32_t>(uvalue), byte_order);
32414f1b3e8SDimitry Andric   case 8:
32594994d37SDimitry Andric     return PutHex64(uvalue, byte_order);
326f034231aSEd Maste   }
327f034231aSEd Maste   return 0;
328f034231aSEd Maste }
329f034231aSEd Maste 
PutPointer(void * ptr)33014f1b3e8SDimitry Andric size_t Stream::PutPointer(void *ptr) {
33114f1b3e8SDimitry Andric   return PutRawBytes(&ptr, sizeof(ptr), endian::InlHostByteOrder(),
33214f1b3e8SDimitry Andric                      endian::InlHostByteOrder());
333f034231aSEd Maste }
334f034231aSEd Maste 
PutFloat(float f,ByteOrder byte_order)33514f1b3e8SDimitry Andric size_t Stream::PutFloat(float f, ByteOrder byte_order) {
336f034231aSEd Maste   if (byte_order == eByteOrderInvalid)
337f034231aSEd Maste     byte_order = m_byte_order;
338f034231aSEd Maste 
339e81d9d49SDimitry Andric   return PutRawBytes(&f, sizeof(f), endian::InlHostByteOrder(), byte_order);
340f034231aSEd Maste }
341f034231aSEd Maste 
PutDouble(double d,ByteOrder byte_order)34214f1b3e8SDimitry Andric size_t Stream::PutDouble(double d, ByteOrder byte_order) {
343f034231aSEd Maste   if (byte_order == eByteOrderInvalid)
344f034231aSEd Maste     byte_order = m_byte_order;
345f034231aSEd Maste 
346e81d9d49SDimitry Andric   return PutRawBytes(&d, sizeof(d), endian::InlHostByteOrder(), byte_order);
347f034231aSEd Maste }
348f034231aSEd Maste 
PutLongDouble(long double ld,ByteOrder byte_order)34914f1b3e8SDimitry Andric size_t Stream::PutLongDouble(long double ld, ByteOrder byte_order) {
350f034231aSEd Maste   if (byte_order == eByteOrderInvalid)
351f034231aSEd Maste     byte_order = m_byte_order;
352f034231aSEd Maste 
353e81d9d49SDimitry Andric   return PutRawBytes(&ld, sizeof(ld), endian::InlHostByteOrder(), byte_order);
354f034231aSEd Maste }
355f034231aSEd Maste 
PutRawBytes(const void * s,size_t src_len,ByteOrder src_byte_order,ByteOrder dst_byte_order)35614f1b3e8SDimitry Andric size_t Stream::PutRawBytes(const void *s, size_t src_len,
35714f1b3e8SDimitry Andric                            ByteOrder src_byte_order, ByteOrder dst_byte_order) {
35894994d37SDimitry Andric   ByteDelta delta(*this);
35994994d37SDimitry Andric 
360f034231aSEd Maste   if (src_byte_order == eByteOrderInvalid)
361f034231aSEd Maste     src_byte_order = m_byte_order;
362f034231aSEd Maste 
363f034231aSEd Maste   if (dst_byte_order == eByteOrderInvalid)
364f034231aSEd Maste     dst_byte_order = m_byte_order;
365f034231aSEd Maste 
3665f29bb8aSDimitry Andric   const uint8_t *src = static_cast<const uint8_t *>(s);
367f034231aSEd Maste   bool binary_was_set = m_flags.Test(eBinary);
368f034231aSEd Maste   if (!binary_was_set)
369f034231aSEd Maste     m_flags.Set(eBinary);
37014f1b3e8SDimitry Andric   if (src_byte_order == dst_byte_order) {
371f034231aSEd Maste     for (size_t i = 0; i < src_len; ++i)
37294994d37SDimitry Andric       _PutHex8(src[i], false);
37314f1b3e8SDimitry Andric   } else {
374145449b1SDimitry Andric     for (size_t i = src_len; i > 0; --i)
375145449b1SDimitry Andric       _PutHex8(src[i - 1], false);
376f034231aSEd Maste   }
377f034231aSEd Maste   if (!binary_was_set)
378f034231aSEd Maste     m_flags.Clear(eBinary);
379f034231aSEd Maste 
38094994d37SDimitry Andric   return *delta;
381f034231aSEd Maste }
382f034231aSEd Maste 
PutBytesAsRawHex8(const void * s,size_t src_len,ByteOrder src_byte_order,ByteOrder dst_byte_order)38314f1b3e8SDimitry Andric size_t Stream::PutBytesAsRawHex8(const void *s, size_t src_len,
38414f1b3e8SDimitry Andric                                  ByteOrder src_byte_order,
38514f1b3e8SDimitry Andric                                  ByteOrder dst_byte_order) {
38694994d37SDimitry Andric   ByteDelta delta(*this);
387145449b1SDimitry Andric 
388f034231aSEd Maste   if (src_byte_order == eByteOrderInvalid)
389f034231aSEd Maste     src_byte_order = m_byte_order;
390f034231aSEd Maste 
391f034231aSEd Maste   if (dst_byte_order == eByteOrderInvalid)
392f034231aSEd Maste     dst_byte_order = m_byte_order;
393f034231aSEd Maste 
3945f29bb8aSDimitry Andric   const uint8_t *src = static_cast<const uint8_t *>(s);
395f034231aSEd Maste   bool binary_is_set = m_flags.Test(eBinary);
396f034231aSEd Maste   m_flags.Clear(eBinary);
39714f1b3e8SDimitry Andric   if (src_byte_order == dst_byte_order) {
398f034231aSEd Maste     for (size_t i = 0; i < src_len; ++i)
39994994d37SDimitry Andric       _PutHex8(src[i], false);
40014f1b3e8SDimitry Andric   } else {
401145449b1SDimitry Andric     for (size_t i = src_len; i > 0; --i)
402145449b1SDimitry Andric       _PutHex8(src[i - 1], false);
403f034231aSEd Maste   }
404f034231aSEd Maste   if (binary_is_set)
405f034231aSEd Maste     m_flags.Set(eBinary);
406f034231aSEd Maste 
40794994d37SDimitry Andric   return *delta;
408f034231aSEd Maste }
409f034231aSEd Maste 
PutStringAsRawHex8(llvm::StringRef s)4105f29bb8aSDimitry Andric size_t Stream::PutStringAsRawHex8(llvm::StringRef s) {
41194994d37SDimitry Andric   ByteDelta delta(*this);
412f034231aSEd Maste   bool binary_is_set = m_flags.Test(eBinary);
413f034231aSEd Maste   m_flags.Clear(eBinary);
4145f29bb8aSDimitry Andric   for (char c : s)
4155f29bb8aSDimitry Andric     _PutHex8(c, false);
416f034231aSEd Maste   if (binary_is_set)
417f034231aSEd Maste     m_flags.Set(eBinary);
41894994d37SDimitry Andric   return *delta;
419f034231aSEd Maste }
420