-
Notifications
You must be signed in to change notification settings - Fork 1
/
split_join.h
81 lines (77 loc) · 2.16 KB
/
split_join.h
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
#pragma once
#include <string>
/**
join/merge/aggregate a set of string/string_view with Separator.
May be defined as static method of basic_string.
@param InputStringList iterable Container(vector,list,...) containing input string
@param Separator used to join strings
@return agregated string
**/
template<class T, class U>
auto string_join(T &InputStringList, U Separator)
{
std::basic_string <typename T::value_type::value_type > result_string;
for (auto it = InputStringList.begin(); it != InputStringList.end(); it++)
{
if (it != InputStringList.begin())
result_string += Separator;
result_string += *it;
}
return result_string;
}
/**
split/tokenize a string/string_view with Separator.
May be defined as method of basic_string.
@param This input string (may not exists has class member, would be simply replaced by this
@param Separator used to split the string
@param Result Output container
**/
template<class T, class V>
void string_split(const T &This, const T Separator, V &Result)
{
Result.clear();
typename T::size_type p, start = 0;
while (1)
{
p = This.find(Separator, start);
if (p == T::npos)
{
Result.emplace_back(This, start, T::npos);
return;
}
else
{
Result.emplace_back(This, start, p - start);
// emplace_back doesn't works with string_view container, other version of string_split use substr method
//, may work if required construtors added on string_view class
start = p + Separator.length();
}
}
}
/**
split/tokenize a string/string_view with Separator.
May be defined as method of basic_string.
@param This input string (may not exists has class member, would be simply replaced by this
@param Separator used to split the string
@return Output container
**/
template<class T, class V>
V string_split(const T &This, const T Separator)
{
V Result; // Result Container V Parametred
typename T::size_type p, start = 0;
while (1)
{
p = This.find(Separator, start);
if (p == T::npos)
{
Result.emplace_back(((typename V::value_type)(This)).substr(start, T::npos));
return Result;
}
else
{
Result.emplace_back(((typename V::value_type)(This)).substr(start, p - start));
start = p + Separator.length();
}
}
}