Simplify string object printing to reduce code duplication

This commit is contained in:
Michael Hansen
2023-06-09 09:09:03 -07:00
parent 4b4f9f8edb
commit 753d42d94a
5 changed files with 54 additions and 77 deletions

View File

@@ -3224,32 +3224,14 @@ bool print_docstring(PycRef<PycObject> obj, int indent, PycModule* mod,
std::ostream& pyc_output)
{
// docstrings are translated from the bytecode __doc__ = 'string' to simply '''string'''
signed char prefix = -1;
switch (obj.type()) {
case PycObject::TYPE_STRING:
prefix = mod->strIsUnicode() ? 'b' : 0;
break;
case PycObject::TYPE_UNICODE:
prefix = mod->strIsUnicode() ? 0 : 'u';
break;
case PycObject::TYPE_INTERNED:
case PycObject::TYPE_ASCII:
case PycObject::TYPE_ASCII_INTERNED:
case PycObject::TYPE_SHORT_ASCII:
case PycObject::TYPE_SHORT_ASCII_INTERNED:
if (mod->majorVer() >= 3)
prefix = 0;
else
prefix = mod->strIsUnicode() ? 'b' : 0;
break;
}
if (prefix != -1) {
auto doc = obj.try_cast<PycString>();
if (doc != nullptr) {
start_line(indent, pyc_output);
OutputString(pyc_output, obj.cast<PycString>(), prefix, true);
doc->print(pyc_output, mod, true);
pyc_output << "\n";
return true;
} else
return false;
}
return false;
}
void decompyle(PycRef<PycCode> code, PycModule* mod, std::ostream& pyc_output)