comparison src/core/strings.cc @ 230:2f5a9247e501

torrents: implement download button erg
author Paper <paper@paper.us.eu.org>
date Sat, 13 Jan 2024 09:42:02 -0500
parents 53211cb1e7f5
children 69f4768a820c
comparison
equal deleted inserted replaced
229:adc20fa321c1 230:2f5a9247e501
14 #include <codecvt> 14 #include <codecvt>
15 #include <locale> 15 #include <locale>
16 #include <string> 16 #include <string>
17 #include <vector> 17 #include <vector>
18 #include <unordered_map> 18 #include <unordered_map>
19 #include <iostream>
19 20
20 namespace Strings { 21 namespace Strings {
21 22
22 /* ew */ 23 /* ew */
23 std::string Implode(const std::vector<std::string>& vector, const std::string& delimiter) { 24 std::string Implode(const std::vector<std::string>& vector, const std::string& delimiter) {
130 std::string ToLower(const std::string& string) { 131 std::string ToLower(const std::string& string) {
131 return ToUtf8String(session.config.locale.GetLocale().toLower(ToQString(string))); 132 return ToUtf8String(session.config.locale.GetLocale().toLower(ToQString(string)));
132 } 133 }
133 134
134 std::wstring ToWstring(const std::string& string) { 135 std::wstring ToWstring(const std::string& string) {
135 static std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter; 136 static std::wstring_convert<std::codecvt_utf8<wchar_t>> converter("", L"");
136 return converter.from_bytes(string); 137
138 std::wstring wstr;
139 try {
140 wstr = converter.from_bytes(string);
141 } catch (std::range_error const& ex) {
142 std::cerr << "Failed to convert UTF-8 to wide string!" << std::endl;
143 }
144 return wstr;
137 } 145 }
138 146
139 std::wstring ToWstring(const QString& string) { 147 std::wstring ToWstring(const QString& string) {
140 std::wstring arr(string.size(), L'\0'); 148 std::wstring arr(string.size(), L'\0');
141 string.toWCharArray(&arr.front()); 149 string.toWCharArray(&arr.front());
142 return arr; 150 return arr;
143 } 151 }
144 152
145 std::string ToUtf8String(const std::wstring& wstring) { 153 std::string ToUtf8String(const std::wstring& wstring) {
146 static std::wstring_convert<std::codecvt_utf8<wchar_t>, wchar_t> converter; 154 static std::wstring_convert<std::codecvt_utf8<wchar_t>> converter("", L"");
147 return converter.to_bytes(wstring); 155 return converter.to_bytes(wstring);
148 } 156 }
149 157
150 std::string ToUtf8String(const QString& string) { 158 std::string ToUtf8String(const QString& string) {
151 const QByteArray ba = string.toUtf8(); 159 const QByteArray ba = string.toUtf8();