9
+ − 1 /**
+ − 2 * strings.cpp: Useful functions for manipulating strings
+ − 3 **/
+ − 4 #include "core/strings.h"
64
+ − 5 #include <QByteArray>
+ − 6 #include <QString>
15
+ − 7 #include <algorithm>
+ − 8 #include <cctype>
62
+ − 9 #include <codecvt>
9
+ − 10 #include <locale>
+ − 11 #include <string>
+ − 12 #include <vector>
+ − 13
+ − 14 namespace Strings {
+ − 15
+ − 16 std::string Implode(const std::vector<std::string>& vector, const std::string& delimiter) {
+ − 17 if (vector.size() < 1)
+ − 18 return "-";
+ − 19 std::string out = "";
+ − 20 for (unsigned long long i = 0; i < vector.size(); i++) {
+ − 21 out.append(vector.at(i));
+ − 22 if (i < vector.size() - 1)
+ − 23 out.append(delimiter);
+ − 24 }
+ − 25 return out;
+ − 26 }
+ − 27
+ − 28 std::string ReplaceAll(const std::string& string, const std::string& find, const std::string& replace) {
+ − 29 std::string result;
+ − 30 size_t pos, find_len = find.size(), from = 0;
+ − 31 while ((pos = string.find(find, from)) != std::string::npos) {
+ − 32 result.append(string, from, pos - from);
+ − 33 result.append(replace);
+ − 34 from = pos + find_len;
+ − 35 }
+ − 36 result.append(string, from, std::string::npos);
+ − 37 return result;
+ − 38 }
+ − 39
+ − 40 /* this function probably fucks your RAM but whatevs */
+ − 41 std::string SanitizeLineEndings(const std::string& string) {
+ − 42 std::string result(string);
+ − 43 result = ReplaceAll(result, "\r\n", "\n");
+ − 44 result = ReplaceAll(result, "<br>", "\n");
+ − 45 result = ReplaceAll(result, "\n\n\n", "\n\n");
+ − 46 return result;
+ − 47 }
+ − 48
+ − 49 std::string RemoveHtmlTags(const std::string& string) {
+ − 50 std::string html(string);
+ − 51 while (html.find("<") != std::string::npos) {
+ − 52 auto startpos = html.find("<");
+ − 53 auto endpos = html.find(">") + 1;
+ − 54
+ − 55 if (endpos != std::string::npos) {
+ − 56 html.erase(startpos, endpos - startpos);
+ − 57 }
+ − 58 }
+ − 59 return html;
+ − 60 }
+ − 61
+ − 62 std::string TextifySynopsis(const std::string& string) {
+ − 63 return RemoveHtmlTags(SanitizeLineEndings(string));
+ − 64 }
+ − 65
15
+ − 66 /* these functions suck for i18n!...
64
+ − 67 but we only use them with JSON
+ − 68 stuff anyway */
15
+ − 69 std::string ToUpper(const std::string& string) {
+ − 70 std::string result(string);
+ − 71 std::transform(result.begin(), result.end(), result.begin(), [](unsigned char c) { return std::toupper(c); });
+ − 72 return result;
+ − 73 }
+ − 74
+ − 75 std::string ToLower(const std::string& string) {
+ − 76 std::string result(string);
+ − 77 std::transform(result.begin(), result.end(), result.begin(), [](unsigned char c) { return std::tolower(c); });
+ − 78 return result;
+ − 79 }
+ − 80
62
+ − 81 std::wstring ToWstring(const std::string& string) {
+ − 82 std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter;
+ − 83 return converter.from_bytes(string);
+ − 84 }
+ − 85
64
+ − 86 std::wstring ToWstring(const QString& string) {
+ − 87 std::wstring arr(string.size(), L'\0');
+ − 88 string.toWCharArray(&arr.front());
+ − 89 return arr;
+ − 90 }
+ − 91
62
+ − 92 std::string ToUtf8String(const std::wstring& wstring) {
+ − 93 std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter;
+ − 94 return converter.to_bytes(wstring);
+ − 95 }
+ − 96
64
+ − 97 std::string ToUtf8String(const QString& string) {
+ − 98 QByteArray ba = string.toUtf8();
+ − 99 return std::string(ba.data(), ba.size());
+ − 100 }
+ − 101
+ − 102 QString ToQString(const std::string& string) {
+ − 103 return QString::fromUtf8(string.c_str(), string.length());
+ − 104 }
+ − 105
+ − 106 QString ToQString(const std::wstring& wstring) {
+ − 107 return QString::fromWCharArray(wstring.c_str(), wstring.length());
+ − 108 }
+ − 109
9
+ − 110 } // namespace Strings