-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsplit.hpp
More file actions
104 lines (87 loc) · 2.02 KB
/
split.hpp
File metadata and controls
104 lines (87 loc) · 2.02 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
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
// algocpp/string/split.hpp
//
// This file is part of algocpp and is copyrighted by algocpp.
// If used, it must comply with the MIT License.
#ifndef ALGOCPP_STRING_SPLIT
#define ALGOCPP_STRING_SPLIT
#include <vector>
#include <string>
#include <locale>
#include <algocpp/exception/blank.hpp>
#include <algocpp/string/wconvert.hpp>
namespace algocpp
{
namespace string
{
namespace base
{
template <typename Tp1>
inline std::vector<Tp1> base_split(Tp1 s, Tp1 div)
{
Tp1 blank = Tp1{};
if (div == blank)
{
std::vector<Tp1> result(s.size());
for (unsigned long long i = 0; i < s.size(); i++)
{
result[i] = Tp1{s[i]};
}
return result;
}
std::vector<Tp1> result;
Tp1 tmp = blank;
for (unsigned long long i = 0; i < s.size(); i++)
{
if (s.size() - i >= div.size() && s[i] == div[0])
{
bool flag = true;
for (unsigned long long j = 0; j < div.size(); j++)
{
if (s[i + j] != div[j])
{
flag = false;
break;
}
}
if (flag)
{
result.push_back(tmp);
tmp = blank;
i += div.size() - 1;
}
}
else
{
tmp += s[i];
}
}
// if (tmp != blank)
result.push_back(tmp);
return result;
}
}
inline std::vector<std::string> split(std::string s, std::string div)
{
return base::base_split<std::string>(s, div);
}
inline std::vector<std::u32string> split(std::u32string s, std::u32string div)
{
return base::base_split<std::u32string>(s, div);
}
inline std::vector<std::u16string> split(std::u16string s, std::u16string div)
{
return base::base_split<std::u16string>(s, div);
}
inline std::vector<std::wstring> split(std::wstring s, std::wstring div)
{
return base::base_split<std::wstring>(s, div);
}
#if __cplusplus >= 202002LL
inline std::vector<std::u8string> split(std::u8string s, std::u8string div)
{
return base::base_split<std::u8string>(s, div);
}
#endif
}
}
#endif // ALGOCPP_STRING_SPLIT