18 #include "llvm/ADT/StringExtras.h"
19 #include "llvm/Support/ConvertUTF.h"
40 llvm_unreachable(
"unsupported length");
41 memcpy(m_data, bytes, size);
47 const uint8_t *
GetBytes()
const {
return m_data; }
52 static constexpr
unsigned MaxLength = 16;
55 uint8_t m_data[MaxLength] = {0};
63 template <StringElementType type>
70 if (codepoint <= 0x1F || codepoint == 0x7F)
74 if (codepoint >= 0x80 && codepoint <= 0x9F)
78 if (codepoint == 0x2028 || codepoint == 0x2029)
82 if (codepoint == 0x200E || codepoint == 0x200F ||
83 (codepoint >= 0x202A &&
88 if (codepoint >= 0xFFF9 &&
98 const bool is_swift_escape_style =
99 escape_style == StringPrinter::EscapeStyle::Swift;
106 if (is_swift_escape_style)
110 if (is_swift_escape_style)
120 if (is_swift_escape_style)
126 if (is_swift_escape_style)
137 uint8_t *buffer, uint8_t *buffer_end, uint8_t *&next,
148 if (llvm::isPrint(*buffer))
151 unsigned escaped_len;
152 constexpr
unsigned max_buffer_size = 7;
153 uint8_t data[max_buffer_size];
154 switch (escape_style) {
155 case StringPrinter::EscapeStyle::CXX:
157 escaped_len = sprintf((
char *)data,
"\\x%02x", *buffer);
159 case StringPrinter::EscapeStyle::Swift:
161 escaped_len = sprintf((
char *)data,
"\\u{%x}", *buffer);
164 lldbassert(escaped_len > 0 &&
"unknown string escape style");
165 return {data, escaped_len};
170 uint8_t *buffer, uint8_t *buffer_end, uint8_t *&next,
175 if (!llvm::isLegalUTF8Sequence(buffer, buffer_end))
176 return GetPrintableImpl<StringElementType::ASCII>(buffer, buffer_end, next,
180 llvm::UTF32 codepoint = 0;
181 const llvm::UTF8 *buffer_for_conversion = buffer;
182 llvm::ConversionResult result = llvm::convertUTF8Sequence(
183 &buffer_for_conversion, buffer_end, &codepoint, llvm::strictConversion);
184 assert(result == llvm::conversionOK &&
185 "Failed to convert legal utf8 sequence");
189 const unsigned utf8_encoded_len = buffer_for_conversion - buffer;
190 next = buffer + utf8_encoded_len;
196 return {buffer, utf8_encoded_len};
198 unsigned escaped_len;
199 constexpr
unsigned max_buffer_size = 13;
200 uint8_t data[max_buffer_size];
201 switch (escape_style) {
202 case StringPrinter::EscapeStyle::CXX:
204 escaped_len = sprintf((
char *)data,
"\\U%08x", codepoint);
206 case StringPrinter::EscapeStyle::Swift:
208 escaped_len = sprintf((
char *)data,
"\\u{%x}", codepoint);
211 lldbassert(escaped_len > 0 &&
"unknown string escape style");
212 return {data, escaped_len};
219 uint8_t *buffer_end, uint8_t *&next,
221 if (!buffer || buffer >= buffer_end)
225 case StringElementType::ASCII:
226 return GetPrintableImpl<StringElementType::ASCII>(buffer, buffer_end, next,
228 case StringElementType::UTF8:
229 return GetPrintableImpl<StringElementType::UTF8>(buffer, buffer_end, next,
240 case GetPrintableElementType::UTF8:
241 case GetPrintableElementType::ASCII:
242 return [escape_style, elem_type](uint8_t *buffer, uint8_t *buffer_end,
244 return GetPrintable(elem_type == GetPrintableElementType::UTF8
245 ? StringElementType::UTF8
246 : StringElementType::ASCII,
247 buffer, buffer_end, next, escape_style);
250 llvm_unreachable(
"bad element type");
255 template <
typename SourceDataType>
258 llvm::ConversionResult (*ConvertFunction)(
const SourceDataType **,
259 const SourceDataType *,
260 llvm::UTF8 **, llvm::UTF8 *,
261 llvm::ConversionFlags),
263 assert(dump_options.
GetStream() &&
"need a Stream to print the string to");
269 auto data(dump_options.
GetData());
271 if (data.GetByteSize() && data.GetDataStart() && data.GetDataEnd()) {
272 const int bufferSPSize = data.GetByteSize();
274 const int origin_encoding = 8 *
sizeof(SourceDataType);
275 source_size = bufferSPSize / (origin_encoding / 4);
278 const SourceDataType *data_ptr =
279 (
const SourceDataType *)data.GetDataStart();
280 const SourceDataType *data_end_ptr = data_ptr + source_size;
284 if (zero_is_terminator) {
285 while (data_ptr < data_end_ptr) {
287 data_end_ptr = data_ptr;
293 data_ptr = (
const SourceDataType *)data.GetDataStart();
296 lldb::WritableDataBufferSP utf8_data_buffer_sp;
297 llvm::UTF8 *utf8_data_ptr =
nullptr;
298 llvm::UTF8 *utf8_data_end_ptr =
nullptr;
300 if (ConvertFunction) {
301 utf8_data_buffer_sp =
302 std::make_shared<DataBufferHeap>(4 * bufferSPSize, 0);
303 utf8_data_ptr = (llvm::UTF8 *)utf8_data_buffer_sp->GetBytes();
304 utf8_data_end_ptr = utf8_data_ptr + utf8_data_buffer_sp->GetByteSize();
305 ConvertFunction(&data_ptr, data_end_ptr, &utf8_data_ptr,
306 utf8_data_end_ptr, llvm::lenientConversion);
307 if (!zero_is_terminator)
308 utf8_data_end_ptr = utf8_data_ptr;
312 (llvm::UTF8 *)utf8_data_buffer_sp->GetBytes();
316 utf8_data_ptr =
const_cast<llvm::UTF8 *
>(
317 reinterpret_cast<const llvm::UTF8 *
>(data_ptr));
318 utf8_data_end_ptr =
const_cast<llvm::UTF8 *
>(
319 reinterpret_cast<const llvm::UTF8 *
>(data_end_ptr));
324 if (escape_non_printables)
331 for (; utf8_data_ptr < utf8_data_end_ptr;) {
332 if (zero_is_terminator && !*utf8_data_ptr)
335 if (escape_non_printables) {
336 uint8_t *next_data =
nullptr;
338 escaping_callback(utf8_data_ptr, utf8_data_end_ptr, next_data);
339 auto printable_bytes = printable.GetBytes();
340 auto printable_size = printable.GetSize();
343 if (!printable_bytes || !next_data)
346 for (
unsigned c = 0; c < printable_size; c++)
347 stream.
Printf(
"%c", *(printable_bytes + c));
348 utf8_data_ptr = (uint8_t *)next_data;
350 stream.
Printf(
"%c", *utf8_data_ptr);
368 valobj.
GetTargetSP()->GetDebugger().GetEscapeNonPrintables());
375 valobj.
GetTargetSP()->GetDebugger().GetEscapeNonPrintables());
393 namespace formatters {
395 template <
typename SourceDataType>
399 llvm::ConversionResult (*ConvertFunction)(
const SourceDataType **,
400 const SourceDataType *,
401 llvm::UTF8 **, llvm::UTF8 *,
402 llvm::ConversionFlags)) {
403 assert(options.
GetStream() &&
"need a Stream to print the string to");
415 constexpr
int type_width =
sizeof(SourceDataType);
416 constexpr
int origin_encoding = 8 * type_width;
417 if (origin_encoding != 8 && origin_encoding != 16 && origin_encoding != 32)
420 if (origin_encoding != 8 && !ConvertFunction)
425 bool is_truncated =
false;
426 const auto max_size = target_sp->GetMaximumSizeOfStringSummary();
437 sourceSize = max_size;
438 needs_zero_terminator =
true;
442 if (sourceSize > max_size) {
443 sourceSize = max_size;
448 sourceSize = max_size;
449 needs_zero_terminator =
true;
452 const int bufferSPSize = sourceSize * type_width;
453 lldb::WritableDataBufferSP buffer_sp(
new DataBufferHeap(bufferSPSize, 0));
458 if (sourceSize != 0 && !buffer_sp->GetBytes())
462 char *buffer =
reinterpret_cast<char *
>(buffer_sp->GetBytes());
465 target_sp->ReadCStringFromMemory(options.
GetLocation(), buffer,
466 bufferSPSize,
error);
467 else if (needs_zero_terminator)
468 target_sp->ReadStringFromMemory(options.
GetLocation(), buffer,
469 bufferSPSize,
error, type_width);
479 DataExtractor(buffer_sp, target_sp->GetArchitecture().GetByteOrder(),
480 target_sp->GetArchitecture().GetAddressByteSize()));
484 if (needs_zero_terminator)
494 bool StringPrinter::ReadStringAndDumpToStream<StringElementType::UTF8>(
495 const ReadStringAndDumpToStreamOptions &options) {
501 bool StringPrinter::ReadStringAndDumpToStream<StringElementType::UTF16>(
502 const ReadStringAndDumpToStreamOptions &options) {
503 return ReadEncodedBufferAndDumpToStream<llvm::UTF16>(
508 bool StringPrinter::ReadStringAndDumpToStream<StringElementType::UTF32>(
509 const ReadStringAndDumpToStreamOptions &options) {
510 return ReadEncodedBufferAndDumpToStream<llvm::UTF32>(
515 bool StringPrinter::ReadStringAndDumpToStream<StringElementType::ASCII>(
516 const ReadStringAndDumpToStreamOptions &options) {
522 bool StringPrinter::ReadBufferAndDumpToStream<StringElementType::UTF8>(
523 const ReadBufferAndDumpToStreamOptions &options) {
529 bool StringPrinter::ReadBufferAndDumpToStream<StringElementType::UTF16>(
530 const ReadBufferAndDumpToStreamOptions &options) {
532 llvm::ConvertUTF16toUTF8, options);
536 bool StringPrinter::ReadBufferAndDumpToStream<StringElementType::UTF32>(
537 const ReadBufferAndDumpToStreamOptions &options) {
539 llvm::ConvertUTF32toUTF8, options);
543 bool StringPrinter::ReadBufferAndDumpToStream<StringElementType::ASCII>(
544 const ReadBufferAndDumpToStreamOptions &options) {
555 return ReadBufferAndDumpToStream<StringElementType::UTF8>(options);