forked from etr/libhttpserver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_utilities.cpp
More file actions
67 lines (61 loc) · 1.76 KB
/
string_utilities.cpp
File metadata and controls
67 lines (61 loc) · 1.76 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
#include <algorithm>
#include <string>
#include <istream>
#include <sstream>
#include <vector>
#include <cctype>
#include "string_utilities.hpp"
#include <stdlib.h>
#include <string.h>
namespace string_utilities
{
std::string to_upper_copy(std::string str)
{
std::transform(str.begin(),
str.end(),
str.begin(),
(int(*)(int)) std::toupper
);
return str;
}
std::string to_lower_copy(std::string str)
{
std::transform(str.begin(),
str.end(),
str.begin(),
(int(*)(int)) std::tolower
);
return str;
}
std::vector<std::string> string_split(std::string s, char sep)
{
std::vector<std::string> v;
std::istringstream buf(s);
for(std::string token; getline(buf, token, sep); )
{
if(token != "")
v.push_back(token);
}
return v;
}
std::string regex_replace(std::string str, std::string pattern, std::string replace_str)
{
regex_t preg;
std::string to_ret;
regmatch_t substmatch[1];
regcomp(&preg, pattern.c_str(), REG_EXTENDED|REG_ICASE);
if ( regexec(&preg, str.c_str(), 1, substmatch, 0) == 0 )
{
//fprintf(stderr, "%d, %d\n", substmatch[0].rm_so, substmatch[0].rm_eo);
char *ns = (char*)malloc(substmatch[0].rm_so + 1 + replace_str.size() + (str.size() - substmatch[0].rm_eo) + 2);
memcpy(ns, str.c_str(), substmatch[0].rm_so+1);
memcpy(&ns[substmatch[0].rm_so], replace_str.c_str(), replace_str.size());
memcpy(&ns[substmatch[0].rm_so+replace_str.size()], &str[substmatch[0].rm_eo], strlen(&str[substmatch[0].rm_eo]));
ns[ substmatch[0].rm_so + replace_str.size() + strlen(&str[substmatch[0].rm_eo]) ] = 0;
to_ret = std::string(ns);
free(ns);
}
regfree(&preg);
return to_ret;
}
};