9
|
1 /**
|
|
2 * strings.cpp: Useful functions for manipulating strings
|
|
3 **/
|
|
4 #include "core/strings.h"
|
|
5 #include <codecvt>
|
|
6 #include <locale>
|
|
7 #include <string>
|
|
8 #include <vector>
|
|
9
|
|
10 namespace Strings {
|
|
11
|
|
12 std::string Implode(const std::vector<std::string>& vector, const std::string& delimiter) {
|
|
13 if (vector.size() < 1)
|
|
14 return "-";
|
|
15 std::string out = "";
|
|
16 for (unsigned long long i = 0; i < vector.size(); i++) {
|
|
17 out.append(vector.at(i));
|
|
18 if (i < vector.size() - 1)
|
|
19 out.append(delimiter);
|
|
20 }
|
|
21 return out;
|
|
22 }
|
|
23
|
|
24 std::string ReplaceAll(const std::string& string, const std::string& find, const std::string& replace) {
|
|
25 std::string result;
|
|
26 size_t pos, find_len = find.size(), from = 0;
|
|
27 while ((pos = string.find(find, from)) != std::string::npos) {
|
|
28 result.append(string, from, pos - from);
|
|
29 result.append(replace);
|
|
30 from = pos + find_len;
|
|
31 }
|
|
32 result.append(string, from, std::string::npos);
|
|
33 return result;
|
|
34 }
|
|
35
|
|
36 /* this function probably fucks your RAM but whatevs */
|
|
37 std::string SanitizeLineEndings(const std::string& string) {
|
|
38 std::string result(string);
|
|
39 result = ReplaceAll(result, "\r\n", "\n");
|
|
40 result = ReplaceAll(result, "<br>", "\n");
|
|
41 result = ReplaceAll(result, "\n\n\n", "\n\n");
|
|
42 return result;
|
|
43 }
|
|
44
|
|
45 std::string RemoveHtmlTags(const std::string& string) {
|
|
46 std::string html(string);
|
|
47 while (html.find("<") != std::string::npos) {
|
|
48 auto startpos = html.find("<");
|
|
49 auto endpos = html.find(">") + 1;
|
|
50
|
|
51 if (endpos != std::string::npos) {
|
|
52 html.erase(startpos, endpos - startpos);
|
|
53 }
|
|
54 }
|
|
55 return html;
|
|
56 }
|
|
57
|
|
58 std::string TextifySynopsis(const std::string& string) {
|
|
59 return RemoveHtmlTags(SanitizeLineEndings(string));
|
|
60 }
|
|
61
|
|
62 } // namespace Strings
|