[utils] Adding source string to tokenized_string struct
This commit is contained in:
25
src/tokens.c
25
src/tokens.c
@@ -3,17 +3,27 @@
|
|||||||
|
|
||||||
tokenized_string_t *tokenized_string_new(void) {
|
tokenized_string_t *tokenized_string_new(void) {
|
||||||
tokenized_string_t *self = malloc(sizeof(tokenized_string_t));
|
tokenized_string_t *self = malloc(sizeof(tokenized_string_t));
|
||||||
self->str = cstring_array_new();
|
self->str = NULL;
|
||||||
|
self->strings = cstring_array_new();
|
||||||
self->tokens = token_array_new();
|
self->tokens = token_array_new();
|
||||||
|
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
tokenized_string_t *tokenized_string_new_size(size_t len, size_t num_tokens) {
|
||||||
|
tokenized_string_t *self = malloc(sizeof(tokenized_string_t));
|
||||||
|
self->str = NULL;
|
||||||
|
self->strings = cstring_array_new_size(len);
|
||||||
|
self->tokens = token_array_new_size(len);
|
||||||
|
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
void tokenized_string_add_token(tokenized_string_t *self, const char *src, size_t len, uint16_t token_type, size_t position) {
|
void tokenized_string_add_token(tokenized_string_t *self, const char *src, size_t len, uint16_t token_type, size_t position) {
|
||||||
char *ptr = (char *) (src + position);
|
char *ptr = (char *) (src + position);
|
||||||
|
|
||||||
cstring_array_add_string_len(self->str, ptr, len);
|
cstring_array_add_string_len(self->strings, ptr, len);
|
||||||
|
|
||||||
token_t token = (token_t){position, len, token_type};
|
token_t token = (token_t){position, len, token_type};
|
||||||
token_array_push(self->tokens, token);
|
token_array_push(self->tokens, token);
|
||||||
@@ -22,7 +32,8 @@ void tokenized_string_add_token(tokenized_string_t *self, const char *src, size_
|
|||||||
|
|
||||||
tokenized_string_t *tokenized_string_from_tokens(char *src, token_array *tokens, bool copy_tokens) {
|
tokenized_string_t *tokenized_string_from_tokens(char *src, token_array *tokens, bool copy_tokens) {
|
||||||
tokenized_string_t *self = malloc(sizeof(tokenized_string_t));
|
tokenized_string_t *self = malloc(sizeof(tokenized_string_t));
|
||||||
self->str = cstring_array_new_size(strlen(src) + tokens->n);
|
self->str = src;
|
||||||
|
self->strings = cstring_array_new_size(strlen(src) + tokens->n);
|
||||||
if (copy_tokens) {
|
if (copy_tokens) {
|
||||||
self->tokens = token_array_new_copy(tokens, tokens->n);
|
self->tokens = token_array_new_copy(tokens, tokens->n);
|
||||||
} else {
|
} else {
|
||||||
@@ -33,7 +44,7 @@ tokenized_string_t *tokenized_string_from_tokens(char *src, token_array *tokens,
|
|||||||
|
|
||||||
for (int i = 0; i < tokens->n; i++) {
|
for (int i = 0; i < tokens->n; i++) {
|
||||||
token = tokens->a[i];
|
token = tokens->a[i];
|
||||||
cstring_array_add_string_len(self->str, src + token.offset, token.len);
|
cstring_array_add_string_len(self->strings, src + token.offset, token.len);
|
||||||
}
|
}
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
@@ -41,7 +52,7 @@ tokenized_string_t *tokenized_string_from_tokens(char *src, token_array *tokens,
|
|||||||
|
|
||||||
char *tokenized_string_get_token(tokenized_string_t *self, uint32_t index) {
|
char *tokenized_string_get_token(tokenized_string_t *self, uint32_t index) {
|
||||||
if (index < self->tokens->n) {
|
if (index < self->tokens->n) {
|
||||||
return cstring_array_get_string(self->str, index);
|
return cstring_array_get_string(self->strings, index);
|
||||||
} else {
|
} else {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
@@ -50,8 +61,8 @@ char *tokenized_string_get_token(tokenized_string_t *self, uint32_t index) {
|
|||||||
void tokenized_string_destroy(tokenized_string_t *self) {
|
void tokenized_string_destroy(tokenized_string_t *self) {
|
||||||
if (!self)
|
if (!self)
|
||||||
return;
|
return;
|
||||||
if (self->str)
|
if (self->strings)
|
||||||
cstring_array_destroy(self->str);
|
cstring_array_destroy(self->strings);
|
||||||
if (self->tokens)
|
if (self->tokens)
|
||||||
token_array_destroy(self->tokens);
|
token_array_destroy(self->tokens);
|
||||||
free(self);
|
free(self);
|
||||||
|
|||||||
@@ -21,11 +21,13 @@ typedef struct token {
|
|||||||
VECTOR_INIT(token_array, token_t)
|
VECTOR_INIT(token_array, token_t)
|
||||||
|
|
||||||
typedef struct tokenized_string {
|
typedef struct tokenized_string {
|
||||||
cstring_array *str;
|
char *str;
|
||||||
|
cstring_array *strings;
|
||||||
token_array *tokens;
|
token_array *tokens;
|
||||||
} tokenized_string_t;
|
} tokenized_string_t;
|
||||||
|
|
||||||
tokenized_string_t *tokenized_string_new(void);
|
tokenized_string_t *tokenized_string_new(void);
|
||||||
|
tokenized_string_t *tokenized_string_new_size(size_t len, size_t num_tokens);
|
||||||
tokenized_string_t *tokenized_string_from_tokens(char *src, token_array *tokens, bool copy_tokens);
|
tokenized_string_t *tokenized_string_from_tokens(char *src, token_array *tokens, bool copy_tokens);
|
||||||
void tokenized_string_add_token(tokenized_string_t *self, const char *src, size_t len, uint16_t token_type, size_t position);
|
void tokenized_string_add_token(tokenized_string_t *self, const char *src, size_t len, uint16_t token_type, size_t position);
|
||||||
char *tokenized_string_get_token(tokenized_string_t *self, uint32_t index);
|
char *tokenized_string_get_token(tokenized_string_t *self, uint32_t index);
|
||||||
|
|||||||
Reference in New Issue
Block a user