annotate src/string_utils.cpp @ 8:b1f73678ef61

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