-
Notifications
You must be signed in to change notification settings - Fork 0
/
str.cpp
98 lines (87 loc) · 1.92 KB
/
str.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
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
#include "common.h"
#include <string>
#include <codecvt>
#include <locale>
#include <sstream>
#ifdef __linux__
std::locale const utf8("en_US.UTF-8");
#elif _WIN32
std::locale const utf8("rus");
#endif
long long int str::fromString(const std::string& s)
{
std::istringstream iss(s);
long long int res;
iss >> res;
return res;
}
args &split(const std::string &s, char delim, std::vector<std::string> &elems)
{
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim))
{
elems.push_back(item);
}
return elems;
}
args str::words(const std::string &s, char delim)
{
std::vector<std::string> elems;
split(s, delim, elems);
return elems;
}
auto converter = new std::wstring_convert<std::codecvt_utf8<wchar_t> >();
// Convert UTF-8 byte string to wstring
std::wstring to_wstring(std::string const& s)
{
wstring data = converter->from_bytes(s);
return data;
}
std::string to_string(std::wstring const& s) {
string data = converter->to_bytes(s);
return data;
}
string str::summ(args words, unsigned int s)
{
string r = "";
if(s+1>words.size()) return r;
for(unsigned int i = s; i < words.size(); i++)
{
r += words[i];
r+= " ";
}
r.resize(r.size()-1);
return r;
}
bool str::at(string str1, string str2)
{
str1=str::low(str1);
str2=str::low(str2);
return strstr(str1.c_str(), str2.c_str());
}
string str::low(string str)
{
auto ss = to_wstring(str);
for (auto& c : ss) {
c = std::tolower(c, utf8);
}
return to_string(ss);
}
string str::replase(string str, string findstr, string replasestr)
{
string::size_type index;
while((index = str.find(findstr))!=std::string::npos)
{
str.replace(index, findstr.size(), replasestr);
}
return str;
}
string str::convertHtml(string str)
{
str = str::replase(str, "&", "&");
str = str::replase(str, """, "\"");
str = str::replase(str, ">", ">");
str = str::replase(str, "<", "<");
return str;
}