Skip to content

Commit

Permalink
feat!: shorten symbols
Browse files Browse the repository at this point in the history
  • Loading branch information
lcsmuller committed Feb 5, 2022
1 parent 9a4e551 commit 9b97152
Show file tree
Hide file tree
Showing 4 changed files with 147 additions and 163 deletions.
38 changes: 19 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ jsonb b;
char buf[1024];

jsonb_init(&b);
jsonb_push_object(&b, buf, sizeof(buf));
jsonb_object(&b, buf, sizeof(buf));
{
jsonb_push_key(&b, buf, sizeof(buf), "foo", strlen("foo"));
jsonb_push_array(&b, buf, sizeof(buf));
jsonb_key(&b, buf, sizeof(buf), "foo", strlen("foo"));
jsonb_array(&b, buf, sizeof(buf));
{
jsonb_push_number(&b, buf, sizeof(buf), 1);
jsonb_push_string(&b, buf, sizeof(buf), "hi", 2);
jsonb_push_bool(&b, buf, sizeof(buf), 0);
jsonb_push_null(&b, buf, sizeof(buf));
jsonb_pop_array(&b, buf, sizeof(buf));
jsonb_number(&b, buf, sizeof(buf), 1);
jsonb_string(&b, buf, sizeof(buf), "hi", 2);
jsonb_bool(&b, buf, sizeof(buf), 0);
jsonb_null(&b, buf, sizeof(buf));
jsonb_array_pop(&b, buf, sizeof(buf));
}
jsonb_pop_object(&b, buf, sizeof(buf));
jsonb_object_pop(&b, buf, sizeof(buf));
}
printf("JSON: %s", buf); // JSON: {"foo":[1,"hi",false,null]}
```
Expand All @@ -58,16 +58,16 @@ API
---

* `jsonb_init()` - initialize a jsonb handle
* `jsonb_push_object()` - push an object to the builder stack
* `jsonb_pop_object()` - pop an object from the builder stack
* `jsonb_push_key()` - push an object key field to the builder stack
* `jsonb_push_array()` - push an array to the builder stack
* `jsonb_pop_array()` - pop an array from the builder stack
* `jsonb_push_token()` - push a raw token to the builder stack
* `jsonb_push_bool()` - push a boolean token to the builder stack
* `jsonb_push_null()` - push a null token to the builder stack
* `jsonb_push_string()` - push a string token to the builder stack
* `jsonb_push_number()` - push a number token to the builder stack
* `jsonb_object()` - push an object to the builder stack
* `jsonb_object_pop()` - pop an object from the builder stack
* `jsonb_key()` - push an object key field to the builder stack
* `jsonb_array()` - push an array to the builder stack
* `jsonb_array_pop()` - pop an array from the builder stack
* `jsonb_token()` - push a raw token to the builder stack
* `jsonb_bool()` - push a boolean token to the builder stack
* `jsonb_null()` - push a null token to the builder stack
* `jsonb_string()` - push a string token to the builder stack
* `jsonb_number()` - push a number token to the builder stack

The following are the possible return codes for the builder functions:
* `JSONB_OK` - operation was a success, user can proceed with the next operation
Expand Down
102 changes: 43 additions & 59 deletions json-build.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,7 @@ JSONB_API void jsonb_init(jsonb *builder);
* @param bufsize the JSON buffer size
* @return @ref jsonbcode value
*/
JSONB_API jsonbcode jsonb_push_object(jsonb *builder,
char buf[],
size_t bufsize);
JSONB_API jsonbcode jsonb_object(jsonb *builder, char buf[], size_t bufsize);

/**
* @brief Pop an object from the builder
Expand All @@ -88,7 +86,7 @@ JSONB_API jsonbcode jsonb_push_object(jsonb *builder,
* @param bufsize the JSON buffer size
* @return @ref jsonbcode value
*/
JSONB_API jsonbcode jsonb_pop_object(jsonb *builder,
JSONB_API jsonbcode jsonb_object_pop(jsonb *builder,
char buf[],
size_t bufsize);

Expand All @@ -102,7 +100,7 @@ JSONB_API jsonbcode jsonb_pop_object(jsonb *builder,
* @param len the key length
* @return @ref jsonbcode value
*/
JSONB_API jsonbcode jsonb_push_key(
JSONB_API jsonbcode jsonb_key(
jsonb *builder, char buf[], size_t bufsize, const char key[], size_t len);

/**
Expand All @@ -113,9 +111,7 @@ JSONB_API jsonbcode jsonb_push_key(
* @param bufsize the JSON buffer size
* @return @ref jsonbcode value
*/
JSONB_API jsonbcode jsonb_push_array(jsonb *builder,
char buf[],
size_t bufsize);
JSONB_API jsonbcode jsonb_array(jsonb *builder, char buf[], size_t bufsize);

/**
* @brief Pop an array from the builder
Expand All @@ -125,7 +121,7 @@ JSONB_API jsonbcode jsonb_push_array(jsonb *builder,
* @param bufsize the JSON buffer size
* @return @ref jsonbcode value
*/
JSONB_API jsonbcode jsonb_pop_array(jsonb *builder,
JSONB_API jsonbcode jsonb_array_pop(jsonb *builder,
char buf[],
size_t bufsize);

Expand All @@ -139,11 +135,11 @@ JSONB_API jsonbcode jsonb_pop_array(jsonb *builder,
* @param len the token length
* @return @ref jsonbcode value
*/
JSONB_API jsonbcode jsonb_push_token(jsonb *builder,
char buf[],
size_t bufsize,
const char token[],
size_t len);
JSONB_API jsonbcode jsonb_token(jsonb *builder,
char buf[],
size_t bufsize,
const char token[],
size_t len);

/**
* @brief Push a boolean token to the builder
Expand All @@ -154,10 +150,10 @@ JSONB_API jsonbcode jsonb_push_token(jsonb *builder,
* @param boolean the boolean to be inserted
* @return @ref jsonbcode value
*/
JSONB_API jsonbcode jsonb_push_bool(jsonb *builder,
char buf[],
size_t bufsize,
int boolean);
JSONB_API jsonbcode jsonb_bool(jsonb *builder,
char buf[],
size_t bufsize,
int boolean);

/**
* @brief Push a null token to the builder
Expand All @@ -167,9 +163,7 @@ JSONB_API jsonbcode jsonb_push_bool(jsonb *builder,
* @param bufsize the JSON buffer size
* @return @ref jsonbcode value
*/
JSONB_API jsonbcode jsonb_push_null(jsonb *builder,
char buf[],
size_t bufsize);
JSONB_API jsonbcode jsonb_null(jsonb *builder, char buf[], size_t bufsize);

/**
* @brief Push a string token to the builder
Expand All @@ -181,7 +175,7 @@ JSONB_API jsonbcode jsonb_push_null(jsonb *builder,
* @param len the string length
* @return @ref jsonbcode value
*/
JSONB_API jsonbcode jsonb_push_string(
JSONB_API jsonbcode jsonb_string(
jsonb *builder, char buf[], size_t bufsize, const char str[], size_t len);

/**
Expand All @@ -193,10 +187,10 @@ JSONB_API jsonbcode jsonb_push_string(
* @param number the number to be inserted
* @return @ref jsonbcode value
*/
JSONB_API jsonbcode jsonb_push_number(jsonb *builder,
char buf[],
size_t bufsize,
double number);
JSONB_API jsonbcode jsonb_number(jsonb *builder,
char buf[],
size_t bufsize,
double number);

#ifndef JSONB_HEADER
#include <stdio.h>
Expand All @@ -208,24 +202,15 @@ static const char *
_jsonb_eval_state(enum jsonbstate state)
{
switch (state) {
case JSONB_ARRAY_OR_OBJECT_OR_VALUE:
return "array or object or value";
case JSONB_OBJECT_KEY_OR_CLOSE:
return "object key or close";
case JSONB_OBJECT_NEXT_KEY_OR_CLOSE:
return "object next key or close";
case JSONB_OBJECT_VALUE:
return "object value";
case JSONB_ARRAY_VALUE_OR_CLOSE:
return "array value or close";
case JSONB_ARRAY_NEXT_VALUE_OR_CLOSE:
return "array next value or close";
case JSONB_ERROR:
return "error";
case JSONB_DONE:
return "done";
default:
return "unknown";
case JSONB_ARRAY_OR_OBJECT_OR_VALUE: return "array or object or value";
case JSONB_OBJECT_KEY_OR_CLOSE: return "object key or close";
case JSONB_OBJECT_NEXT_KEY_OR_CLOSE: return "object next key or close";
case JSONB_OBJECT_VALUE: return "object value";
case JSONB_ARRAY_VALUE_OR_CLOSE: return "array value or close";
case JSONB_ARRAY_NEXT_VALUE_OR_CLOSE: return "array next value or close";
case JSONB_ERROR: return "error";
case JSONB_DONE: return "done";
default: return "unknown";
}
}
#define TRACE(prev, next) \
Expand Down Expand Up @@ -270,7 +255,7 @@ jsonb_init(jsonb *b)
}

jsonbcode
jsonb_push_object(jsonb *b, char buf[], size_t bufsize)
jsonb_object(jsonb *b, char buf[], size_t bufsize)
{
enum jsonbstate new_state;
size_t pos = 0;
Expand Down Expand Up @@ -303,7 +288,7 @@ jsonb_push_object(jsonb *b, char buf[], size_t bufsize)
}

jsonbcode
jsonb_pop_object(jsonb *b, char buf[], size_t bufsize)
jsonb_object_pop(jsonb *b, char buf[], size_t bufsize)
{
enum jsonbcode code;
size_t pos = 0;
Expand Down Expand Up @@ -388,8 +373,7 @@ _jsonb_escape(
}

jsonbcode
jsonb_push_key(
jsonb *b, char buf[], size_t bufsize, const char key[], size_t len)
jsonb_key(jsonb *b, char buf[], size_t bufsize, const char key[], size_t len)
{
size_t pos = 0;
switch (*b->top) {
Expand All @@ -415,7 +399,7 @@ jsonb_push_key(
}

jsonbcode
jsonb_push_array(jsonb *b, char buf[], size_t bufsize)
jsonb_array(jsonb *b, char buf[], size_t bufsize)
{
enum jsonbstate new_state;
size_t pos = 0;
Expand Down Expand Up @@ -448,7 +432,7 @@ jsonb_push_array(jsonb *b, char buf[], size_t bufsize)
}

jsonbcode
jsonb_pop_array(jsonb *b, char buf[], size_t bufsize)
jsonb_array_pop(jsonb *b, char buf[], size_t bufsize)
{
enum jsonbcode code;
size_t pos = 0;
Expand All @@ -471,7 +455,7 @@ jsonb_pop_array(jsonb *b, char buf[], size_t bufsize)
}

jsonbcode
jsonb_push_token(
jsonb_token(
jsonb *b, char buf[], size_t bufsize, const char token[], size_t len)
{
enum jsonbstate next_state;
Expand Down Expand Up @@ -507,20 +491,20 @@ jsonb_push_token(
}

jsonbcode
jsonb_push_bool(jsonb *b, char buf[], size_t bufsize, int boolean)
jsonb_bool(jsonb *b, char buf[], size_t bufsize, int boolean)
{
if (boolean) return jsonb_push_token(b, buf, bufsize, "true", 4);
return jsonb_push_token(b, buf, bufsize, "false", 5);
if (boolean) return jsonb_token(b, buf, bufsize, "true", 4);
return jsonb_token(b, buf, bufsize, "false", 5);
}

jsonbcode
jsonb_push_null(jsonb *b, char buf[], size_t bufsize)
jsonb_null(jsonb *b, char buf[], size_t bufsize)
{
return jsonb_push_token(b, buf, bufsize, "null", 4);
return jsonb_token(b, buf, bufsize, "null", 4);
}

jsonbcode
jsonb_push_string(
jsonb_string(
jsonb *b, char buf[], size_t bufsize, const char str[], size_t len)
{
enum jsonbstate next_state;
Expand Down Expand Up @@ -559,12 +543,12 @@ jsonb_push_string(
}

jsonbcode
jsonb_push_number(jsonb *b, char buf[], size_t bufsize, double number)
jsonb_number(jsonb *b, char buf[], size_t bufsize, double number)
{
char token[32];
long len = sprintf(token, "%.17G", number);
if (len < 0) return JSONB_ERROR_INPUT;
return jsonb_push_token(b, buf, bufsize, token, len);
return jsonb_token(b, buf, bufsize, token, len);
}
#endif /* JSONB_HEADER */

Expand Down
18 changes: 9 additions & 9 deletions test/fuzz.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,43 +33,43 @@ main(void)

switch (c % 9) {
case 0:
r = jsonb_push_object(b, buf, z);
r = jsonb_object(b, buf, z);
break;
case 1:
r = jsonb_pop_object(b, buf, z);
r = jsonb_object_pop(b, buf, z);
break;
case 2:
if (arg > cmdlen - i - 1) {
r = JSONB_ERROR_INPUT;
}
else {
r = jsonb_push_key(b, buf, z, cmd + i + 1, arg);
r = jsonb_key(b, buf, z, cmd + i + 1, arg);
i += arg;
}
break;
case 3:
r = jsonb_push_array(b, buf, z);
r = jsonb_array(b, buf, z);
break;
case 4:
r = jsonb_pop_array(b, buf, z);
r = jsonb_array_pop(b, buf, z);
break;
case 5:
r = jsonb_push_bool(b, buf, z, arg % 2);
r = jsonb_bool(b, buf, z, arg % 2);
break;
case 6:
r = jsonb_push_null(b, buf, z);
r = jsonb_null(b, buf, z);
break;
case 7:
if (arg > cmdlen - i - 1) {
r = JSONB_ERROR_INPUT;
}
else {
r = jsonb_push_string(b, buf, z, cmd + i + 1, arg);
r = jsonb_string(b, buf, z, cmd + i + 1, arg);
i += arg;
}
break;
case 8:
r = jsonb_push_number(b, buf, z, arg / 28.0);
r = jsonb_number(b, buf, z, arg / 28.0);
break;
}

Expand Down
Loading

0 comments on commit 9b97152

Please sign in to comment.