Skip to content

Commit c644f32

Browse files
committed
Remove compressed token feature
1 parent 0a9f9e0 commit c644f32

File tree

4 files changed

+6
-46
lines changed

4 files changed

+6
-46
lines changed

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,6 @@ return value indicates a failure. Possible failures:
8989
`JSTR_NOMEM (-2)` Token array is too small. Grow the array. Resume
9090
the parser by calling `jstr_parse` again.
9191

92-
`JSTR_2BIG (-3)` String is too large or too many tokens produced.
93-
9492
### Tokens
9593

9694
Token is an opaque `jstr_token_t` structure. No cleanup is necessary.

jstr-test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,11 @@ static void test(int lineno, const char *json, ssize_t rc_expected, ... ) {
5454
}
5555
if (type_expected&(JSTR_ARRAY|JSTR_OBJECT)) {
5656
size_t offset_expected = va_arg(ap, size_t);
57-
if (type_match && jstr__offset(token+i) != offset_expected) {
57+
if (type_match && (token+i)->value__ != offset_expected) {
5858
fprintf(
5959
stderr, "@%04d: [%03zu] %6s (expected) %zu != %zu\n",
6060
lineno, i, jstr_type_to_str(type_expected),
61-
offset_expected, jstr__offset(token + i)
61+
offset_expected, (token + i)->value__
6262
);
6363
}
6464
} else {

jstr.c

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@
33
static inline void token_init(
44
jstr_token_t *token, jstr_type_t type, uintptr_t value
55
) {
6-
#if JSTR_TOKEN_COMPRESSED
7-
token->type_and_value__ = value<<8 | type;
8-
#else
96
token->type__ = type;
107
token->value__ = value;
11-
#endif
128
}
139

1410
// > 0: success, end pos
@@ -28,9 +24,6 @@ ssize_t jstr_parse(
2824
enum {
2925
TOP, OBJECT_VALUE = JSTR_OBJECT, ARRAY_ITEM = JSTR_ARRAY, OBJECT_KEY
3026
} cs = token_parent == token_cur ? TOP : jstr_type(token_parent);
31-
#if JSTR_TOKEN_COMPRESSED
32-
if (((uintptr_t)-1>>8) <= token_count) return JSTR_2BIG;
33-
#endif
3427
parse_generic:
3528
if (token_end - token_cur < 2) {
3629
parser->parse_pos = (char *)p-str;
@@ -193,9 +186,6 @@ commit_token: {
193186
switch (cs) {
194187
case TOP:
195188
if (c) return JSTR_INVAL;
196-
#if JSTR_TOKEN_COMPRESSED
197-
if ((uintptr_t)p > ((uintptr_t)-1>>8)) return JSTR_2BIG;
198-
#endif
199189
return (char *)p-str-1;
200190
case OBJECT_KEY:
201191
if (c != ':') return JSTR_INVAL;
@@ -214,9 +204,8 @@ commit_token: {
214204

215205
pop_context: {
216206
jstr_token_t *token_grandparent = token_parent
217-
- jstr__offset(token_parent);
218-
token_init(
219-
token_parent, jstr_type(token_parent), token_cur-token_parent);
207+
- token_parent->value__;
208+
token_parent->value__ = token_cur-token_parent;
220209
cs = token_parent == token_grandparent
221210
? TOP : jstr_type(token_grandparent);
222211
token_parent = token_grandparent;

jstr.h

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,48 +13,22 @@ typedef enum {
1313
JSTR_NULL = 0x40
1414
} jstr_type_t;
1515

16-
#ifndef JSTR_TOKEN_COMPRESSED
17-
#if __x86_64__
18-
#define JSTR_TOKEN_COMPRESSED 1
19-
#endif
20-
#endif
21-
2216
typedef struct {
23-
#if JSTR_TOKEN_COMPRESSED
24-
uintptr_t type_and_value__;
25-
#else
2617
jstr_type_t type__;
2718
uintptr_t value__;
28-
#endif
2919
} jstr_token_t;
3020

3121
static inline jstr_type_t jstr_type(const jstr_token_t *token) {
32-
#if JSTR_TOKEN_COMPRESSED
33-
return 0xff & token->type_and_value__;
34-
#else
3522
return token->type__;
36-
#endif
3723
}
3824

3925
static inline const char *jstr_value(const jstr_token_t *token) {
40-
#if JSTR_TOKEN_COMPRESSED
41-
return (const char *)(token->type_and_value__ >> 8);
42-
#else
4326
return (const char *)token->value__;
44-
#endif
45-
}
46-
47-
static inline size_t jstr__offset(const jstr_token_t *token) {
48-
#if JSTR_TOKEN_COMPRESSED
49-
return token->type_and_value__ >> 8;
50-
#else
51-
return token->value__;
52-
#endif
5327
}
5428

5529
static inline const jstr_token_t *jstr_next(const jstr_token_t *token) {
56-
return token + ((jstr_type(token)&(JSTR_OBJECT|JSTR_ARRAY)) ?
57-
jstr__offset(token) : 1);
30+
return token + ((token->type__&(JSTR_OBJECT|JSTR_ARRAY)) ?
31+
token->value__ : 1);
5832
}
5933

6034
typedef struct {
@@ -72,7 +46,6 @@ static inline void jstr_init(jstr_parser_t *parser) {
7246
enum {
7347
JSTR_INVAL = -1, // parse error
7448
JSTR_NOMEM = -2, // token array too small
75-
JSTR_2BIG = -3 // not enough bits in token to store pointer/offset
7649
};
7750

7851
ssize_t jstr_parse(

0 commit comments

Comments
 (0)