4 * OPT: This is not optimized at all. It uses printf() which parses the format
5 * string every time, and it allocates memory for every put.
8 #include "upb/pb/textprinter.h"
19 #include "upb/port_def.inc"
21 struct upb_textprinter {
23 upb_bytessink output_;
29 #define CHECK(x) if ((x) < 0) goto err;
31 static const char *shortname(const char *longname) {
32 const char *last = strrchr(longname, '.');
33 return last ? last + 1 : longname;
36 static int indent(upb_textprinter *p) {
39 for (i = 0; i < p->indent_depth_; i++)
40 upb_bytessink_putbuf(p->output_, p->subc, " ", 2, NULL);
44 static int endfield(upb_textprinter *p) {
45 const char ch = (p->single_line_ ? ' ' : '\n');
46 upb_bytessink_putbuf(p->output_, p->subc, &ch, 1, NULL);
50 static int putescaped(upb_textprinter *p, const char *buf, size_t len,
52 /* Based on CEscapeInternal() from Google's protobuf release. */
53 char dstbuf[4096], *dst = dstbuf, *dstend = dstbuf + sizeof(dstbuf);
54 const char *end = buf + len;
56 /* I think hex is prettier and more useful, but proto2 uses octal; should
57 * investigate whether it can parse hex also. */
58 const bool use_hex = false;
59 bool last_hex_escape = false; /* true if last output char was \xNN */
61 for (; buf < end; buf++) {
64 if (dstend - dst < 4) {
65 upb_bytessink_putbuf(p->output_, p->subc, dstbuf, dst - dstbuf, NULL);
69 is_hex_escape = false;
71 case '\n': *(dst++) = '\\'; *(dst++) = 'n'; break;
72 case '\r': *(dst++) = '\\'; *(dst++) = 'r'; break;
73 case '\t': *(dst++) = '\\'; *(dst++) = 't'; break;
74 case '\"': *(dst++) = '\\'; *(dst++) = '\"'; break;
75 case '\'': *(dst++) = '\\'; *(dst++) = '\''; break;
76 case '\\': *(dst++) = '\\'; *(dst++) = '\\'; break;
78 /* Note that if we emit \xNN and the buf character after that is a hex
79 * digit then that digit must be escaped too to prevent it being
80 * interpreted as part of the character code by C. */
81 if ((!preserve_utf8 || (uint8_t)*buf < 0x80) &&
82 (!isprint(*buf) || (last_hex_escape && isxdigit(*buf)))) {
83 sprintf(dst, (use_hex ? "\\x%02x" : "\\%03o"), (uint8_t)*buf);
84 is_hex_escape = use_hex;
87 *(dst++) = *buf; break;
90 last_hex_escape = is_hex_escape;
92 /* Flush remaining data. */
93 upb_bytessink_putbuf(p->output_, p->subc, dstbuf, dst - dstbuf, NULL);
97 bool putf(upb_textprinter *p, const char *fmt, ...) {
107 /* Run once to get the length of the string. */
108 va_copy(args_copy, args);
109 len = vsnprintf(NULL, 0, fmt, args_copy);
112 /* + 1 for NULL terminator (vsprintf() requires it even if we don't). */
113 str = upb_gmalloc(len + 1);
114 if (!str) return false;
115 written = vsprintf(str, fmt, args);
117 UPB_ASSERT(written == len);
119 ok = upb_bytessink_putbuf(p->output_, p->subc, str, len, NULL);
125 /* handlers *******************************************************************/
127 static bool textprinter_startmsg(void *c, const void *hd) {
128 upb_textprinter *p = c;
130 if (p->indent_depth_ == 0) {
131 upb_bytessink_start(p->output_, 0, &p->subc);
136 static bool textprinter_endmsg(void *c, const void *hd, upb_status *s) {
137 upb_textprinter *p = c;
140 if (p->indent_depth_ == 0) {
141 upb_bytessink_end(p->output_);
146 #define TYPE(name, ctype, fmt) \
147 static bool textprinter_put ## name(void *closure, const void *handler_data, \
149 upb_textprinter *p = closure; \
150 const upb_fielddef *f = handler_data; \
152 putf(p, "%s: " fmt, upb_fielddef_name(f), val); \
153 CHECK(endfield(p)); \
159 static bool textprinter_putbool(void *closure, const void *handler_data,
161 upb_textprinter *p = closure;
162 const upb_fielddef *f = handler_data;
164 putf(p, "%s: %s", upb_fielddef_name(f), val ? "true" : "false");
171 #define STRINGIFY_HELPER(x) #x
172 #define STRINGIFY_MACROVAL(x) STRINGIFY_HELPER(x)
174 TYPE(int32, int32_t, "%" PRId32)
175 TYPE(int64, int64_t, "%" PRId64)
176 TYPE(uint32, uint32_t, "%" PRIu32)
177 TYPE(uint64, uint64_t, "%" PRIu64)
178 TYPE(float, float, "%." STRINGIFY_MACROVAL(FLT_DIG) "g")
179 TYPE(double, double, "%." STRINGIFY_MACROVAL(DBL_DIG) "g")
183 /* Output a symbolic value from the enum if found, else just print as int32. */
184 static bool textprinter_putenum(void *closure, const void *handler_data,
186 upb_textprinter *p = closure;
187 const upb_fielddef *f = handler_data;
188 const upb_enumdef *enum_def = upb_fielddef_enumsubdef(f);
189 const char *label = upb_enumdef_iton(enum_def, val);
192 putf(p, "%s: %s", upb_fielddef_name(f), label);
195 if (!textprinter_putint32(closure, handler_data, val))
201 static void *textprinter_startstr(void *closure, const void *handler_data,
203 upb_textprinter *p = closure;
204 const upb_fielddef *f = handler_data;
205 UPB_UNUSED(size_hint);
207 putf(p, "%s: \"", upb_fielddef_name(f));
211 static bool textprinter_endstr(void *closure, const void *handler_data) {
212 upb_textprinter *p = closure;
213 UPB_UNUSED(handler_data);
219 static size_t textprinter_putstr(void *closure, const void *hd, const char *buf,
220 size_t len, const upb_bufhandle *handle) {
221 upb_textprinter *p = closure;
222 const upb_fielddef *f = hd;
224 CHECK(putescaped(p, buf, len, upb_fielddef_type(f) == UPB_TYPE_STRING));
230 static void *textprinter_startsubmsg(void *closure, const void *handler_data) {
231 upb_textprinter *p = closure;
232 const char *name = handler_data;
234 putf(p, "%s {%c", name, p->single_line_ ? ' ' : '\n');
241 static bool textprinter_endsubmsg(void *closure, const void *handler_data) {
242 upb_textprinter *p = closure;
243 UPB_UNUSED(handler_data);
246 upb_bytessink_putbuf(p->output_, p->subc, "}", 1, NULL);
253 static void onmreg(const void *c, upb_handlers *h) {
254 const upb_msgdef *m = upb_handlers_msgdef(h);
258 upb_handlers_setstartmsg(h, textprinter_startmsg, NULL);
259 upb_handlers_setendmsg(h, textprinter_endmsg, NULL);
261 n = upb_msgdef_fieldcount(m);
262 for(i = 0; i < n; i++) {
263 const upb_fielddef *f = upb_msgdef_field(m, i);
264 upb_handlerattr attr = UPB_HANDLERATTR_INIT;
265 attr.handler_data = f;
266 switch (upb_fielddef_type(f)) {
268 upb_handlers_setint32(h, f, textprinter_putint32, &attr);
271 upb_handlers_setint64(h, f, textprinter_putint64, &attr);
273 case UPB_TYPE_UINT32:
274 upb_handlers_setuint32(h, f, textprinter_putuint32, &attr);
276 case UPB_TYPE_UINT64:
277 upb_handlers_setuint64(h, f, textprinter_putuint64, &attr);
280 upb_handlers_setfloat(h, f, textprinter_putfloat, &attr);
282 case UPB_TYPE_DOUBLE:
283 upb_handlers_setdouble(h, f, textprinter_putdouble, &attr);
286 upb_handlers_setbool(h, f, textprinter_putbool, &attr);
288 case UPB_TYPE_STRING:
290 upb_handlers_setstartstr(h, f, textprinter_startstr, &attr);
291 upb_handlers_setstring(h, f, textprinter_putstr, &attr);
292 upb_handlers_setendstr(h, f, textprinter_endstr, &attr);
294 case UPB_TYPE_MESSAGE: {
296 upb_fielddef_descriptortype(f) == UPB_DESCRIPTOR_TYPE_GROUP
297 ? shortname(upb_msgdef_fullname(upb_fielddef_msgsubdef(f)))
298 : upb_fielddef_name(f);
299 attr.handler_data = name;
300 upb_handlers_setstartsubmsg(h, f, textprinter_startsubmsg, &attr);
301 upb_handlers_setendsubmsg(h, f, textprinter_endsubmsg, &attr);
305 upb_handlers_setint32(h, f, textprinter_putenum, &attr);
311 static void textprinter_reset(upb_textprinter *p, bool single_line) {
312 p->single_line_ = single_line;
313 p->indent_depth_ = 0;
317 /* Public API *****************************************************************/
319 upb_textprinter *upb_textprinter_create(upb_arena *arena, const upb_handlers *h,
320 upb_bytessink output) {
321 upb_textprinter *p = upb_arena_malloc(arena, sizeof(upb_textprinter));
325 upb_sink_reset(&p->input_, h, p);
326 textprinter_reset(p, false);
331 upb_handlercache *upb_textprinter_newcache(void) {
332 return upb_handlercache_new(&onmreg, NULL);
335 upb_sink upb_textprinter_input(upb_textprinter *p) { return p->input_; }
337 void upb_textprinter_setsingleline(upb_textprinter *p, bool single_line) {
338 p->single_line_ = single_line;