comparison src/core/strings.cc @ 365:f81bed4e04ac

*: megacommit that probably breaks things
author Paper <paper@paper.us.eu.org>
date Wed, 02 Oct 2024 23:06:43 -0400
parents a0aa8c8c4307
children
comparison
equal deleted inserted replaced
364:99c961c91809 365:f81bed4e04ac
80 } 80 }
81 81
82 /* this also performs case folding, so our string is lowercase after this */ 82 /* this also performs case folding, so our string is lowercase after this */
83 void NormalizeUnicode(std::string& string) { 83 void NormalizeUnicode(std::string& string) {
84 static constexpr utf8proc_option_t options = static_cast<utf8proc_option_t>( 84 static constexpr utf8proc_option_t options = static_cast<utf8proc_option_t>(
85 UTF8PROC_COMPAT | UTF8PROC_COMPOSE | UTF8PROC_STABLE | 85 UTF8PROC_COMPAT | UTF8PROC_COMPOSE | UTF8PROC_STABLE |
86 UTF8PROC_IGNORE | UTF8PROC_STRIPCC | UTF8PROC_STRIPMARK | 86 UTF8PROC_IGNORE | UTF8PROC_STRIPCC | UTF8PROC_STRIPMARK |
87 UTF8PROC_LUMP | UTF8PROC_CASEFOLD | UTF8PROC_NLF2LS 87 UTF8PROC_LUMP | UTF8PROC_CASEFOLD | UTF8PROC_NLF2LS
88 ); 88 );
89 89
90 /* ack */ 90 /* ack */
91 utf8proc_uint8_t* buf = nullptr; 91 utf8proc_uint8_t* buf = nullptr;
92 92
95 string.size(), 95 string.size(),
96 &buf, 96 &buf,
97 options 97 options
98 ); 98 );
99 99
100 if (size) 100 if (buf) {
101 string = std::string(reinterpret_cast<const char*>(buf), size); 101 if (size)
102 102 string.assign(reinterpret_cast<const char*>(buf), size);
103 if (buf) 103
104 free(buf); 104 std::free(buf);
105 }
105 } 106 }
106 107
107 void NormalizeAnimeTitle(std::string& string) { 108 void NormalizeAnimeTitle(std::string& string) {
108 ConvertRomanNumerals(string); 109 ConvertRomanNumerals(string);
109 NormalizeUnicode(string); 110 NormalizeUnicode(string);
188 std::istringstream s(Strings::ToLower(str)); 189 std::istringstream s(Strings::ToLower(str));
189 s >> std::boolalpha >> def; 190 s >> std::boolalpha >> def;
190 return def; 191 return def;
191 } 192 }
192 193
194 template<typename T>
195 constexpr T ipow(T num, unsigned int pow) {
196 return (pow >= sizeof(unsigned int)*8) ? 0 :
197 pow == 0 ? 1 : num * ipow(num, pow-1);
198 }
199
193 /* util funcs */ 200 /* util funcs */
194 uint64_t HumanReadableSizeToBytes(const std::string& str) { 201 uint64_t HumanReadableSizeToBytes(const std::string& str) {
195 static const std::unordered_map<std::string, uint64_t> bytes_map = { 202 static const std::unordered_map<std::string, uint64_t> bytes_map = {
196 {"KB", 1000ull}, 203 {"KB", 1e3},
197 {"MB", 1000000ull}, 204 {"MB", 1e6},
198 {"GB", 1000000000ull}, 205 {"GB", 1e9},
199 {"TB", 1000000000000ull}, 206 {"TB", 1e12},
200 {"PB", 1000000000000000ull}, 207 {"PB", 1e15},
201 {"KiB", 1ull << 10}, 208 {"KiB", 1ull << 10},
202 {"MiB", 1ull << 20}, 209 {"MiB", 1ull << 20},
203 {"GiB", 1ull << 30}, 210 {"GiB", 1ull << 30},
204 {"TiB", 1ull << 40}, 211 {"TiB", 1ull << 40},
205 {"PiB", 1ull << 50} /* surely we won't need more than this */ 212 {"PiB", 1ull << 50} /* surely we won't need more than this */