9
|
1 /**
|
|
2 * strings.cpp: Useful functions for manipulating strings
|
|
3 **/
|
|
4 #include "core/strings.h"
|
15
|
5 #include <algorithm>
|
|
6 #include <cctype>
|
9
|
7 #include <locale>
|
|
8 #include <string>
|
|
9 #include <vector>
|
|
10
|
|
11 namespace Strings {
|
|
12
|
|
13 std::string Implode(const std::vector<std::string>& vector, const std::string& delimiter) {
|
|
14 if (vector.size() < 1)
|
|
15 return "-";
|
|
16 std::string out = "";
|
|
17 for (unsigned long long i = 0; i < vector.size(); i++) {
|
|
18 out.append(vector.at(i));
|
|
19 if (i < vector.size() - 1)
|
|
20 out.append(delimiter);
|
|
21 }
|
|
22 return out;
|
|
23 }
|
|
24
|
|
25 std::string ReplaceAll(const std::string& string, const std::string& find, const std::string& replace) {
|
|
26 std::string result;
|
|
27 size_t pos, find_len = find.size(), from = 0;
|
|
28 while ((pos = string.find(find, from)) != std::string::npos) {
|
|
29 result.append(string, from, pos - from);
|
|
30 result.append(replace);
|
|
31 from = pos + find_len;
|
|
32 }
|
|
33 result.append(string, from, std::string::npos);
|
|
34 return result;
|
|
35 }
|
|
36
|
|
37 /* this function probably fucks your RAM but whatevs */
|
|
38 std::string SanitizeLineEndings(const std::string& string) {
|
|
39 std::string result(string);
|
|
40 result = ReplaceAll(result, "\r\n", "\n");
|
|
41 result = ReplaceAll(result, "<br>", "\n");
|
|
42 result = ReplaceAll(result, "\n\n\n", "\n\n");
|
|
43 return result;
|
|
44 }
|
|
45
|
|
46 std::string RemoveHtmlTags(const std::string& string) {
|
|
47 std::string html(string);
|
|
48 while (html.find("<") != std::string::npos) {
|
|
49 auto startpos = html.find("<");
|
|
50 auto endpos = html.find(">") + 1;
|
|
51
|
|
52 if (endpos != std::string::npos) {
|
|
53 html.erase(startpos, endpos - startpos);
|
|
54 }
|
|
55 }
|
|
56 return html;
|
|
57 }
|
|
58
|
|
59 std::string TextifySynopsis(const std::string& string) {
|
|
60 return RemoveHtmlTags(SanitizeLineEndings(string));
|
|
61 }
|
|
62
|
15
|
63 /* these functions suck for i18n!...
|
|
64 but we only use them with JSON
|
|
65 stuff anyway */
|
|
66 std::string ToUpper(const std::string& string) {
|
|
67 std::string result(string);
|
|
68 std::transform(result.begin(), result.end(), result.begin(), [](unsigned char c) { return std::toupper(c); });
|
|
69 return result;
|
|
70 }
|
|
71
|
|
72 std::string ToLower(const std::string& string) {
|
|
73 std::string result(string);
|
|
74 std::transform(result.begin(), result.end(), result.begin(), [](unsigned char c) { return std::tolower(c); });
|
|
75 return result;
|
|
76 }
|
|
77
|
9
|
78 } // namespace Strings
|