forked from ColinPitrat/caprice32
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringutils.cpp
More file actions
87 lines (78 loc) · 2.13 KB
/
stringutils.cpp
File metadata and controls
87 lines (78 loc) · 2.13 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
#include "stringutils.h"
#include <algorithm>
#include <cstring>
#include <sstream>
#include <string>
#include <strings.h>
namespace stringutils
{
std::vector<std::string> split(const std::string& s, char delim, bool ignore_empty)
{
std::vector<std::string> elems;
std::stringstream ss(s);
std::string item;
while (std::getline(ss, item, delim))
{
if (ignore_empty && item.empty()) continue;
elems.push_back(item);
}
return elems;
}
std::string join(const std::vector<std::string>& v, const std::string& delim)
{
std::string result;
for(auto it = v.begin(); it != v.end(); ++it)
{
result += *it;
if (it != v.end() - 1) result += delim;
}
return result;
}
std::string trim(const std::string& s, char c)
{
auto b = s.begin();
auto e = s.end();
e--;
while(*b == c) b++;
while(*e == c) e--;
if(e++ >= b) return std::string(b, e);
return "";
}
std::string lower(const std::string& s)
{
std::string result(s);
std::transform(result.begin(), result.end(), result.begin(), ::tolower);
return result;
}
std::string upper(const std::string& s)
{
std::string result(s);
std::transform(result.begin(), result.end(), result.begin(), ::toupper);
return result;
}
std::string replace(std::string s, const std::string& search, const std::string& replace)
{
auto start_pos = s.find(search);
if (start_pos == std::string::npos) return s;
return s.replace(start_pos, search.size(), replace);
}
void splitPath(const std::string& path, std::string& dirname, std::string& filename)
{
auto delimiter = path.rfind('/');
if(delimiter == std::string::npos) {
delimiter = path.rfind('\\');
}
if(delimiter != std::string::npos) {
delimiter++;
dirname = path.substr(0, delimiter);
filename = path.substr(delimiter);
} else {
dirname = "./";
filename = path;
}
}
bool caseInsensitiveCompare(const std::string& str1, const std::string& str2)
{
return strncasecmp(str1.c_str(), str2.c_str(), std::max(str1.size(), str2.size())) < 0;
}
}