-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgdnode.h
More file actions
174 lines (123 loc) · 3.67 KB
/
gdnode.h
File metadata and controls
174 lines (123 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#ifndef _GDNODE_H_
#define _GDNODE_H_
#include <stdlib.h>
#include <string.h>
#include <Python.h>
#include <stdbool.h>
#include "b64.h"
#include "yyjson/yyjson.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct buffer_s {
size_t len;
char* buf;
} buffer_t;
typedef struct gd_node_s {
size_t pos; /* The string's current position on the parsing phase...*/
buffer_t* buf; /* An arrary of 30 buffers to use */
char* raw; /* the raw robtop string to parse through */
} gd_node_t;
void instert_slot(buffer_t* buff,const char* obj , size_t size){
memcpy(buff->buf, obj, size);
buff->len = size;
}
PyObject* from_buffer(buffer_t* buff){
return PyBytes_FromStringAndSize(buff->buf, (Py_ssize_t)buff->len);
}
/* All of these can be ran without the gil if required... */
/* 176 is my magic number to take in the limit of base64 comments (body) which is the longest size possible */
int give_memory(buffer_t* buff){
buff->buf = (char*)malloc(sizeof(char) * 200);
if (buff == NULL)
return -1;
buff->len = 200;
return 0;
}
void reset_memory(buffer_t* buffer){
memset(buffer->buf, 0, buffer->len);
buffer->len = 0;
}
void free_memory(buffer_t* buff){
free(buff->buf);
}
/* 2~bunchofcrap~3~etc...*/
int parse_next(gd_node_t* node){
const char* p = (const char*)node->raw + node->pos;
const char* lastpos = p;
const char* startpos;
size_t slot = 0, offset = 0;
/* start parsing */
parse_key:
switch (*p) {
case '~':
p++;
startpos = p;
goto parse_value;
default:
slot = (slot * 10) + (*p - 48);
p++;
goto parse_key;
}
/* parse the value */
parse_value:
switch (*p) {
case '~':
instert_slot(&node->buf[slot + offset], startpos, (size_t)(p - startpos));
p++;
slot = 0;
goto parse_key;
case ':':
/* insert items... */
instert_slot(&node->buf[slot + offset], startpos, (size_t)(p - startpos));
p++;
offset = 13;
slot = 0;
goto parse_key;
case '|':
instert_slot(&node->buf[slot + offset], startpos, (size_t)(p - startpos));
p++;
slot = 0;
goto finalize;
case '#':
instert_slot(&node->buf[slot + offset], startpos, (size_t)(p - startpos));
p++;
slot = 0;
goto finish;
default:
p++;
goto parse_value;
}
/* we will be allowed to parse more and return the length correctly the other does
returns -1 which means we have no more comments left and we need to move to the pagesum...*/
finalize:
node->pos += (size_t)(p - lastpos);
return 0;
/* now were really done... */
finish:
node->pos += (size_t)(p - lastpos);
return -1;
}
bool fast_boolean(buffer_t* buff){
return (buff->buf[0] == 49) ? true: false;
}
Py_ssize_t fast_atoi(buffer_t * buff){
Py_ssize_t result = 0;
size_t i = 0;
int _final = 1;
if (buff->buf[0] == '-'){
_final = -1;
i++;
}
for (i; i < buff->len; i++)
result = (result * 10) + (buff->buf[i] - 48);
return result * _final;
}
/* ternary is ? true:false */
Py_ssize_t fast_ternary(buffer_t * buff){
return (buff->buf[0] == 0) ? fast_atoi(buff) : 0;
}
#ifdef __cplusplus
}
#endif
#endif /* _GDNODE_H_ */