-
Notifications
You must be signed in to change notification settings - Fork 0
/
tbuf_data.h
123 lines (109 loc) · 3.31 KB
/
tbuf_data.h
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
#ifndef TURBO_BUF_DATA_H
#define TURBO_BUF_DATA_H
#include<memory>
#include<vector>
#include<cstdio>
#include<cstring>
#include<cctype>
#include<functional>
#include<initializer_list>
#include<unordered_set>
#include"fio_data.h"
// Uncomment this if we want to see the debug logging
// Or even better: define this before including us...
//#define DEBUG_LOG 1
namespace tbuf {
/** Hex-data stream class */
class Hexes {
public:
/**
* Contains the data hex string (can be empty length). It is represented in the "most significant bytes first" way!
*/
fio::LenString digits;
/** Try to return the integral representation of this hex stream if possible */
inline unsigned long long asIntegral() {
// This could be optimized to do 4byte or 8byte processing in loop I think...
long long result = 0;
unsigned int len = digits.length;
for(unsigned int i = 0; i < len; ++i) {
result = (result << 4) + Hexes::hexValueOf(digits.startPtr[i]);
}
return result;
}
/** Try to return the unsigned int representation of this hex stream if possible */
inline unsigned int asUint() {
// This could be optimized to do 4byte or 8byte processing in loop I think...
unsigned int result = 0;
unsigned int len = digits.length;
for(unsigned int i = 0; i < len; ++i) {
result = (result << 4) + Hexes::hexValueOf(digits.startPtr[i]);
}
return result;
}
inline bool isEmpty() {
return (digits.length == 0);
}
/** Converts a character of [0..9] or [A..F] to its [0..15] integer range */
inline static char hexValueOf(char hex) {
if('A' <= hex && hex <= 'F') {
// Hexit
return (hex - 'A') + 10;
} else {
// Digit
return (hex - '0');
}
}
/** Tells if a character is among ['0'..'9'] or ['A'..'F'] */
inline static bool isHexCharacter(char c) {
// TODO: check if there is a faster way for ascii using bit hackz...
if(('A' <= c && c <= 'F') || ('0' <= c && c <= '9')) {
return true;
} else {
return false;
}
}
/** Return the empty hexes */
inline static Hexes EMPTY_HEXES() {
return {{0, nullptr}};
}
};
/**
* Defines possible node kinds
*/
enum class NodeKind {
/** An empty node indicating we cannot read anything */
EMPTY = 0,
/** The root-node - it is a special normal node */
ROOT = 1,
/** Just a normal node */
NORM = 2,
/** Text-containing ${str} nodes */
TEXT = 3,
};
/**
* The generic core of a turbo-buf tree-node. Do not cache the values that are owned by the tbuf trees to avoid shooting your leg!
* This is what the user should see when he is providing callbacks to walk over the tree or get data.
*/
struct NodeCore {
public:
/** Defines the kind of this node */
NodeKind nodeKind;
/**
* Contains the data hex string (can be empty length)
* - DO NOT USE DANGEROUS AND DESTRUCTIVE OPERATIONS ON THIS - Underlying memory is OWNED BY THE TREE! Do not cache this!
*/
Hexes data;
/**
* Contains the name of the node (different nodes might share the same name)
* - MEMORY IS OWNED BY THE TREE! Do not cache this!
*/
const char *name;
/**
* Only contains a valid pointer if the node kind is TEXT when it contains the utf8 char string. Otherwise nullptr.
* Also a nullptr if the node text is empty (so instead of "", we use nullptr).
* - MEMORY IS OWNED BY THE TREE! Do not cache this!
*/
const char *text;
};
} // end of namespace tbuf
#endif // TURBO_BUF_DATA_H