From 458da2563fa674bbcfd32ade2254b3915f751491 Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Tue, 16 Feb 2016 23:13:53 -0800 Subject: Addressed code review comments. --- upb/def.c | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) (limited to 'upb/def.c') diff --git a/upb/def.c b/upb/def.c index a1dc192..abb5b05 100644 --- a/upb/def.c +++ b/upb/def.c @@ -722,33 +722,43 @@ const char *upb_fielddef_name(const upb_fielddef *f) { return upb_def_fullname(upb_fielddef_upcast(f)); } -bool upb_fielddef_getjsonname(const upb_fielddef *f, char *buf) { +size_t upb_fielddef_getjsonname(const upb_fielddef *f, char *buf, size_t len) { const char *name = upb_fielddef_name(f); - size_t i, j; + size_t src, dst = 0; bool ucase_next = false; - if (!name) return false; +#define WRITE(byte) \ + ++dst; \ + if (dst < len) buf[dst - 1] = byte; \ + else if (dst == len) buf[dst - 1] = '\0' + + if (!name) { + WRITE('\0'); + return 0; + } /* Implement the transformation as described in the spec: * 1. upper case all letters after an underscore. * 2. remove all underscores. */ - for (i = 0, j = 0; name[i]; i++) { - if (name[i] == '_') { + for (src = 0; name[src]; src++) { + if (name[src] == '_') { ucase_next = true; continue; } if (ucase_next) { - buf[j++] = toupper(name[i]); + WRITE(toupper(name[src])); ucase_next = false; } else { - buf[j++] = name[i]; + WRITE(name[src]); } } - buf[j] = '\0'; - return true; + WRITE('\0'); + return dst; + +#undef WRITE } const upb_msgdef *upb_fielddef_containingtype(const upb_fielddef *f) { -- cgit v1.2.3