std::string print_entry(lazy_entry const& e, bool single_line, int indent) { char indent_str[200]; memset(indent_str, ' ', 200); indent_str[0] = ','; indent_str[1] = '\n'; indent_str[199] = 0; if (indent < 197 && indent >= 0) indent_str[indent+2] = 0; std::string ret; switch (e.type()) { case lazy_entry::none_t: return "none"; case lazy_entry::int_t: { char str[100]; std::snprintf(str, sizeof(str), "%" PRId64, e.int_value()); return str; } case lazy_entry::string_t: { print_string(ret, e.string_ptr(), e.string_length(), single_line); return ret; } case lazy_entry::list_t: { ret += '['; bool one_liner = line_longer_than(e, 200) != -1 || single_line; if (!one_liner) ret += indent_str + 1; for (int i = 0; i < e.list_size(); ++i) { if (i == 0 && one_liner) ret += " "; ret += print_entry(*e.list_at(i), single_line, indent + 2); if (i < e.list_size() - 1) ret += (one_liner?", ":indent_str); else ret += (one_liner?" ":indent_str+1); } ret += "]"; return ret; } case lazy_entry::dict_t: { ret += "{"; bool one_liner = line_longer_than(e, 200) != -1 || single_line; if (!one_liner) ret += indent_str+1; for (int i = 0; i < e.dict_size(); ++i) { if (i == 0 && one_liner) ret += " "; std::pair<std::string, lazy_entry const*> ent = e.dict_at(i); print_string(ret, ent.first.c_str(), int(ent.first.size()), true); ret += ": "; ret += print_entry(*ent.second, single_line, indent + 2); if (i < e.dict_size() - 1) ret += (one_liner?", ":indent_str); else ret += (one_liner?" ":indent_str+1); } ret += "}"; return ret; } } return ret; }
std::string print_entry(lazy_entry const& e, bool single_line, int indent) { char indent_str[200]; memset(indent_str, ' ', 200); indent_str[0] = ','; indent_str[1] = '\n'; indent_str[199] = 0; if (indent < 197 && indent >= 0) indent_str[indent+2] = 0; std::string ret; switch (e.type()) { case lazy_entry::none_t: return "none"; case lazy_entry::int_t: { char str[100]; snprintf(str, sizeof(str), "%"PRId64, e.int_value()); return str; } case lazy_entry::string_t: { bool printable = true; char const* str = e.string_ptr(); for (int i = 0; i < e.string_length(); ++i) { using namespace std; if (is_print((unsigned char)str[i])) continue; printable = false; break; } ret += "'"; if (printable) { ret += e.string_value(); ret += "'"; return ret; } for (int i = 0; i < e.string_length(); ++i) { char tmp[5]; snprintf(tmp, sizeof(tmp), "%02x", (unsigned char)str[i]); ret += tmp; } ret += "'"; return ret; } case lazy_entry::list_t: { ret += '['; bool one_liner = line_longer_than(e, 200) != -1 || single_line; if (!one_liner) ret += indent_str + 1; for (int i = 0; i < e.list_size(); ++i) { if (i == 0 && one_liner) ret += " "; ret += print_entry(*e.list_at(i), single_line, indent + 2); if (i < e.list_size() - 1) ret += (one_liner?", ":indent_str); else ret += (one_liner?" ":indent_str+1); } ret += "]"; return ret; } case lazy_entry::dict_t: { ret += "{"; bool one_liner = line_longer_than(e, 200) != -1 || single_line; if (!one_liner) ret += indent_str+1; for (int i = 0; i < e.dict_size(); ++i) { if (i == 0 && one_liner) ret += " "; std::pair<std::string, lazy_entry const*> ent = e.dict_at(i); ret += "'"; ret += ent.first; ret += "': "; ret += print_entry(*ent.second, single_line, indent + 2); if (i < e.dict_size() - 1) ret += (one_liner?", ":indent_str); else ret += (one_liner?" ":indent_str+1); } ret += "}"; return ret; } } return ret; }
std::string print_entry(lazy_entry const& e) { std::string ret; switch (e.type()) { case lazy_entry::none_t: return "none"; case lazy_entry::int_t: { char str[100]; snprintf(str, sizeof(str), "%"PRId64, e.int_value()); return str; } case lazy_entry::string_t: { bool printable = true; char const* str = e.string_ptr(); for (int i = 0; i < e.string_length(); ++i) { using namespace std; if (is_print((unsigned char)str[i])) continue; printable = false; break; } ret += "'"; if (printable) { ret += e.string_value(); ret += "'"; return ret; } for (int i = 0; i < e.string_length(); ++i) { char tmp[5]; snprintf(tmp, sizeof(tmp), "%02x", (unsigned char)str[i]); ret += tmp; } ret += "'"; return ret; } case lazy_entry::list_t: { ret += '['; bool one_liner = (e.list_size() == 0 || (e.list_at(0)->type() == lazy_entry::int_t && e.list_size() < 20) || (e.list_at(0)->type() == lazy_entry::string_t && (e.list_at(0)->string_length() < 10 || e.list_size() < 2) && e.list_size() < 5)); if (!one_liner) ret += "\n"; for (int i = 0; i < e.list_size(); ++i) { if (i == 0 && one_liner) ret += " "; ret += print_entry(*e.list_at(i)); if (i < e.list_size() - 1) ret += (one_liner?", ":",\n"); else ret += (one_liner?" ":"\n"); } ret += "]"; return ret; } case lazy_entry::dict_t: { ret += "{"; bool one_liner = (e.dict_size() == 0 || e.dict_at(0).second->type() == lazy_entry::int_t || (e.dict_at(0).second->type() == lazy_entry::string_t && e.dict_at(0).second->string_length() < 30) || e.dict_at(0).first.size() < 10) && e.dict_size() < 5; if (!one_liner) ret += "\n"; for (int i = 0; i < e.dict_size(); ++i) { if (i == 0 && one_liner) ret += " "; std::pair<std::string, lazy_entry const*> ent = e.dict_at(i); ret += "'"; ret += ent.first; ret += "': "; ret += print_entry(*ent.second); if (i < e.dict_size() - 1) ret += (one_liner?", ":",\n"); else ret += (one_liner?" ":"\n"); } ret += "}"; return ret; } } return ret; }