-
Notifications
You must be signed in to change notification settings - Fork 0
/
Tokenizer.cpp
53 lines (42 loc) · 861 Bytes
/
Tokenizer.cpp
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
#include "Tokenizer.h"
#include <iostream>
#include <string.h>
Tokenizer::Tokenizer()
{
}
Tokenizer::~Tokenizer()
{
}
ylib::Vector<ylib::String> Tokenizer::getTokens(const char* str) {
int i = 0;
ylib::Vector<ylib::String> tokens;
char c;
char *token;
const char *startPointer;
size_t size = 0;
while ((c = str[i]) != '\0' && c != EOF) {
if (isspace(c)) {
if (size != 0) {
token = (char*)malloc(sizeof(char)*(size+1));
strncpy(token, startPointer, size);
token[size] = '\0';
tokens += token;
size = 0;
}
}
else {
if (size == 0) {
startPointer = &str[i];
}
++size;
}
++i;
}
if (size != 0) {
token = (char*)malloc(sizeof(char)*(size + 1));
strncpy(token, startPointer, size);
token[size] = '\0';
tokens += token;
}
return tokens;
}