2014-05-14 23:52:45 -04:00
|
|
|
/*
|
|
|
|
* token.c
|
2014-06-27 01:24:40 -04:00
|
|
|
* Copyright (C) 2014 c9s <yoanlin93@gmail.com>
|
2014-05-14 23:52:45 -04:00
|
|
|
*
|
|
|
|
* Distributed under terms of the MIT license.
|
|
|
|
*/
|
2015-11-17 07:59:17 -05:00
|
|
|
#include "config.h"
|
2014-05-14 23:52:45 -04:00
|
|
|
#include <stdlib.h>
|
2014-05-15 00:47:14 -04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h>
|
2014-05-26 09:39:36 -04:00
|
|
|
#include "r3.h"
|
2015-11-17 08:17:18 -05:00
|
|
|
#include "r3_slug.h"
|
2014-05-28 09:08:06 -04:00
|
|
|
#include "str_array.h"
|
2014-05-20 13:50:15 -04:00
|
|
|
#include "zmalloc.h"
|
2016-03-21 22:23:37 -04:00
|
|
|
#include "memory.h"
|
2014-05-14 23:52:45 -04:00
|
|
|
|
2014-05-16 06:03:52 -04:00
|
|
|
void str_array_free(str_array *l) {
|
2014-05-31 13:26:36 -04:00
|
|
|
assert(l);
|
2016-03-21 22:23:37 -04:00
|
|
|
free(l->tokens.entries);
|
2016-03-08 01:19:54 -05:00
|
|
|
}
|
|
|
|
|
2016-03-21 22:23:37 -04:00
|
|
|
bool str_array_append(str_array * l, char * token, unsigned int len) {
|
|
|
|
R3_VECTOR(r3_iovec_t) *tks = &l->tokens;
|
|
|
|
r3_vector_reserve(NULL, tks, tks->size + 1);
|
|
|
|
r3_iovec_t *temp = tks->entries + tks->size++;
|
|
|
|
memset(temp, 0, sizeof(*temp));
|
|
|
|
temp->base = token;
|
|
|
|
temp->len = len;
|
2015-11-17 08:43:10 -05:00
|
|
|
return true;
|
2014-05-14 23:52:45 -04:00
|
|
|
}
|
|
|
|
|
2016-03-08 01:19:54 -05:00
|
|
|
void str_array_dump_slugs(const str_array *l) {
|
2016-03-21 22:23:37 -04:00
|
|
|
if (l->tokens.size) {
|
|
|
|
printf("[");
|
|
|
|
for ( int i = 0; i < l->tokens.size ; i++ ) {
|
|
|
|
printf("\"%*.*s\"", l->slugs.entries[i].len,l->slugs.entries[i].len,l->slugs.entries[i].base );
|
|
|
|
if ( i + 1 != l->tokens.size ) {
|
|
|
|
printf(", ");
|
|
|
|
}
|
2016-03-08 01:19:54 -05:00
|
|
|
}
|
2016-03-21 22:23:37 -04:00
|
|
|
printf("]\n");
|
|
|
|
} else {
|
|
|
|
printf("[]\n");
|
2016-03-08 01:19:54 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-26 13:07:33 -04:00
|
|
|
void str_array_dump(const str_array *l) {
|
2014-05-15 00:47:14 -04:00
|
|
|
printf("[");
|
2016-03-21 22:23:37 -04:00
|
|
|
for ( int i = 0; i < l->tokens.size ; i++ ) {
|
|
|
|
printf("\"%*.*s\"", l->tokens.entries[i].len,l->tokens.entries[i].len,l->tokens.entries[i].base );
|
|
|
|
// printf("\"%s\"", l->tokens.entries[i] );
|
|
|
|
if ( i + 1 != l->tokens.size ) {
|
2014-05-15 00:47:14 -04:00
|
|
|
printf(", ");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("]\n");
|
|
|
|
}
|
|
|
|
|
2014-05-14 23:52:45 -04:00
|
|
|
|
|
|
|
|
2014-05-15 00:47:14 -04:00
|
|
|
|