-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringutils.h
153 lines (124 loc) · 4.77 KB
/
stringutils.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#pragma once
#include <algorithm>
#include <codecvt>
#include <sstream>
#include <string>
#include <vector>
/*
MIT License
Copyright (c) 2021 Northwind Industries
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
namespace string
{
template <typename T>
std::string str(T str)
{
std::stringstream s;
s << str;
return s.str();
}
std::string to_string(std::wstring str);
std::wstring to_wstring(std::string str);
std::vector<std::string> split(std::string str, std::string delimiter);
std::vector<std::wstring> split(std::wstring str, std::wstring delimiter);
bool find(std::string haystack, std::string needle);
bool find(std::wstring haystack, std::wstring needle);
bool starts_with(std::string str, std::string prefix);
bool starts_with(std::wstring str, std::wstring prefix);
bool ends_with(std::string str, std::string suffix);
bool ends_with(std::wstring str, std::wstring suffix);
}
// Convert a wide string into a normal string
std::string string::to_string(std::wstring str)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> convert;
return convert.to_bytes(str);
}
// Convert a normal string into a wide string
std::wstring string::to_wstring(std::string str)
{
std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> convert;
return convert.from_bytes(str);
}
// Split a string in a vector by a delimiter
std::vector<std::string> string::split(std::string str, std::string delimiter)
{
size_t pos_start = 0, pos_end;
std::string token;
std::vector<std::string> splitString;
while ((pos_end = str.find(delimiter, pos_start)) != std::string::npos) {
token = str.substr(pos_start, pos_end - pos_start);
pos_start = pos_end + delimiter.length();
splitString.push_back(token);
}
splitString.push_back(str.substr(pos_start));
return splitString;
}
// Split a wide string in a vector by a delimiter
std::vector<std::wstring> string::split(std::wstring str, std::wstring delimiter)
{
size_t pos_start = 0, pos_end;
std::wstring token;
std::vector<std::wstring> splitWString;
while ((pos_end = str.find(delimiter, pos_start)) != std::string::npos) {
token = str.substr(pos_start, pos_end - pos_start);
pos_start = pos_end + delimiter.length();
splitWString.push_back(token);
}
splitWString.push_back(str.substr(pos_start));
return splitWString;
}
// Small wrapper function for string.find()
bool string::find(std::string haystack, std::string needle)
{
return haystack.find(needle) != std::string::npos;
}
// Small wrapper function for wstring.find()
bool string::find(std::wstring haystack, std::wstring needle)
{
return haystack.find(needle) != std::string::npos;
}
// Small wrapper function for checking if a string starts with another string
bool string::starts_with(std::string str, std::string prefix)
{
return str.rfind(prefix, 0) == 0;
}
// Small wrapper function for checking if a wide string starts with another wide string
bool string::starts_with(std::wstring str, std::wstring prefix)
{
return str.rfind(prefix, 0) == 0;
}
// Check if a string ends with another string
bool string::ends_with(std::string str, std::string suffix)
{
auto it = suffix.begin();
return str.size() >= suffix.size() &&
std::all_of(std::next(str.begin(), str.size() - suffix.size()), str.end(), [&it](const char & c){
return c == *(it++);
} );
}
// Check if a wide string ends with another wide string
bool string::ends_with(std::wstring str, std::wstring suffix)
{
auto it = suffix.begin();
return str.size() >= suffix.size() &&
std::all_of(std::next(str.begin(), str.size() - suffix.size()), str.end(), [&it](const char& c) {
return c == *(it++);
});
}
// Don't be rude, I'm not a C++ god nor I pretend to be ;)