diff src/core/strings.cc @ 369:47c9f8502269

*: clang-format all the things I've edited the formatting a bit. Now pointer asterisks (and reference ampersands) are on the variable instead of the type, as well as having newlines for function braces (but nothing else)
author Paper <paper@tflc.us>
date Fri, 25 Jul 2025 10:16:02 -0400
parents f81bed4e04ac
children
line wrap: on
line diff
--- a/src/core/strings.cc	Fri Jul 25 10:05:23 2025 -0400
+++ b/src/core/strings.cc	Fri Jul 25 10:16:02 2025 -0400
@@ -5,17 +5,17 @@
 #include "core/session.h" // locale
 
 #include <QByteArray>
+#include <QCoreApplication>
 #include <QDebug>
 #include <QLocale>
 #include <QString>
 #include <QTextDocument>
-#include <QCoreApplication>
 
 #include <algorithm>
 #include <cctype>
 #include <codecvt>
+#include <iomanip>
 #include <iostream>
-#include <iomanip>
 #include <locale>
 #include <string>
 #include <unordered_map>
@@ -26,7 +26,8 @@
 namespace Strings {
 
 /* ew */
-std::string Implode(const std::vector<std::string>& vector, const std::string& delimiter) {
+std::string Implode(const std::vector<std::string> &vector, const std::string &delimiter)
+{
 	if (vector.size() < 1)
 		return "";
 
@@ -41,7 +42,8 @@
 	return out;
 }
 
-std::vector<std::string> Split(const std::string& text, const std::string& delimiter) {
+std::vector<std::string> Split(const std::string &text, const std::string &delimiter)
+{
 	if (text.length() < 1)
 		return {};
 
@@ -60,7 +62,8 @@
 /* This function is really only used for cleaning up the synopsis of
  * horrible HTML debris from AniList :)
  */
-void ReplaceAll(std::string& string, std::string_view find, std::string_view replace) {
+void ReplaceAll(std::string &string, std::string_view find, std::string_view replace)
+{
 	size_t pos = 0;
 	while ((pos = string.find(find, pos)) != std::string::npos) {
 		string.replace(pos, find.length(), replace);
@@ -68,51 +71,57 @@
 	}
 }
 
-void ConvertRomanNumerals(std::string& string) {
+void ConvertRomanNumerals(std::string &string)
+{
 	static const std::vector<std::pair<std::string_view, std::string_view>> vec = {
-		{"2", "II"}, {"3", "III"}, {"4", "IV"}, {"5", "V"}, {"6", "VI"},
-		{"7", "VII"}, {"8", "VIII"}, {"9", "IX"}, {"11", "XI"}, {"12", "XII"},
-		{"13", "XIII"}
-	};
+	    {"2",  "II"  },
+        {"3",  "III" },
+        {"4",  "IV"  },
+        {"5",  "V"   },
+        {"6",  "VI"  },
+        {"7",  "VII" },
+	    {"8",  "VIII"},
+        {"9",  "IX"  },
+        {"11", "XI"  },
+        {"12", "XII" },
+        {"13", "XIII"}
+    };
 
-	for (const auto& item : vec)
+	for (const auto &item : vec)
 		ReplaceAll(string, item.second, item.first);
 }
 
 /* this also performs case folding, so our string is lowercase after this */
-void NormalizeUnicode(std::string& string) {
+void NormalizeUnicode(std::string &string)
+{
 	static constexpr utf8proc_option_t options = static_cast<utf8proc_option_t>(
-		UTF8PROC_COMPAT | UTF8PROC_COMPOSE  | UTF8PROC_STABLE |
-		UTF8PROC_IGNORE | UTF8PROC_STRIPCC  | UTF8PROC_STRIPMARK |
-		UTF8PROC_LUMP   | UTF8PROC_CASEFOLD | UTF8PROC_NLF2LS
-	);
+	    UTF8PROC_COMPAT | UTF8PROC_COMPOSE | UTF8PROC_STABLE | UTF8PROC_IGNORE | UTF8PROC_STRIPCC | UTF8PROC_STRIPMARK |
+	    UTF8PROC_LUMP | UTF8PROC_CASEFOLD | UTF8PROC_NLF2LS);
 
 	/* ack */
-	utf8proc_uint8_t* buf = nullptr;
+	utf8proc_uint8_t *buf = nullptr;
 
-	const utf8proc_ssize_t size = utf8proc_map(
-		reinterpret_cast<const utf8proc_uint8_t*>(string.data()),
-		string.size(),
-		&buf,
-		options
-	);
+	const utf8proc_ssize_t size =
+	    utf8proc_map(reinterpret_cast<const utf8proc_uint8_t *>(string.data()), string.size(), &buf, options);
 
 	if (buf) {
 		if (size)
-			string.assign(reinterpret_cast<const char*>(buf), size);
+			string.assign(reinterpret_cast<const char *>(buf), size);
 
 		std::free(buf);
 	}
 }
 
-void NormalizeAnimeTitle(std::string& string) {
+void NormalizeAnimeTitle(std::string &string)
+{
 	ConvertRomanNumerals(string);
 	NormalizeUnicode(string);
 	RemoveLeadingChars(string, ' ');
 	RemoveTrailingChars(string, ' ');
 }
 
-void TextifySynopsis(std::string& string) {
+void TextifySynopsis(std::string &string)
+{
 	/* Just let Qt deal with it. */
 	QTextDocument text;
 	text.setHtml(Strings::ToQString(string));
@@ -122,102 +131,116 @@
 /* let Qt handle the heavy lifting of locale shit
  * I don't want to deal with
  */
-std::string ToUpper(const std::string& string) {
+std::string ToUpper(const std::string &string)
+{
 	return ToUtf8String(session.config.locale.GetLocale().toUpper(ToQString(string)));
 }
 
-std::string ToLower(const std::string& string) {
+std::string ToLower(const std::string &string)
+{
 	return ToUtf8String(session.config.locale.GetLocale().toLower(ToQString(string)));
 }
 
-std::wstring ToWstring(const std::string& string) {
+std::wstring ToWstring(const std::string &string)
+{
 	static std::wstring_convert<std::codecvt_utf8<wchar_t>> converter("", L"");
 
 	std::wstring wstr;
 	try {
 		wstr = converter.from_bytes(string);
-	} catch (std::range_error const& ex) {
+	} catch (std::range_error const &ex) {
 		/* XXX how? */
 		std::cerr << "Failed to convert UTF-8 to wide string!" << std::endl;
 	}
 	return wstr;
 }
 
-std::wstring ToWstring(const QString& 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::string ToUtf8String(const std::wstring &wstring)
+{
 	static std::wstring_convert<std::codecvt_utf8<wchar_t>> converter("", L"");
 	return converter.to_bytes(wstring);
 }
 
-std::string ToUtf8String(const std::u32string& u32string) {
+std::string ToUtf8String(const std::u32string &u32string)
+{
 	static std::wstring_convert<std::codecvt_utf8_utf16<char32_t>, char32_t> converter;
 	return converter.to_bytes(u32string);
 }
 
-std::u32string ToUcs4String(const std::string& string) {
+std::u32string ToUcs4String(const std::string &string)
+{
 	static std::wstring_convert<std::codecvt_utf8_utf16<char32_t>, char32_t> converter;
 	return converter.from_bytes(string);
 }
 
-std::string ToUtf8String(const QString& string) {
+std::string ToUtf8String(const QString &string)
+{
 	const QByteArray ba = string.toUtf8();
 	return std::string(ba.constData(), ba.size());
 }
 
-std::string ToUtf8String(const QByteArray& ba) {
+std::string ToUtf8String(const QByteArray &ba)
+{
 	return std::string(ba.constData(), ba.size());
 }
 
-QString ToQString(const std::string& string) {
+QString ToQString(const std::string &string)
+{
 	return QString::fromUtf8(string.c_str(), string.length());
 }
 
-QString ToQString(const std::wstring& wstring) {
+QString ToQString(const std::wstring &wstring)
+{
 	return QString::fromWCharArray(wstring.c_str(), wstring.length());
 }
 
-std::string ToUtf8String(const bool b) {
+std::string ToUtf8String(const bool b)
+{
 	return b ? "true" : "false"; // lol
 }
 
-bool ToBool(const std::string& str, bool def) {
+bool ToBool(const std::string &str, bool def)
+{
 	std::istringstream s(Strings::ToLower(str));
 	s >> std::boolalpha >> def;
 	return def;
 }
 
 template<typename T>
-constexpr T ipow(T num, unsigned int pow) {
-    return (pow >= sizeof(unsigned int)*8) ? 0 :
-        pow == 0 ? 1 : num * ipow(num, pow-1);
+constexpr T ipow(T num, unsigned int pow)
+{
+	return (pow >= sizeof(unsigned int) * 8) ? 0 : pow == 0 ? 1 : num * ipow(num, pow - 1);
 }
 
 /* util funcs */
-uint64_t HumanReadableSizeToBytes(const std::string& str) {
+uint64_t HumanReadableSizeToBytes(const std::string &str)
+{
 	static const std::unordered_map<std::string, uint64_t> bytes_map = {
-		{"KB",  1e3},
-		{"MB",  1e6},
-		{"GB",  1e9},
-		{"TB",  1e12},
-		{"PB",  1e15},
-	    {"KiB", 1ull << 10},
-	    {"MiB", 1ull << 20},
-	    {"GiB", 1ull << 30},
+	    {"KB",  1e3       },
+        {"MB",  1e6       },
+        {"GB",  1e9       },
+        {"TB",  1e12      },
+	    {"PB",  1e15      },
+        {"KiB", 1ull << 10},
+        {"MiB", 1ull << 20},
+        {"GiB", 1ull << 30},
 	    {"TiB", 1ull << 40},
-	    {"PiB", 1ull << 50}  /* surely we won't need more than this */
+        {"PiB", 1ull << 50}  /* surely we won't need more than this */
 	};
 
-	for (const auto& suffix : bytes_map) {
+	for (const auto &suffix : bytes_map) {
 		if (str.find(suffix.first) != std::string::npos) {
 			try {
 				uint64_t size = std::stod(str) * suffix.second;
 				return size;
-			} catch (std::invalid_argument const& ex) {
+			} catch (std::invalid_argument const &ex) {
 				continue;
 			}
 		}
@@ -226,27 +249,26 @@
 	return ToInt(str, 0);
 }
 
-std::string BytesToHumanReadableSize(uint64_t bytes, int precision) {
+std::string BytesToHumanReadableSize(uint64_t bytes, int precision)
+{
 #if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
 	/* QLocale in Qt >= 5.10.0 has a function for this */
 	return Strings::ToUtf8String(session.config.locale.GetLocale().formattedDataSize(bytes, precision));
 #else
 	static const std::unordered_map<uint64_t, std::string> map = {
-		{1ull << 10, "KiB"},
-		{1ull << 20, "MiB"},
-		{1ull << 30, "GiB"},
-		{1ull << 40, "TiB"},
-		{1ull << 50, "PiB"}
-	};
+	    {1ull << 10, "KiB"},
+        {1ull << 20, "MiB"},
+        {1ull << 30, "GiB"},
+        {1ull << 40, "TiB"},
+        {1ull << 50, "PiB"}
+    };
 
-	for (const auto& suffix : map) {
+	for (const auto &suffix : map) {
 		if (bytes / suffix.first < 1)
 			continue;
 
 		std::stringstream ss;
-		ss << std::setprecision(precision)
-		   << (static_cast<double>(bytes) / suffix.first) << " "
-		   << suffix.second;
+		ss << std::setprecision(precision) << (static_cast<double>(bytes) / suffix.first) << " " << suffix.second;
 		return ss.str();
 	}
 
@@ -255,15 +277,18 @@
 #endif
 }
 
-void RemoveLeadingChars(std::string& s, const char c) {
+void RemoveLeadingChars(std::string &s, const char c)
+{
 	s.erase(0, std::min(s.find_first_not_of(c), s.size() - 1));
 }
 
-void RemoveTrailingChars(std::string& s, const char c) {
+void RemoveTrailingChars(std::string &s, const char c)
+{
 	s.erase(s.find_last_not_of(c) + 1, std::string::npos);
 }
 
-bool BeginningMatchesSubstring(const std::string& str, const std::string& sub) {
+bool BeginningMatchesSubstring(const std::string &str, const std::string &sub)
+{
 	for (unsigned long long i = 0; i < str.length() && i < sub.length(); i++)
 		if (str[i] != sub[i])
 			return false;
@@ -271,7 +296,8 @@
 	return true;
 }
 
-std::string Translate(const char* str) {
+std::string Translate(const char *str)
+{
 	return Strings::ToUtf8String(QCoreApplication::tr(str));
 }