4 # include "php_config.h"
11 #include <sys/param.h>
13 #include "parser_proc.h"
17 size_t psi_token_alloc_size(size_t token_len
, size_t fname_len
) {
18 return sizeof(struct psi_token
) + token_len
+ fname_len
+ 2;
21 struct psi_token
*psi_token_alloc(struct psi_parser
*P
) {
23 size_t token_len
, fname_len
;
26 if (P
->cur
< P
->tok
) {
31 token_len
= P
->cur
- P
->tok
;
32 fname_len
= strlen(P
->psi
.file
.fn
);
34 T
= calloc(1, psi_token_alloc_size(token_len
, fname_len
));
38 T
->file
= &T
->buf
[token_len
+ 1];
42 memcpy(T
->text
, P
->tok
, token_len
);
43 memcpy(T
->file
, P
->psi
.file
.fn
, fname_len
);
48 struct psi_token
*psi_token_copy(struct psi_token
*src
) {
49 size_t strct_len
= psi_token_alloc_size(src
->size
, strlen(src
->file
));
50 struct psi_token
*ptr
= malloc(strct_len
);
52 memcpy(ptr
, src
, strct_len
);
54 ptr
->text
= &ptr
->buf
[0];
55 ptr
->file
= &ptr
->buf
[ptr
->size
+ 1];
60 struct psi_token
*psi_token_cat(unsigned argc
, ...) {
63 struct psi_token
*T
= NULL
;
66 for (i
= 0; i
< argc
; ++i
) {
67 struct psi_token
*arg
= va_arg(argv
, struct psi_token
*);
70 size_t token_len
= T
->size
, fname_len
= strlen(T
->file
);
72 T
= realloc(T
, psi_token_alloc_size(T
->size
+= arg
->size
+ 1, fname_len
));
74 T
->file
= &T
->buf
[T
->size
+ 1];
75 T
->buf
[token_len
] = ' ';
76 memmove(&T
->buf
[T
->size
+ 1], &T
->buf
[token_len
+ 1], fname_len
+ 1);
77 memcpy(&T
->buf
[token_len
+ 1], arg
->text
, arg
->size
+ 1);
79 T
= psi_token_copy(arg
);
88 struct psi_token
*psi_token_append(struct psi_token
*T
, unsigned argc
, ...) {
93 for (i
= 0; i
< argc
; ++i
) {
94 char *str
= va_arg(argv
, char *);
95 size_t str_len
= strlen(str
), token_len
= T
->size
, fname_len
= strlen(T
->file
);
97 T
= realloc(T
, psi_token_alloc_size(T
->size
+= str_len
+ 1, fname_len
));
99 T
->file
= &T
->buf
[T
->size
+ 1];
100 T
->buf
[token_len
] = ' ';
101 memmove(&T
->buf
[T
->size
+ 1], &T
->buf
[token_len
+ 1], fname_len
+ 1);
102 memcpy(&T
->buf
[token_len
+ 1], str
, str_len
+ 1);
109 char *php_strtr(char *str
, size_t len
, char *str_from
, char *str_to
, size_t trlen
);
110 struct psi_token
*psi_token_translit(struct psi_token
*T
, char *from
, char *to
) {
111 php_strtr(T
->text
, T
->size
, from
, to
, MIN(strlen(from
), strlen(to
)));
115 static inline uint64_t psi_hash(char *digest_buf
, ...)
117 uint64_t hash
= 5381;
122 va_start(argv
, digest_buf
);
123 while ((ptr
= va_arg(argv
, const uint8_t *))) {
124 while ((c
= *ptr
++)) {
125 hash
= ((hash
<< 5) + hash
) + c
;
131 sprintf(digest_buf
, "%" PRIx64
, hash
);
137 uint64_t psi_token_hash(struct psi_token
*t
, char *digest_buf
) {
140 sprintf(loc_buf
, "%u%u", t
->line
, t
->col
);
141 return psi_hash(digest_buf
, t
->file
, loc_buf
, NULL
);