summaryrefslogtreecommitdiff
path: root/upb/def.c
diff options
context:
space:
mode:
authorJoshua Haberman <jhaberman@gmail.com>2016-02-17 22:01:34 -0800
committerJoshua Haberman <jhaberman@gmail.com>2016-02-17 22:01:34 -0800
commit32236c9cbc5880024a4532030a21b4f396b30b63 (patch)
treeea224e80be970cf03a705cd40aadd0f2f83ef2b7 /upb/def.c
parentff6fe32744b75df52c98d8eb67dcea53d1572b68 (diff)
parent41506406506332675016916280693a8d94e1b3ac (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.c40
1 files changed, 40 insertions, 0 deletions
diff --git a/upb/def.c b/upb/def.c
index 0c694f8..abb5b05 100644
--- a/upb/def.c
+++ b/upb/def.c
@@ -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;
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback