Skip to content

Commit 9b97152

Browse files
committed
feat!: shorten symbols
1 parent 9a4e551 commit 9b97152

File tree

4 files changed

+147
-163
lines changed

4 files changed

+147
-163
lines changed

README.md

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,18 @@ jsonb b;
2424
char buf[1024];
2525

2626
jsonb_init(&b);
27-
jsonb_push_object(&b, buf, sizeof(buf));
27+
jsonb_object(&b, buf, sizeof(buf));
2828
{
29-
jsonb_push_key(&b, buf, sizeof(buf), "foo", strlen("foo"));
30-
jsonb_push_array(&b, buf, sizeof(buf));
29+
jsonb_key(&b, buf, sizeof(buf), "foo", strlen("foo"));
30+
jsonb_array(&b, buf, sizeof(buf));
3131
{
32-
jsonb_push_number(&b, buf, sizeof(buf), 1);
33-
jsonb_push_string(&b, buf, sizeof(buf), "hi", 2);
34-
jsonb_push_bool(&b, buf, sizeof(buf), 0);
35-
jsonb_push_null(&b, buf, sizeof(buf));
36-
jsonb_pop_array(&b, buf, sizeof(buf));
32+
jsonb_number(&b, buf, sizeof(buf), 1);
33+
jsonb_string(&b, buf, sizeof(buf), "hi", 2);
34+
jsonb_bool(&b, buf, sizeof(buf), 0);
35+
jsonb_null(&b, buf, sizeof(buf));
36+
jsonb_array_pop(&b, buf, sizeof(buf));
3737
}
38-
jsonb_pop_object(&b, buf, sizeof(buf));
38+
jsonb_object_pop(&b, buf, sizeof(buf));
3939
}
4040
printf("JSON: %s", buf); // JSON: {"foo":[1,"hi",false,null]}
4141
```
@@ -58,16 +58,16 @@ API
5858
---
5959

6060
* `jsonb_init()` - initialize a jsonb handle
61-
* `jsonb_push_object()` - push an object to the builder stack
62-
* `jsonb_pop_object()` - pop an object from the builder stack
63-
* `jsonb_push_key()` - push an object key field to the builder stack
64-
* `jsonb_push_array()` - push an array to the builder stack
65-
* `jsonb_pop_array()` - pop an array from the builder stack
66-
* `jsonb_push_token()` - push a raw token to the builder stack
67-
* `jsonb_push_bool()` - push a boolean token to the builder stack
68-
* `jsonb_push_null()` - push a null token to the builder stack
69-
* `jsonb_push_string()` - push a string token to the builder stack
70-
* `jsonb_push_number()` - push a number token to the builder stack
61+
* `jsonb_object()` - push an object to the builder stack
62+
* `jsonb_object_pop()` - pop an object from the builder stack
63+
* `jsonb_key()` - push an object key field to the builder stack
64+
* `jsonb_array()` - push an array to the builder stack
65+
* `jsonb_array_pop()` - pop an array from the builder stack
66+
* `jsonb_token()` - push a raw token to the builder stack
67+
* `jsonb_bool()` - push a boolean token to the builder stack
68+
* `jsonb_null()` - push a null token to the builder stack
69+
* `jsonb_string()` - push a string token to the builder stack
70+
* `jsonb_number()` - push a number token to the builder stack
7171

7272
The following are the possible return codes for the builder functions:
7373
* `JSONB_OK` - operation was a success, user can proceed with the next operation

json-build.h

Lines changed: 43 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@ JSONB_API void jsonb_init(jsonb *builder);
7676
* @param bufsize the JSON buffer size
7777
* @return @ref jsonbcode value
7878
*/
79-
JSONB_API jsonbcode jsonb_push_object(jsonb *builder,
80-
char buf[],
81-
size_t bufsize);
79+
JSONB_API jsonbcode jsonb_object(jsonb *builder, char buf[], size_t bufsize);
8280

8381
/**
8482
* @brief Pop an object from the builder
@@ -88,7 +86,7 @@ JSONB_API jsonbcode jsonb_push_object(jsonb *builder,
8886
* @param bufsize the JSON buffer size
8987
* @return @ref jsonbcode value
9088
*/
91-
JSONB_API jsonbcode jsonb_pop_object(jsonb *builder,
89+
JSONB_API jsonbcode jsonb_object_pop(jsonb *builder,
9290
char buf[],
9391
size_t bufsize);
9492

@@ -102,7 +100,7 @@ JSONB_API jsonbcode jsonb_pop_object(jsonb *builder,
102100
* @param len the key length
103101
* @return @ref jsonbcode value
104102
*/
105-
JSONB_API jsonbcode jsonb_push_key(
103+
JSONB_API jsonbcode jsonb_key(
106104
jsonb *builder, char buf[], size_t bufsize, const char key[], size_t len);
107105

108106
/**
@@ -113,9 +111,7 @@ JSONB_API jsonbcode jsonb_push_key(
113111
* @param bufsize the JSON buffer size
114112
* @return @ref jsonbcode value
115113
*/
116-
JSONB_API jsonbcode jsonb_push_array(jsonb *builder,
117-
char buf[],
118-
size_t bufsize);
114+
JSONB_API jsonbcode jsonb_array(jsonb *builder, char buf[], size_t bufsize);
119115

120116
/**
121117
* @brief Pop an array from the builder
@@ -125,7 +121,7 @@ JSONB_API jsonbcode jsonb_push_array(jsonb *builder,
125121
* @param bufsize the JSON buffer size
126122
* @return @ref jsonbcode value
127123
*/
128-
JSONB_API jsonbcode jsonb_pop_array(jsonb *builder,
124+
JSONB_API jsonbcode jsonb_array_pop(jsonb *builder,
129125
char buf[],
130126
size_t bufsize);
131127

@@ -139,11 +135,11 @@ JSONB_API jsonbcode jsonb_pop_array(jsonb *builder,
139135
* @param len the token length
140136
* @return @ref jsonbcode value
141137
*/
142-
JSONB_API jsonbcode jsonb_push_token(jsonb *builder,
143-
char buf[],
144-
size_t bufsize,
145-
const char token[],
146-
size_t len);
138+
JSONB_API jsonbcode jsonb_token(jsonb *builder,
139+
char buf[],
140+
size_t bufsize,
141+
const char token[],
142+
size_t len);
147143

148144
/**
149145
* @brief Push a boolean token to the builder
@@ -154,10 +150,10 @@ JSONB_API jsonbcode jsonb_push_token(jsonb *builder,
154150
* @param boolean the boolean to be inserted
155151
* @return @ref jsonbcode value
156152
*/
157-
JSONB_API jsonbcode jsonb_push_bool(jsonb *builder,
158-
char buf[],
159-
size_t bufsize,
160-
int boolean);
153+
JSONB_API jsonbcode jsonb_bool(jsonb *builder,
154+
char buf[],
155+
size_t bufsize,
156+
int boolean);
161157

162158
/**
163159
* @brief Push a null token to the builder
@@ -167,9 +163,7 @@ JSONB_API jsonbcode jsonb_push_bool(jsonb *builder,
167163
* @param bufsize the JSON buffer size
168164
* @return @ref jsonbcode value
169165
*/
170-
JSONB_API jsonbcode jsonb_push_null(jsonb *builder,
171-
char buf[],
172-
size_t bufsize);
166+
JSONB_API jsonbcode jsonb_null(jsonb *builder, char buf[], size_t bufsize);
173167

174168
/**
175169
* @brief Push a string token to the builder
@@ -181,7 +175,7 @@ JSONB_API jsonbcode jsonb_push_null(jsonb *builder,
181175
* @param len the string length
182176
* @return @ref jsonbcode value
183177
*/
184-
JSONB_API jsonbcode jsonb_push_string(
178+
JSONB_API jsonbcode jsonb_string(
185179
jsonb *builder, char buf[], size_t bufsize, const char str[], size_t len);
186180

187181
/**
@@ -193,10 +187,10 @@ JSONB_API jsonbcode jsonb_push_string(
193187
* @param number the number to be inserted
194188
* @return @ref jsonbcode value
195189
*/
196-
JSONB_API jsonbcode jsonb_push_number(jsonb *builder,
197-
char buf[],
198-
size_t bufsize,
199-
double number);
190+
JSONB_API jsonbcode jsonb_number(jsonb *builder,
191+
char buf[],
192+
size_t bufsize,
193+
double number);
200194

201195
#ifndef JSONB_HEADER
202196
#include <stdio.h>
@@ -208,24 +202,15 @@ static const char *
208202
_jsonb_eval_state(enum jsonbstate state)
209203
{
210204
switch (state) {
211-
case JSONB_ARRAY_OR_OBJECT_OR_VALUE:
212-
return "array or object or value";
213-
case JSONB_OBJECT_KEY_OR_CLOSE:
214-
return "object key or close";
215-
case JSONB_OBJECT_NEXT_KEY_OR_CLOSE:
216-
return "object next key or close";
217-
case JSONB_OBJECT_VALUE:
218-
return "object value";
219-
case JSONB_ARRAY_VALUE_OR_CLOSE:
220-
return "array value or close";
221-
case JSONB_ARRAY_NEXT_VALUE_OR_CLOSE:
222-
return "array next value or close";
223-
case JSONB_ERROR:
224-
return "error";
225-
case JSONB_DONE:
226-
return "done";
227-
default:
228-
return "unknown";
205+
case JSONB_ARRAY_OR_OBJECT_OR_VALUE: return "array or object or value";
206+
case JSONB_OBJECT_KEY_OR_CLOSE: return "object key or close";
207+
case JSONB_OBJECT_NEXT_KEY_OR_CLOSE: return "object next key or close";
208+
case JSONB_OBJECT_VALUE: return "object value";
209+
case JSONB_ARRAY_VALUE_OR_CLOSE: return "array value or close";
210+
case JSONB_ARRAY_NEXT_VALUE_OR_CLOSE: return "array next value or close";
211+
case JSONB_ERROR: return "error";
212+
case JSONB_DONE: return "done";
213+
default: return "unknown";
229214
}
230215
}
231216
#define TRACE(prev, next) \
@@ -270,7 +255,7 @@ jsonb_init(jsonb *b)
270255
}
271256

272257
jsonbcode
273-
jsonb_push_object(jsonb *b, char buf[], size_t bufsize)
258+
jsonb_object(jsonb *b, char buf[], size_t bufsize)
274259
{
275260
enum jsonbstate new_state;
276261
size_t pos = 0;
@@ -303,7 +288,7 @@ jsonb_push_object(jsonb *b, char buf[], size_t bufsize)
303288
}
304289

305290
jsonbcode
306-
jsonb_pop_object(jsonb *b, char buf[], size_t bufsize)
291+
jsonb_object_pop(jsonb *b, char buf[], size_t bufsize)
307292
{
308293
enum jsonbcode code;
309294
size_t pos = 0;
@@ -388,8 +373,7 @@ _jsonb_escape(
388373
}
389374

390375
jsonbcode
391-
jsonb_push_key(
392-
jsonb *b, char buf[], size_t bufsize, const char key[], size_t len)
376+
jsonb_key(jsonb *b, char buf[], size_t bufsize, const char key[], size_t len)
393377
{
394378
size_t pos = 0;
395379
switch (*b->top) {
@@ -415,7 +399,7 @@ jsonb_push_key(
415399
}
416400

417401
jsonbcode
418-
jsonb_push_array(jsonb *b, char buf[], size_t bufsize)
402+
jsonb_array(jsonb *b, char buf[], size_t bufsize)
419403
{
420404
enum jsonbstate new_state;
421405
size_t pos = 0;
@@ -448,7 +432,7 @@ jsonb_push_array(jsonb *b, char buf[], size_t bufsize)
448432
}
449433

450434
jsonbcode
451-
jsonb_pop_array(jsonb *b, char buf[], size_t bufsize)
435+
jsonb_array_pop(jsonb *b, char buf[], size_t bufsize)
452436
{
453437
enum jsonbcode code;
454438
size_t pos = 0;
@@ -471,7 +455,7 @@ jsonb_pop_array(jsonb *b, char buf[], size_t bufsize)
471455
}
472456

473457
jsonbcode
474-
jsonb_push_token(
458+
jsonb_token(
475459
jsonb *b, char buf[], size_t bufsize, const char token[], size_t len)
476460
{
477461
enum jsonbstate next_state;
@@ -507,20 +491,20 @@ jsonb_push_token(
507491
}
508492

509493
jsonbcode
510-
jsonb_push_bool(jsonb *b, char buf[], size_t bufsize, int boolean)
494+
jsonb_bool(jsonb *b, char buf[], size_t bufsize, int boolean)
511495
{
512-
if (boolean) return jsonb_push_token(b, buf, bufsize, "true", 4);
513-
return jsonb_push_token(b, buf, bufsize, "false", 5);
496+
if (boolean) return jsonb_token(b, buf, bufsize, "true", 4);
497+
return jsonb_token(b, buf, bufsize, "false", 5);
514498
}
515499

516500
jsonbcode
517-
jsonb_push_null(jsonb *b, char buf[], size_t bufsize)
501+
jsonb_null(jsonb *b, char buf[], size_t bufsize)
518502
{
519-
return jsonb_push_token(b, buf, bufsize, "null", 4);
503+
return jsonb_token(b, buf, bufsize, "null", 4);
520504
}
521505

522506
jsonbcode
523-
jsonb_push_string(
507+
jsonb_string(
524508
jsonb *b, char buf[], size_t bufsize, const char str[], size_t len)
525509
{
526510
enum jsonbstate next_state;
@@ -559,12 +543,12 @@ jsonb_push_string(
559543
}
560544

561545
jsonbcode
562-
jsonb_push_number(jsonb *b, char buf[], size_t bufsize, double number)
546+
jsonb_number(jsonb *b, char buf[], size_t bufsize, double number)
563547
{
564548
char token[32];
565549
long len = sprintf(token, "%.17G", number);
566550
if (len < 0) return JSONB_ERROR_INPUT;
567-
return jsonb_push_token(b, buf, bufsize, token, len);
551+
return jsonb_token(b, buf, bufsize, token, len);
568552
}
569553
#endif /* JSONB_HEADER */
570554

test/fuzz.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,43 +33,43 @@ main(void)
3333

3434
switch (c % 9) {
3535
case 0:
36-
r = jsonb_push_object(b, buf, z);
36+
r = jsonb_object(b, buf, z);
3737
break;
3838
case 1:
39-
r = jsonb_pop_object(b, buf, z);
39+
r = jsonb_object_pop(b, buf, z);
4040
break;
4141
case 2:
4242
if (arg > cmdlen - i - 1) {
4343
r = JSONB_ERROR_INPUT;
4444
}
4545
else {
46-
r = jsonb_push_key(b, buf, z, cmd + i + 1, arg);
46+
r = jsonb_key(b, buf, z, cmd + i + 1, arg);
4747
i += arg;
4848
}
4949
break;
5050
case 3:
51-
r = jsonb_push_array(b, buf, z);
51+
r = jsonb_array(b, buf, z);
5252
break;
5353
case 4:
54-
r = jsonb_pop_array(b, buf, z);
54+
r = jsonb_array_pop(b, buf, z);
5555
break;
5656
case 5:
57-
r = jsonb_push_bool(b, buf, z, arg % 2);
57+
r = jsonb_bool(b, buf, z, arg % 2);
5858
break;
5959
case 6:
60-
r = jsonb_push_null(b, buf, z);
60+
r = jsonb_null(b, buf, z);
6161
break;
6262
case 7:
6363
if (arg > cmdlen - i - 1) {
6464
r = JSONB_ERROR_INPUT;
6565
}
6666
else {
67-
r = jsonb_push_string(b, buf, z, cmd + i + 1, arg);
67+
r = jsonb_string(b, buf, z, cmd + i + 1, arg);
6868
i += arg;
6969
}
7070
break;
7171
case 8:
72-
r = jsonb_push_number(b, buf, z, arg / 28.0);
72+
r = jsonb_number(b, buf, z, arg / 28.0);
7373
break;
7474
}
7575

0 commit comments

Comments
 (0)