Skip to content

Commit 6f51cee

Browse files
committed
[lldb] Refactor character printing in DumpDataExtractor
Summary: Just unifying all that copy-pasted code. Reviewers: JDevlieghere Reviewed By: JDevlieghere Differential Revision: https://reviews.llvm.org/D83662
1 parent 90e34b5 commit 6f51cee

File tree

1 file changed

+54
-100
lines changed

1 file changed

+54
-100
lines changed

lldb/source/Core/DumpDataExtractor.cpp

Lines changed: 54 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,57 @@ static lldb::offset_t DumpInstructions(const DataExtractor &DE, Stream *s,
175175
return offset;
176176
}
177177

178+
/// Prints the specific escape sequence of the given character to the stream.
179+
/// If the character doesn't have a known specific escape sequence (e.g., '\a',
180+
/// '\n' but not generic escape sequences such as'\x12'), this function will
181+
/// not modify the stream and return false.
182+
static bool TryDumpSpecialEscapedChar(Stream &s, const char c) {
183+
switch (c) {
184+
case '\033':
185+
// Common non-standard escape code for 'escape'.
186+
s.Printf("\\e");
187+
return true;
188+
case '\a':
189+
s.Printf("\\a");
190+
return true;
191+
case '\b':
192+
s.Printf("\\b");
193+
return true;
194+
case '\f':
195+
s.Printf("\\f");
196+
return true;
197+
case '\n':
198+
s.Printf("\\n");
199+
return true;
200+
case '\r':
201+
s.Printf("\\r");
202+
return true;
203+
case '\t':
204+
s.Printf("\\t");
205+
return true;
206+
case '\v':
207+
s.Printf("\\v");
208+
return true;
209+
case '\0':
210+
s.Printf("\\0");
211+
return true;
212+
default:
213+
return false;
214+
}
215+
}
216+
217+
/// Dump the character to a stream. A character that is not printable will be
218+
/// represented by its escape sequence.
219+
static void DumpCharacter(Stream &s, const char c) {
220+
if (TryDumpSpecialEscapedChar(s, c))
221+
return;
222+
if (llvm::isPrint(c)) {
223+
s.PutChar(c);
224+
return;
225+
}
226+
s.Printf("\\x%2.2x", c);
227+
}
228+
178229
lldb::offset_t lldb_private::DumpDataExtractor(
179230
const DataExtractor &DE, Stream *s, offset_t start_offset,
180231
lldb::Format item_format, size_t item_byte_size, size_t item_count,
@@ -299,40 +350,11 @@ lldb::offset_t lldb_private::DumpDataExtractor(
299350
if (llvm::isPrint(ch))
300351
s->Printf("%c", (char)ch);
301352
else if (item_format != eFormatCharPrintable) {
302-
switch (ch) {
303-
case '\033':
304-
s->Printf("\\e");
305-
break;
306-
case '\a':
307-
s->Printf("\\a");
308-
break;
309-
case '\b':
310-
s->Printf("\\b");
311-
break;
312-
case '\f':
313-
s->Printf("\\f");
314-
break;
315-
case '\n':
316-
s->Printf("\\n");
317-
break;
318-
case '\r':
319-
s->Printf("\\r");
320-
break;
321-
case '\t':
322-
s->Printf("\\t");
323-
break;
324-
case '\v':
325-
s->Printf("\\v");
326-
break;
327-
case '\0':
328-
s->Printf("\\0");
329-
break;
330-
default:
353+
if (!TryDumpSpecialEscapedChar(*s, ch)) {
331354
if (item_byte_size == 1)
332355
s->Printf("\\x%2.2x", (uint8_t)ch);
333356
else
334357
s->Printf("%" PRIu64, ch);
335-
break;
336358
}
337359
} else {
338360
s->PutChar(NON_PRINTABLE_CHAR);
@@ -387,42 +409,7 @@ lldb::offset_t lldb_private::DumpDataExtractor(
387409
s->PutChar('\'');
388410
for (uint32_t i = 0; i < item_byte_size; ++i) {
389411
uint8_t ch = (uint8_t)(uval64 >> ((item_byte_size - i - 1) * 8));
390-
if (llvm::isPrint(ch))
391-
s->Printf("%c", ch);
392-
else {
393-
switch (ch) {
394-
case '\033':
395-
s->Printf("\\e");
396-
break;
397-
case '\a':
398-
s->Printf("\\a");
399-
break;
400-
case '\b':
401-
s->Printf("\\b");
402-
break;
403-
case '\f':
404-
s->Printf("\\f");
405-
break;
406-
case '\n':
407-
s->Printf("\\n");
408-
break;
409-
case '\r':
410-
s->Printf("\\r");
411-
break;
412-
case '\t':
413-
s->Printf("\\t");
414-
break;
415-
case '\v':
416-
s->Printf("\\v");
417-
break;
418-
case '\0':
419-
s->Printf("\\0");
420-
break;
421-
default:
422-
s->Printf("\\x%2.2x", ch);
423-
break;
424-
}
425-
}
412+
DumpCharacter(*s, ch);
426413
}
427414
s->PutChar('\'');
428415
} break;
@@ -437,40 +424,7 @@ lldb::offset_t lldb_private::DumpDataExtractor(
437424
s->PutChar('\"');
438425

439426
while (const char c = *cstr) {
440-
if (llvm::isPrint(c)) {
441-
s->PutChar(c);
442-
} else {
443-
switch (c) {
444-
case '\033':
445-
s->Printf("\\e");
446-
break;
447-
case '\a':
448-
s->Printf("\\a");
449-
break;
450-
case '\b':
451-
s->Printf("\\b");
452-
break;
453-
case '\f':
454-
s->Printf("\\f");
455-
break;
456-
case '\n':
457-
s->Printf("\\n");
458-
break;
459-
case '\r':
460-
s->Printf("\\r");
461-
break;
462-
case '\t':
463-
s->Printf("\\t");
464-
break;
465-
case '\v':
466-
s->Printf("\\v");
467-
break;
468-
default:
469-
s->Printf("\\x%2.2x", c);
470-
break;
471-
}
472-
}
473-
427+
DumpCharacter(*s, c);
474428
++cstr;
475429
}
476430

0 commit comments

Comments
 (0)