-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest.cpp
46 lines (41 loc) · 1.23 KB
/
test.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
#include "pch.h"
#include <assert.h>
#include <vector>
#include <algorithm>
#include <string>
void split(std::vector<std::string>& tokens,
const std::string& src, const std::string& seps, bool compress)
{
char sepFlags[256] = {0};
for (size_t i = 0; i < seps.size(); ++i) sepFlags[seps[i]] = 1;
std::string s;
for (size_t i = 0; i < src.size(); ++i) {
if (sepFlags[src[i]]) {
if (s.size() > 0 || !compress) tokens.push_back(s);
s.clear();
}
else s.push_back(src[i]);
}
if (s.size() > 0 || !compress) tokens.push_back(s);
}
int main()
{
{
std::string refs[] = {"", "", ""};
std::vector<std::string> tokens;
split(tokens, ",,", ",", false);
assert(std::equal(tokens.begin(), tokens.end(), refs));
}
{
std::string refs[] = {"a", "", "", "b", "c"};
std::vector<std::string> tokens;
split(tokens, "a...b,c", ".#,", false);
assert(std::equal(tokens.begin(), tokens.end(), refs));
}
{
std::string refs[] = {"a", "b", "aa"};
std::vector<std::string> tokens;
split(tokens, "#!!a!@b@#aa", "#@!", true);
assert(std::equal(tokens.begin(), tokens.end(), refs));
}
}