diff src/core/strings.cc @ 81:9b2b41f83a5e

boring: mass rename to cc because this is a very unix-y project, it makes more sense to use the 'cc' extension
author Paper <mrpapersonic@gmail.com>
date Mon, 23 Oct 2023 12:07:27 -0400
parents src/core/strings.cpp@6f7385bd334c
children 582b2fca1561
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/core/strings.cc	Mon Oct 23 12:07:27 2023 -0400
@@ -0,0 +1,114 @@
+/**
+ * strings.cpp: Useful functions for manipulating strings
+ **/
+#include "core/strings.h"
+#include <QByteArray>
+#include <QString>
+#include <algorithm>
+#include <cctype>
+#include <codecvt>
+#include <locale>
+#include <string>
+#include <vector>
+
+namespace Strings {
+
+std::string Implode(const std::vector<std::string>& vector, const std::string& delimiter) {
+	if (vector.size() < 1)
+		return "-";
+	std::string out = "";
+	for (unsigned long long i = 0; i < vector.size(); i++) {
+		out.append(vector.at(i));
+		if (i < vector.size() - 1)
+			out.append(delimiter);
+	}
+	return out;
+}
+
+std::string ReplaceAll(const std::string& string, const std::string& find, const std::string& replace) {
+	std::string result;
+	size_t pos, find_len = find.size(), from = 0;
+	while ((pos = string.find(find, from)) != std::string::npos) {
+		result.append(string, from, pos - from);
+		result.append(replace);
+		from = pos + find_len;
+	}
+	result.append(string, from, std::string::npos);
+	return result;
+}
+
+/* this function probably fucks your RAM but whatevs */
+std::string SanitizeLineEndings(const std::string& string) {
+	std::string result(string);
+	result = ReplaceAll(result, "\r\n", "\n");
+	result = ReplaceAll(result, "<br>", "\n");
+	result = ReplaceAll(result, "\n\n\n", "\n\n");
+	return result;
+}
+
+std::string RemoveHtmlTags(const std::string& string) {
+	std::string html(string);
+	while (html.find("<") != std::string::npos) {
+		auto startpos = html.find("<");
+		auto endpos = html.find(">") + 1;
+
+		if (endpos != std::string::npos) {
+			html.erase(startpos, endpos - startpos);
+		}
+	}
+	return html;
+}
+
+std::string TextifySynopsis(const std::string& string) {
+	return RemoveHtmlTags(SanitizeLineEndings(string));
+}
+
+/* these functions suck for i18n!...
+    but we only use them with JSON
+    stuff anyway */
+std::string ToUpper(const std::string& string) {
+	std::string result(string);
+	std::transform(result.begin(), result.end(), result.begin(), [](unsigned char c) { return std::toupper(c); });
+	return result;
+}
+
+std::string ToLower(const std::string& string) {
+	std::string result(string);
+	std::transform(result.begin(), result.end(), result.begin(), [](unsigned char c) { return std::tolower(c); });
+	return result;
+}
+
+std::wstring ToWstring(const std::string& string) {
+	std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter;
+	return converter.from_bytes(string);
+}
+
+std::wstring ToWstring(const QString& string) {
+	std::wstring arr(string.size(), L'\0');
+	string.toWCharArray(&arr.front());
+	return arr;
+}
+
+std::string ToUtf8String(const std::wstring& wstring) {
+	std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter;
+	return converter.to_bytes(wstring);
+}
+
+std::string ToUtf8String(const QString& string) {
+	QByteArray ba = string.toUtf8();
+	return std::string(ba.constData(), ba.size());
+}
+
+std::string ToUtf8String(const QByteArray& ba) {
+	return std::string(ba.constData(), ba.size());
+}
+
+QString ToQString(const std::string& string) {
+	return QString::fromUtf8(string.c_str(), string.length());
+}
+
+QString ToQString(const std::wstring& wstring) {
+	return QString::fromWCharArray(wstring.c_str(), wstring.length());
+}
+
+} // namespace Strings