-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.cpp
124 lines (108 loc) · 2.48 KB
/
functions.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
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
#include "includes/functions.h"
#include<iostream>
using namespace std;
void toLowerString(string &str)
{
transform(str.begin(), str.end(), str.begin(), (int (*)(int))tolower);
}
string <rim(string &s)
{
s.erase(s.begin(), find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace))));
return s;
}
string &rtrim(string &s)
{
s.erase(find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(), s.end());
return s;
}
string &Trim(string &s)
{
return ltrim(rtrim(s));
}
string generate_name()
{
srand(time(NULL));
string characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFHHIJKLMNOPQRSTUVWXYZ";
string gen = "";
for(int i = 0; i<20; i++)
{
gen += characters[rand()%62];
}
return gen;
}
string to_String(int n)
{
string tmp = "";
int ca = n;
while (ca>0)
{
tmp = (char)('0'+ca%10) + tmp;
ca /= 10;
}
return tmp;
}
int to_Int(string num)
{
int tmp = 0;
int _size = num.size();
for (int i = 0; i < _size; i++)
{
tmp += num[i]-'0';
if (i != _size-1) tmp *= 10;
}
return tmp;
}
int hex_char_value(char c)
{
if(c >= '0' && c <= '9')
return c - '0';
else if(c >= 'a' && c <= 'f')
return (c - 'a' + 10);
else if(c >= 'A' && c <= 'F')
return (c - 'A' + 10);
assert(0);
return 0;
}
int smallpow(int base, int power)
{
int tmp = 1;
int count = power;
while ( power-- > 0)
{
tmp *= base;
}
return tmp;
}
int hex_to_dec(string hex)
{
int result = 0;
int len = hex.size();
for(int i = 0; i < len; i++)
{
result += smallpow(16, len-i-1) * hex_char_value(hex[i]);
}
return result;
}
string urlDecode(string str)
{
string decoded = str;
regex para_pattern("%([\\dA-Fa-f]{2})");
regex replace;
replace.assign("\\+");
string t = " ";
replace_all_regex(decoded, replace, t);
smatch result;
string::const_iterator start = str.begin();
string::const_iterator end = str.end();
while ( regex_search(start, end, result, para_pattern))
{
int code = hex_to_dec(result[1].str());
char tmp = char(code);
string to_replace = "";
to_replace += tmp;
replace.assign("%"+result[1].str());
replace_all_regex(decoded, replace, to_replace);
start = result[0].second;
}
return decoded;
}