Mercurial > minori
diff src/core/strings.cc @ 405:4562bc8bfdff
strings: add conversion to/from CFString on Mac OS X
| author | Paper <paper@tflc.us> |
|---|---|
| date | Mon, 19 Jan 2026 22:47:29 -0500 |
| parents | df4a027623d0 |
| children |
line wrap: on
line diff
--- a/src/core/strings.cc Mon Jan 19 20:50:40 2026 -0500 +++ b/src/core/strings.cc Mon Jan 19 22:47:29 2026 -0500 @@ -206,6 +206,29 @@ return b ? "true" : "false"; // lol } +#if defined(__APPLE__) && defined(__MACH__) +CFStringRef ToCFString(const std::string &string) +{ + return CFStringCreateWithBytes(kCFAllocatorDefault, reinterpret_cast<const UInt8 *>(string.data()), string.size(), kCFStringEncodingUTF8, false); +} +std::string ToUtf8String(CFStringRef str) +{ + if (const char *ptr = CFStringGetCStringPtr(str, kCFStringEncodingUTF8)) + return std::string(ptr); // easy! + + // ... + const CFIndex len = CFStringGetLength(str); + std::string buf(CFStringGetMaximumSizeForEncoding(len, kCFStringEncodingUTF8), 0); + CFRange range; + range.length = len; + range.location = 0; + CFIndex used; + CFStringGetBytes(str, range, kCFStringEncodingUTF8, 0, false, reinterpret_cast<UInt8 *>(buf.data()), buf.size(), &used); + buf.resize(used); + return buf; +} +#endif + bool ToBool(const std::string &str, bool def) { std::istringstream s(Strings::ToLower(str));
