-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoken.c
More file actions
125 lines (99 loc) · 2.84 KB
/
Copy pathtoken.c
File metadata and controls
125 lines (99 loc) · 2.84 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#include "token.h"
#include "ronto.h"
#include <assert.h>
#include <stdlib.h>
#include <string.h>
// Get a range of continous sequence of
// EITHER delimeters OR non-delimeter characters
// Based on whether the first character of `string`
// is a delimeter or not, this function returns a `int`
// which is a sequence range of the number of delimeter or
// non-delimeter characters
int str_range(char *string, char delimeter) {
if (string == NULL || *string == '\0')
return -1;
int index = 0;
size_t len = strlen(string);
// if the first character in the string is a delimeter
if (string[0] == delimeter)
for (size_t i = 0; i < len; i++)
if (string[i] == delimeter)
index++;
else
break;
// if the first character in the string is NOT a delimeter
else
for (size_t i = 0; i < len; i++)
if (string[i] != delimeter)
index++;
else
break;
return index;
}
// Tokenize `str` into a group of tokens. The characters are
// tokenized based on the criteria of whether or not they are
// delimeters
Token *token_tokenize(char *str, char delimeter) {
if (str == NULL)
return NULL;
Token *t = malloc(sizeof(Token));
if (t == NULL)
return NULL;
t->len = 0;
t->curr = 0;
t->inner = NULL;
size_t original_strlen = strlen(str);
int token_nums = 0;
// iterate over the characters in the string
for (size_t i = 0; i < original_strlen; i++) {
// get the range of the current character type
int range = str_range(str + i, delimeter);
if (range <= 0)
continue;
t->len++;
char **new_inner = realloc(t->inner, t->len * sizeof(char *));
if (new_inner == NULL) {
// Free previously allocated tokens on failure
for (int j = 0; j < token_nums; j++)
free(t->inner[j]);
free(t->inner);
free(t);
return NULL;
}
t->inner = new_inner;
t->inner[token_nums] = malloc(range + 1);
if (t->inner[token_nums] == NULL) {
// Free previously allocated tokens on failure
for (int j = 0; j < token_nums; j++)
free(t->inner[j]);
free(t->inner);
free(t);
return NULL;
}
// insert it into the token container
memcpy(t->inner[token_nums], str + i, range);
t->inner[token_nums][range] = '\0';
// increase the current iterator to match the range
i += (range - 1);
// indicate that we got another token
token_nums++;
}
return t;
}
// get the first token from a collection of tokens
char *token_get_next(Token *t) {
if (t == NULL) {
editor_log("[ERROR]: t == NULL\n");
return NULL;
}
// when all the tokens are consumed
if (t->curr == t->len) {
return NULL;
}
// extract the current token from the index
char *next = t->inner[t->curr];
// advance the token pointer
t->curr++;
return next;
}
int token_len(char *str) { return strlen(str); }