r3/src/token.c

63 lines
1.5 KiB
C
Raw Normal View History

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"
#include "r3_slug.h"
2014-05-28 09:08:06 -04:00
#include "str_array.h"
#include "zmalloc.h"
#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);
free(l->tokens.entries);
}
2018-02-20 12:18:43 -05:00
bool str_array_append(str_array * l, const 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
}
void str_array_dump_slugs(const str_array *l) {
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(", ");
}
}
printf("]\n");
} else {
printf("[]\n");
}
}
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("[");
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