This repository was archived by the owner on Mar 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathTokenizer.cpp
89 lines (72 loc) · 2.57 KB
/
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
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
/*
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2017 Ivan Vaccari <[email protected]>
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
*/
#include "Tokenizer.h"
#include <cstring>
Tokenizer::Tokenizer()
{
//ctor
}
Tokenizer::~Tokenizer()
{
//dtor
}
std::string Tokenizer::removeSpaces(std::string tmp_token){
size_t remove_pos=tmp_token.find(" ");
while (remove_pos!=std::string::npos){
tmp_token.erase(remove_pos,1);
remove_pos=tmp_token.find(" ");
}
return tmp_token;
}
std::vector<std::string> Tokenizer::tokenize(const std::string &line){
std::vector<std::string> tokens;
char * input = new char [line.length()+1];
std::strcpy (input, line.c_str());
int max_delim_count=4;
char delimiters[max_delim_count][2];
strcpy(delimiters[0],"=");
strcpy(delimiters[1],",");
strcpy(delimiters[2],"(");
strcpy(delimiters[3],")");
char *string_last_match_pos=input;
char *string_curr_pos=input;
while (string_curr_pos[0]!='\0'){
char *minimum_match_pos=input+strlen(input);
int minimum_match_length=0;
for(int i=0;i<max_delim_count;i++){
int delim_len=strlen(delimiters[i]);
if (strncmp(string_curr_pos,delimiters[i],delim_len)==0){
if (minimum_match_pos>string_curr_pos){
minimum_match_pos=string_curr_pos;
minimum_match_length=delim_len;
}
}
}
if (minimum_match_length>0){
//will crash with strings longer than 200. don't care for now.
char tmp[200];
if (minimum_match_pos-string_last_match_pos>0){
memset(tmp,0,200);
strncpy(tmp,string_last_match_pos,minimum_match_pos-string_last_match_pos);
tokens.push_back(removeSpaces(std::string(tmp)));
}
memset(tmp,0,200);
strncpy(tmp,minimum_match_pos,minimum_match_length);
tokens.push_back(removeSpaces(std::string(tmp)));
string_last_match_pos=minimum_match_pos+1;
string_curr_pos=minimum_match_pos+1;
}else{
string_curr_pos++;
}
}
return tokens;
}