-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.h
More file actions
43 lines (39 loc) · 1.03 KB
/
common.h
File metadata and controls
43 lines (39 loc) · 1.03 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
#pragma once
#include <vector>
#include <string>
#include <sstream>
std::vector<std::string> split(const std::string &original, char delimiter = ' ', int maxSplits = -1)
{
std::vector<std::string> result;
std::string buffer;
std::istringstream source(original);
int splits = 0;
while (getline(source, buffer, delimiter) && splits != maxSplits)
{
result.push_back(buffer);
splits++;
}
return result;
}
std::vector<std::string> split(const std::string &original, const std::string &delimiter, int maxSplits = -1)
{
std::vector<std::string> result;
int splits = 0;
size_t offset = 0;
while (true)
{
if (splits == maxSplits)
{
break;
}
size_t pos = original.find(delimiter, offset);
if (pos == std::string::npos)
{
break;
}
result.push_back(original.substr(offset, pos - offset));
offset = pos + delimiter.length();
}
result.push_back(original.substr(offset));
return result;
}