forked from coderhare/cppjson
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.cpp
More file actions
44 lines (39 loc) · 1.06 KB
/
stack.cpp
File metadata and controls
44 lines (39 loc) · 1.06 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
//
// Created by Morisummer on 2021/12/16.
//
#include "stack.h"
stack_j::stack_j():stack(nullptr), top(0), json(nullptr), size(0){}
void* stack_j::push(size_t size){
void* ret;
assert(size > 0);
if (top + size >= this->size) {
if (this->size == 0)
this->size = PARSER_STACK_INIT_SIZE;
while (top + size >= this->size)
this->size += this->size >> 1; /* c->size * 1.5 */
stack = (char*)realloc(stack, this->size);
}
ret = stack + top;
top += size;
return ret;
}
void stack_j::parse_whitespace() {
while(*json == ' ' || *json == '\t' || *json == '\n' || *json == '\r') json++;
}
void* stack_j::pop(size_t size){
assert(this->top >= size);
return this->stack + (this->top -= size);
}
void stack_j::put_char(char c){
char * p = (char*)push(sizeof(char));
*p = c;
}
void stack_j::put_string(const string & s){
size_t len = s.size();
char * p = (char*)push(sizeof(char) * len);
for(const char & c: s) *p++ = c;
}
void stack_j::expect(char c) {
assert(c == *json);
json++;
}