comparison src/string_utils.cpp @ 1:1ae666fdf9e2

*: initial commit
author Paper <mrpapersonic@gmail.com>
date Tue, 08 Aug 2023 19:49:15 -0400
parents
children 23d0d9319a00
comparison
equal deleted inserted replaced
0:5a76e1b94163 1:1ae666fdf9e2
1 /**
2 * string_utils.cpp: Useful functions for manipulating strings
3 *
4 * Every function in here *should* have a working wstring equivalent.
5 **/
6 #include <vector>
7 #include <string>
8 #include <codecvt>
9 #include <locale>
10 #include "string_utils.h"
11
12 /* It's actually pretty insane how the standard library still doesn't
13 have a function for this. Look at how simple this is. */
14 std::string StringUtils::Implode(const std::vector<std::string>& vector,
15 const std::string& delimiter) {
16 std::string out = "";
17 for (int 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::wstring StringUtils::Implode(const std::vector<std::wstring>& vector,
26 const std::wstring& delimiter) {
27 std::wstring out = L"";
28 for (int i = 0; i < vector.size(); i++) {
29 out.append(vector.at(i));
30 if (i < vector.size()-1)
31 out.append(delimiter);
32 }
33 return out;
34 }
35
36 std::string StringUtils::WstrToUtf8(const std::wstring& string) {
37 std::wstring_convert<std::codecvt_utf8<wchar_t>> convert;
38 return convert.to_bytes(string);
39 }
40
41 std::wstring StringUtils::Utf8ToWstr(const std::string& string) {
42 std::wstring_convert<std::codecvt_utf8<wchar_t>> convert;
43 return convert.from_bytes(string);
44 }
45
46 std::string StringUtils::ReplaceAll(const std::string& string,
47 const std::string& find,
48 const std::string& replace) {
49 std::string result;
50 size_t pos, find_len = find.size(), from = 0;
51 while ((pos=string.find(find,from)) != std::string::npos) {
52 result.append(string, from, pos - from);
53 result.append(replace);
54 from = pos + find_len;
55 }
56 result.append(string, from, std::string::npos);
57 return result;
58 }
59
60 std::wstring StringUtils::ReplaceAll(const std::wstring& string,
61 const std::wstring& find,
62 const std::wstring& replace) {
63 std::wstring result;
64 size_t pos, find_len = find.size(), from = 0;
65 while ((pos=string.find(find,from)) != std::wstring::npos) {
66 result.append(string, from, pos - from);
67 result.append(replace);
68 from = pos + find_len;
69 }
70 result.append(string, from, std::wstring::npos);
71 return result;
72 }
73
74 /* this function probably fucks your RAM but whatevs */
75 std::string StringUtils::SanitizeLineEndings(const std::string& string) {
76 std::string result(string);
77 result = ReplaceAll(result, "\r\n", "\n");
78 result = ReplaceAll(result, "<br>", "\n");
79 result = ReplaceAll(result, "\n\n\n", "\n\n");
80 return result;
81 }
82
83 std::wstring StringUtils::SanitizeLineEndings(const std::wstring& string) {
84 std::wstring result(string);
85 result = ReplaceAll(result, L"\r\n", L"\n");
86 result = ReplaceAll(result, L"<br>", L"\n");
87 result = ReplaceAll(result, L"\n\n\n", L"\n\n");
88 return result;
89 }
90
91 std::string StringUtils::RemoveHtmlTags(const std::string& string) {
92 std::string html(string);
93 while (html.find("<") != std::string::npos)
94 {
95 auto startpos = html.find("<");
96 auto endpos = html.find(">") + 1;
97
98 if (endpos != std::string::npos)
99 {
100 html.erase(startpos, endpos - startpos);
101 }
102 }
103 return html;
104 }
105
106 std::wstring StringUtils::RemoveHtmlTags(const std::wstring& string) {
107 std::wstring html(string);
108 while (html.find(L"<") != std::wstring::npos)
109 {
110 auto startpos = html.find(L"<");
111 auto endpos = html.find(L">") + 1;
112
113 if (endpos != std::wstring::npos)
114 {
115 html.erase(startpos, endpos - startpos);
116 }
117 }
118 return html;
119 }
120
121 std::string StringUtils::TextifySynopsis(const std::string& string) {
122 std::string result = SanitizeLineEndings(string);
123 result = RemoveHtmlTags(string);
124 return result;
125 }
126
127 std::wstring StringUtils::TextifySynopsis(const std::wstring& string) {
128 std::wstring result = SanitizeLineEndings(string);
129 result = RemoveHtmlTags(string);
130 return result;
131 }