2
|
1 /**
|
|
2 * string_utils.cpp: Useful functions for manipulating strings
|
|
3 *
|
7
|
4 * Every function in here *should* have a working wstring equivalent,
|
|
5 * although we don't use wstrings anymore...
|
2
|
6 **/
|
|
7 #include <vector>
|
|
8 #include <string>
|
|
9 #include <codecvt>
|
|
10 #include <locale>
|
|
11 #include "string_utils.h"
|
|
12
|
|
13 std::string StringUtils::Implode(const std::vector<std::string>& vector,
|
|
14 const std::string& delimiter) {
|
7
|
15 if (vector.size() < 1)
|
|
16 return "-";
|
2
|
17 std::string out = "";
|
3
|
18 for (unsigned long long i = 0; i < vector.size(); i++) {
|
2
|
19 out.append(vector.at(i));
|
|
20 if (i < vector.size()-1)
|
|
21 out.append(delimiter);
|
|
22 }
|
|
23 return out;
|
|
24 }
|
|
25
|
|
26 std::wstring StringUtils::Implode(const std::vector<std::wstring>& vector,
|
|
27 const std::wstring& delimiter) {
|
|
28 std::wstring out = L"";
|
3
|
29 for (unsigned long long i = 0; i < vector.size(); i++) {
|
2
|
30 out.append(vector.at(i));
|
|
31 if (i < vector.size()-1)
|
|
32 out.append(delimiter);
|
|
33 }
|
|
34 return out;
|
|
35 }
|
|
36
|
|
37 std::string StringUtils::WstrToUtf8(const std::wstring& string) {
|
|
38 std::wstring_convert<std::codecvt_utf8<wchar_t>> convert;
|
|
39 return convert.to_bytes(string);
|
|
40 }
|
|
41
|
|
42 std::wstring StringUtils::Utf8ToWstr(const std::string& string) {
|
|
43 std::wstring_convert<std::codecvt_utf8<wchar_t>> convert;
|
|
44 return convert.from_bytes(string);
|
|
45 }
|
|
46
|
|
47 std::string StringUtils::ReplaceAll(const std::string& string,
|
|
48 const std::string& find,
|
|
49 const std::string& replace) {
|
|
50 std::string result;
|
|
51 size_t pos, find_len = find.size(), from = 0;
|
|
52 while ((pos=string.find(find,from)) != std::string::npos) {
|
|
53 result.append(string, from, pos - from);
|
|
54 result.append(replace);
|
|
55 from = pos + find_len;
|
|
56 }
|
|
57 result.append(string, from, std::string::npos);
|
|
58 return result;
|
|
59 }
|
|
60
|
|
61 std::wstring StringUtils::ReplaceAll(const std::wstring& string,
|
|
62 const std::wstring& find,
|
|
63 const std::wstring& replace) {
|
|
64 std::wstring result;
|
|
65 size_t pos, find_len = find.size(), from = 0;
|
|
66 while ((pos=string.find(find,from)) != std::wstring::npos) {
|
|
67 result.append(string, from, pos - from);
|
|
68 result.append(replace);
|
|
69 from = pos + find_len;
|
|
70 }
|
|
71 result.append(string, from, std::wstring::npos);
|
|
72 return result;
|
|
73 }
|
|
74
|
|
75 /* this function probably fucks your RAM but whatevs */
|
|
76 std::string StringUtils::SanitizeLineEndings(const std::string& string) {
|
|
77 std::string result(string);
|
|
78 result = ReplaceAll(result, "\r\n", "\n");
|
|
79 result = ReplaceAll(result, "<br>", "\n");
|
|
80 result = ReplaceAll(result, "\n\n\n", "\n\n");
|
|
81 return result;
|
|
82 }
|
|
83
|
|
84 std::wstring StringUtils::SanitizeLineEndings(const std::wstring& string) {
|
|
85 std::wstring result(string);
|
|
86 result = ReplaceAll(result, L"\r\n", L"\n");
|
|
87 result = ReplaceAll(result, L"<br>", L"\n");
|
|
88 result = ReplaceAll(result, L"\n\n\n", L"\n\n");
|
|
89 return result;
|
|
90 }
|
|
91
|
|
92 std::string StringUtils::RemoveHtmlTags(const std::string& string) {
|
|
93 std::string html(string);
|
|
94 while (html.find("<") != std::string::npos)
|
|
95 {
|
|
96 auto startpos = html.find("<");
|
|
97 auto endpos = html.find(">") + 1;
|
|
98
|
|
99 if (endpos != std::string::npos)
|
|
100 {
|
|
101 html.erase(startpos, endpos - startpos);
|
|
102 }
|
|
103 }
|
|
104 return html;
|
|
105 }
|
|
106
|
|
107 std::wstring StringUtils::RemoveHtmlTags(const std::wstring& string) {
|
|
108 std::wstring html(string);
|
|
109 while (html.find(L"<") != std::wstring::npos)
|
|
110 {
|
|
111 auto startpos = html.find(L"<");
|
|
112 auto endpos = html.find(L">") + 1;
|
|
113
|
|
114 if (endpos != std::wstring::npos)
|
|
115 {
|
|
116 html.erase(startpos, endpos - startpos);
|
|
117 }
|
|
118 }
|
|
119 return html;
|
|
120 }
|
|
121
|
|
122 std::string StringUtils::TextifySynopsis(const std::string& string) {
|
|
123 std::string result = SanitizeLineEndings(string);
|
|
124 result = RemoveHtmlTags(string);
|
|
125 return result;
|
|
126 }
|
|
127
|
|
128 std::wstring StringUtils::TextifySynopsis(const std::wstring& string) {
|
|
129 std::wstring result = SanitizeLineEndings(string);
|
|
130 result = RemoveHtmlTags(string);
|
|
131 return result;
|
|
132 }
|