-
Notifications
You must be signed in to change notification settings - Fork 0
/
String.h
45 lines (40 loc) · 1.09 KB
/
String.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
#pragma once
#include <regex>
#include <string>
#include <algorithm>
#include <cctype>
class String
{
public:
static std::string ToLower(const std::string& value)
{
auto lower = value;
std::transform(lower.begin(), lower.end(), lower.begin(), &std::tolower);
return lower;
}
static bool Equals(const std::string& value1, const std::string& value2, bool caseSensitive)
{
return caseSensitive ?
value1 == value2 :
ToLower(value1) == ToLower(value2);
}
static bool EndsWith(const std::string& value, const std::string& suffix)
{
if (suffix.size() > value.size())
return false;
auto pos = value.rfind(suffix);
return pos == (value.size() - suffix.size());
}
static std::string Replace(const std::string& source, const std::string& find, const std::string& replaceWith)
{
return std::regex_replace(source, std::regex{ find }, replaceWith);
}
static std::string EscapeXml(const std::string& value)
{
auto escaped = value;
escaped = Replace(escaped, "&", "&");
escaped = Replace(escaped, "<", "<");
escaped = Replace(escaped, ">", ">");
return escaped;
}
};