annotate src/string_utils.cpp @ 5:51ae25154b70

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