Mercurial > minori
comparison src/core/strings.cc @ 413:192da585a0a8 default tip
*: fix formatting
the clang-format file was broken andjust using LLVM defaults.
now it's fixed and is more in line with what I actually prefer my
code to look like -- esp. with regards to tabulators
| author | Paper <paper@tflc.us> |
|---|---|
| date | Sat, 25 Jul 2026 14:54:32 -0400 |
| parents | 02a670a8e1c4 |
| children |
comparison
equal
deleted
inserted
replaced
| 412:05aed03e0111 | 413:192da585a0a8 |
|---|---|
| 72 } | 72 } |
| 73 | 73 |
| 74 void ConvertRomanNumerals(std::string &string) | 74 void ConvertRomanNumerals(std::string &string) |
| 75 { | 75 { |
| 76 static const std::vector<std::pair<std::string_view, std::string_view>> vec = { | 76 static const std::vector<std::pair<std::string_view, std::string_view>> vec = { |
| 77 {"2", "II" }, | 77 {"2", "II" }, |
| 78 {"3", "III" }, | 78 {"3", "III" }, |
| 79 {"4", "IV" }, | 79 {"4", "IV" }, |
| 80 {"5", "V" }, | 80 {"5", "V" }, |
| 81 {"6", "VI" }, | 81 {"6", "VI" }, |
| 82 {"7", "VII" }, | 82 {"7", "VII" }, |
| 83 {"8", "VIII"}, | 83 {"8", "VIII"}, |
| 84 {"9", "IX" }, | 84 {"9", "IX" }, |
| 85 {"11", "XI" }, | 85 {"11", "XI" }, |
| 86 {"12", "XII" }, | 86 {"12", "XII" }, |
| 87 {"13", "XIII"} | 87 {"13", "XIII"} |
| 88 }; | 88 }; |
| 89 | 89 |
| 90 for (const auto &item : vec) | 90 for (const auto &item : vec) |
| 93 | 93 |
| 94 /* this also performs case folding, so our string is lowercase after this */ | 94 /* this also performs case folding, so our string is lowercase after this */ |
| 95 void NormalizeUnicode(std::string &string) | 95 void NormalizeUnicode(std::string &string) |
| 96 { | 96 { |
| 97 static constexpr utf8proc_option_t options = static_cast<utf8proc_option_t>( | 97 static constexpr utf8proc_option_t options = static_cast<utf8proc_option_t>( |
| 98 UTF8PROC_COMPAT | UTF8PROC_COMPOSE | UTF8PROC_STABLE | UTF8PROC_IGNORE | UTF8PROC_STRIPCC | UTF8PROC_STRIPMARK | | 98 UTF8PROC_COMPAT | UTF8PROC_COMPOSE | UTF8PROC_STABLE | UTF8PROC_IGNORE | UTF8PROC_STRIPCC | UTF8PROC_STRIPMARK | |
| 99 UTF8PROC_LUMP | UTF8PROC_CASEFOLD | UTF8PROC_NLF2LS); | 99 UTF8PROC_LUMP | UTF8PROC_CASEFOLD | UTF8PROC_NLF2LS); |
| 100 | 100 |
| 101 /* ack */ | 101 /* ack */ |
| 102 utf8proc_uint8_t *buf = nullptr; | 102 utf8proc_uint8_t *buf = nullptr; |
| 103 | 103 |
| 104 const utf8proc_ssize_t size = | 104 const utf8proc_ssize_t size = |
| 105 utf8proc_map(reinterpret_cast<const utf8proc_uint8_t *>(string.data()), string.size(), &buf, options); | 105 utf8proc_map(reinterpret_cast<const utf8proc_uint8_t *>(string.data()), string.size(), &buf, options); |
| 106 | 106 |
| 107 if (buf) { | 107 if (buf) { |
| 108 if (size) | 108 if (size) |
| 109 string.assign(reinterpret_cast<const char *>(buf), size); | 109 string.assign(reinterpret_cast<const char *>(buf), size); |
| 110 | 110 |
| 214 } | 214 } |
| 215 | 215 |
| 216 #if defined(__APPLE__) && defined(__MACH__) | 216 #if defined(__APPLE__) && defined(__MACH__) |
| 217 CFStringRef ToCFString(const std::string &string) | 217 CFStringRef ToCFString(const std::string &string) |
| 218 { | 218 { |
| 219 return CFStringCreateWithBytes(kCFAllocatorDefault, reinterpret_cast<const UInt8 *>(string.data()), string.size(), kCFStringEncodingUTF8, false); | 219 return CFStringCreateWithBytes(kCFAllocatorDefault, reinterpret_cast<const UInt8 *>(string.data()), string.size(), |
| 220 kCFStringEncodingUTF8, false); | |
| 220 } | 221 } |
| 221 std::string ToUtf8String(CFStringRef str) | 222 std::string ToUtf8String(CFStringRef str) |
| 222 { | 223 { |
| 223 if (const char *ptr = CFStringGetCStringPtr(str, kCFStringEncodingUTF8)) | 224 if (const char *ptr = CFStringGetCStringPtr(str, kCFStringEncodingUTF8)) |
| 224 return std::string(ptr); // easy! | 225 return std::string(ptr); // easy! |
| 228 std::string buf(CFStringGetMaximumSizeForEncoding(len, kCFStringEncodingUTF8), 0); | 229 std::string buf(CFStringGetMaximumSizeForEncoding(len, kCFStringEncodingUTF8), 0); |
| 229 CFRange range; | 230 CFRange range; |
| 230 range.length = len; | 231 range.length = len; |
| 231 range.location = 0; | 232 range.location = 0; |
| 232 CFIndex used; | 233 CFIndex used; |
| 233 CFStringGetBytes(str, range, kCFStringEncodingUTF8, 0, false, reinterpret_cast<UInt8 *>(buf.data()), buf.size(), &used); | 234 CFStringGetBytes( |
| 235 str, range, kCFStringEncodingUTF8, 0, false, reinterpret_cast<UInt8 *>(buf.data()), buf.size(), &used); | |
| 234 buf.resize(used); | 236 buf.resize(used); |
| 235 return buf; | 237 return buf; |
| 236 } | 238 } |
| 237 #endif | 239 #endif |
| 238 | 240 |
| 251 | 253 |
| 252 /* util funcs */ | 254 /* util funcs */ |
| 253 uint64_t HumanReadableSizeToBytes(const std::string &str) | 255 uint64_t HumanReadableSizeToBytes(const std::string &str) |
| 254 { | 256 { |
| 255 static const std::unordered_map<std::string, uint64_t> bytes_map = { | 257 static const std::unordered_map<std::string, uint64_t> bytes_map = { |
| 256 {"KB", 1e3 }, | 258 {"KB", 1e3 }, |
| 257 {"MB", 1e6 }, | 259 {"MB", 1e6 }, |
| 258 {"GB", 1e9 }, | 260 {"GB", 1e9 }, |
| 259 {"TB", 1e12 }, | 261 {"TB", 1e12 }, |
| 260 {"PB", 1e15 }, | 262 {"PB", 1e15 }, |
| 261 {"KiB", 1ull << 10}, | 263 {"KiB", 1ull << 10}, |
| 262 {"MiB", 1ull << 20}, | 264 {"MiB", 1ull << 20}, |
| 263 {"GiB", 1ull << 30}, | 265 {"GiB", 1ull << 30}, |
| 264 {"TiB", 1ull << 40}, | 266 {"TiB", 1ull << 40}, |
| 265 {"PiB", 1ull << 50} /* surely we won't need more than this */ | 267 {"PiB", 1ull << 50} /* surely we won't need more than this */ |
| 266 }; | 268 }; |
| 267 | 269 |
| 268 for (const auto &suffix : bytes_map) { | 270 for (const auto &suffix : bytes_map) { |
| 269 if (str.find(suffix.first) != std::string::npos) { | 271 if (str.find(suffix.first) != std::string::npos) { |
| 284 #if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0) | 286 #if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0) |
| 285 /* QLocale in Qt >= 5.10.0 has a function for this */ | 287 /* QLocale in Qt >= 5.10.0 has a function for this */ |
| 286 return Strings::ToUtf8String(session.config.locale.GetLocale().formattedDataSize(bytes, precision)); | 288 return Strings::ToUtf8String(session.config.locale.GetLocale().formattedDataSize(bytes, precision)); |
| 287 #else | 289 #else |
| 288 static const std::unordered_map<uint64_t, std::string> map = { | 290 static const std::unordered_map<uint64_t, std::string> map = { |
| 289 {1ull << 10, "KiB"}, | 291 {1ull << 10, "KiB"}, |
| 290 {1ull << 20, "MiB"}, | 292 {1ull << 20, "MiB"}, |
| 291 {1ull << 30, "GiB"}, | 293 {1ull << 30, "GiB"}, |
| 292 {1ull << 40, "TiB"}, | 294 {1ull << 40, "TiB"}, |
| 293 {1ull << 50, "PiB"} | 295 {1ull << 50, "PiB"} |
| 294 }; | 296 }; |
| 337 /* if that doesn't exist, use the GTK theme and check for some known | 339 /* if that doesn't exist, use the GTK theme and check for some known |
| 338 * suffixes. if one is found, return | 340 * suffixes. if one is found, return |
| 339 * | 341 * |
| 340 * XXX probably better to use case folding here */ | 342 * XXX probably better to use case folding here */ |
| 341 static constexpr std::array<std::string_view, 3> suffixes = { | 343 static constexpr std::array<std::string_view, 3> suffixes = { |
| 342 "-dark", /* Adwaita-dark */ | 344 "-dark", /* Adwaita-dark */ |
| 343 "-Dark", /* Arc-Dark */ | 345 "-Dark", /* Arc-Dark */ |
| 344 "-Darker", /* Arc-Darker */ | 346 "-Darker", /* Arc-Darker */ |
| 345 }; | 347 }; |
| 346 | 348 |
| 347 for (const auto &suffix : suffixes) { | 349 for (const auto &suffix : suffixes) { |
| 348 if (str.size() < suffix.size()) | 350 if (str.size() < suffix.size()) |
| 349 continue; | 351 continue; |
| 350 | 352 |
| 351 if (std::equal(str.data() + str.size() - suffix.length(), str.data() + str.size(), suffix.begin(), | 353 if (std::equal( |
| 352 suffix.end())) | 354 str.data() + str.size() - suffix.length(), str.data() + str.size(), suffix.begin(), suffix.end())) |
| 353 return true; | 355 return true; |
| 354 } | 356 } |
| 355 | 357 |
| 356 return false; | 358 return false; |
| 357 } | 359 } |
