diff options
author | Joshua Haberman <jhaberman@gmail.com> | 2016-02-17 22:01:34 -0800 |
---|---|---|
committer | Joshua Haberman <jhaberman@gmail.com> | 2016-02-17 22:01:34 -0800 |
commit | 32236c9cbc5880024a4532030a21b4f396b30b63 (patch) | |
tree | ea224e80be970cf03a705cd40aadd0f2f83ef2b7 /upb/def.c | |
parent | ff6fe32744b75df52c98d8eb67dcea53d1572b68 (diff) | |
parent | 41506406506332675016916280693a8d94e1b3ac (diff) |
Merge pull request #46 from haberman/jsoncamel
Changed JSON parser/printer to correctly camelCase names.
Diffstat (limited to 'upb/def.c')
-rw-r--r-- | upb/def.c | 40 |
1 files changed, 40 insertions, 0 deletions
@@ -1,6 +1,7 @@ #include "upb/def.h" +#include <ctype.h> #include <stdlib.h> #include <string.h> #include "upb/structdefs.int.h" @@ -721,6 +722,45 @@ const char *upb_fielddef_name(const upb_fielddef *f) { return upb_def_fullname(upb_fielddef_upcast(f)); } +size_t upb_fielddef_getjsonname(const upb_fielddef *f, char *buf, size_t len) { + const char *name = upb_fielddef_name(f); + size_t src, dst = 0; + bool ucase_next = 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 (src = 0; name[src]; src++) { + if (name[src] == '_') { + ucase_next = true; + continue; + } + + if (ucase_next) { + WRITE(toupper(name[src])); + ucase_next = false; + } else { + WRITE(name[src]); + } + } + + WRITE('\0'); + return dst; + +#undef WRITE +} + const upb_msgdef *upb_fielddef_containingtype(const upb_fielddef *f) { return f->msg_is_symbolic ? NULL : f->msg.def; } |