-
Notifications
You must be signed in to change notification settings - Fork 0
/
stringtokenizer.cpp
181 lines (154 loc) · 5.94 KB
/
stringtokenizer.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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/* stringtokenizer.cpp; reference implementation for stringtokenizer.h
* Copyright (C) 2016 Aayush Agarwal
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdexcept>
#include "stringtokenizer.h"
namespace stringtokenizer {
/* WARNING: tokenize is currently only tested to work with standard strings
* where each character fits in a byte. Using unicode or any other multi-byte
* encodings may result in unexpected behaviour.
*/
std::vector<std::string> tokenize(std::string source, char delimiter) {
//to ensure the last word is correctly parsed
if(source.back() != delimiter) source += delimiter;
std::vector<std::string> stringtokens(0);
std::string currtoken = "";
for(const auto& i: source) {
if(i == delimiter) {
if(currtoken != "") {
stringtokens.push_back(currtoken);
currtoken = "";
}
else currtoken = "";
}
else currtoken += i;
}
return stringtokens;
}
/* WARNING: It is your responsibility to ensure that the values in the
* strings sent to these functions are not out of range.
*
* These functions will not add any values that are out of range to the vector.
* These functions can handle mixed input using the same rules as the
* std::sto<type> functions.
*/
/* functions that take string and delimiter as input */
std::vector<int> sttoiv(std::string source, char delimiter) {
std::vector<std::string> tokens = tokenize(source, delimiter);
return sttoiv(tokens);
}
std::vector<long> sttolv(std::string source, char delimiter) {
std::vector<std::string> tokens = tokenize(source, delimiter);
return sttolv(tokens);
}
std::vector<long long> sttollv(std::string source, char delimiter) {
std::vector<std::string> tokens = tokenize(source, delimiter);
return sttollv(tokens);
}
std::vector<unsigned long> sttoulv(std::string source, char delimiter) {
std::vector<std::string> tokens = tokenize(source, delimiter);
return sttoulv(tokens);
}
std::vector<unsigned long long> sttoullv(std::string source, char delimiter) {
std::vector<std::string> tokens = tokenize(source, delimiter);
return sttoullv(tokens);
}
std::vector<float> sttofv(std::string source, char delimiter) {
std::vector<std::string> tokens = tokenize(source, delimiter);
return sttofv(tokens);
}
std::vector<double> sttodv(std::string source, char delimiter) {
std::vector<std::string> tokens = tokenize(source, delimiter);
return sttodv(tokens);
}
std::vector<long double> sttoldv(std::string source, char delimiter) {
std::vector<std::string> tokens = tokenize(source, delimiter);
return sttoldv(tokens);
}
/* functions that take string vectors as input */
std::vector<int> sttoiv(std::vector<std::string> tokens) {
std::vector<int> int_tokens(0);
for(const auto& i: tokens) {
try { int_tokens.push_back(std::stoi(i)); }
catch(std::invalid_argument) { continue; }
catch(std::out_of_range) { continue; }
}
return int_tokens;
}
std::vector<long> sttolv(std::vector<std::string> tokens) {
std::vector<long> long_tokens(0);
for(const auto& i: tokens) {
try { long_tokens.push_back(std::stol(i)); }
catch(std::invalid_argument) { continue; }
catch(std::out_of_range) { continue; }
}
return long_tokens;
}
std::vector<long long> sttollv(std::vector<std::string> tokens) {
std::vector<long long> long_long_tokens(0);
for(const auto& i: tokens) {
try { long_long_tokens.push_back(std::stoll(i)); }
catch(std::invalid_argument) { continue; }
catch(std::out_of_range) { continue; }
}
return long_long_tokens;
}
std::vector<unsigned long> sttoulv(std::vector<std::string> tokens) {
std::vector<unsigned long> unsigned_long_tokens(0);
for(const auto& i: tokens) {
try { unsigned_long_tokens.push_back(std::stoul(i)); }
catch(std::invalid_argument) { continue; }
catch(std::out_of_range) { continue; }
}
return unsigned_long_tokens;
}
std::vector<unsigned long long> sttoullv(std::vector<std::string> tokens) {
std::vector<unsigned long long> unsigned_long_long_tokens(0);
for(const auto& i: tokens) {
try { unsigned_long_long_tokens.push_back(std::stoull(i)); }
catch(std::invalid_argument) { continue; }
catch(std::out_of_range) { continue; }
}
return unsigned_long_long_tokens;
}
std::vector<float> sttofv(std::vector<std::string> tokens) {
std::vector<float> float_tokens(0);
for(const auto& i: tokens) {
try { float_tokens.push_back(std::stof(i)); }
catch(std::invalid_argument& iarg) { continue; }
catch(std::out_of_range& oor) { continue; }
}
return float_tokens;
}
std::vector<double> sttodv(std::vector<std::string> tokens) {
std::vector<double> double_tokens(0);
for(const auto& i: tokens) {
try { double_tokens.push_back(std::stod(i)); }
catch(std::invalid_argument) { continue; }
catch(std::out_of_range) { continue; }
}
return double_tokens;
}
std::vector<long double> sttoldv(std::vector<std::string> tokens) {
std::vector<long double> long_double_tokens(0);
for(const auto& i: tokens) {
try { long_double_tokens.push_back(std::stold(i)); }
catch(std::invalid_argument) { continue; }
catch(std::out_of_range) { continue; }
}
return long_double_tokens;
}
}