-
Notifications
You must be signed in to change notification settings - Fork 518
/
toksep.h
50 lines (44 loc) · 1.45 KB
/
toksep.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
/*
* Copyright Redis Ltd. 2016 - present
* Licensed under your choice of the Redis Source Available License 2.0 (RSALv2) or
* the Server Side Public License v1 (SSPLv1).
*/
#ifndef TOKSEP_H
#define TOKSEP_H
#include <stdint.h>
#include <stdlib.h>
//! " # $ % & ' ( ) * + , - . / : ; < = > ? @ [ \ ] ^ ` { | } ~
static const char ToksepMap_g[256] = {
[' '] = 1, ['\t'] = 1, [','] = 1, ['.'] = 1, ['/'] = 1, ['('] = 1, [')'] = 1, ['{'] = 1,
['}'] = 1, ['['] = 1, [']'] = 1, [':'] = 1, [';'] = 1, ['~'] = 1, ['!'] = 1, ['@'] = 1,
['#'] = 1, ['$'] = 1, ['%'] = 1, ['^'] = 1, ['&'] = 1, ['*'] = 1, ['-'] = 1, ['='] = 1,
['+'] = 1, ['|'] = 1, ['\''] = 1, ['`'] = 1, ['"'] = 1, ['<'] = 1, ['>'] = 1, ['?'] = 1,
};
/**
* Function reads string pointed to by `s` and indicates the length of the next
* token in `tokLen`. `s` is set to NULL if this is the last token.
*/
static inline char *toksep(char **s, size_t *tokLen) {
uint8_t *pos = (uint8_t *)*s;
char *orig = *s;
int escaped = 0;
for (; *pos; ++pos) {
if (ToksepMap_g[*pos] && !escaped) {
*s = (char *)++pos;
*tokLen = ((char *)pos - orig) - 1;
if (!*pos) {
*s = NULL;
}
return orig;
}
escaped = !escaped && *pos == '\\';
}
// Didn't find a terminating token. Use a simpler length calculation
*s = NULL;
*tokLen = (char *)pos - orig;
return orig;
}
static inline int istoksep(int c) {
return ToksepMap_g[(uint8_t)c] != 0;
}
#endif