-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringsplit.cpp
71 lines (62 loc) · 1.49 KB
/
stringsplit.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
#include "stringsplit.h"
int StringUtils::SplitString(const string& input,
const string& delimiter, vector<string>& results,
bool includeEmpties)
{
int iPos = 0;
int newPos = -1;
int sizeS2 = (int)delimiter.size();
int isize = (int)input.size();
if(
( isize == 0 )
||
( sizeS2 == 0 )
)
{
return 0;
}
vector<int> positions;
newPos = input.find (delimiter, 0);
if( newPos < 0 )
{
return 0;
}
int numFound = 0;
while( newPos >= iPos )
{
numFound++;
positions.push_back(newPos);
iPos = newPos;
newPos = input.find (delimiter, iPos+sizeS2);
}
if( numFound == 0 )
{
return 0;
}
for( unsigned int i=0; i <= positions.size(); ++i )
{
string s("");
if( i == 0 )
{
s = input.substr( i, positions[i] );
}
int offset = positions[i-1] + sizeS2;
if( offset < isize )
{
if( i == positions.size() )
{
s = input.substr(offset);
}
else if( i > 0 )
{
s = input.substr( positions[i-1] + sizeS2,
positions[i] - positions[i-1] - sizeS2 );
}
}
if( includeEmpties || ( s.size() > 0 ) )
{
results.push_back(s);
}
}
return numFound;
}