From 77c2f1c42c871a48449c058139235fb8e5857296 Mon Sep 17 00:00:00 2001 From: akirashirosawa <38557723+akirashirosawa@users.noreply.github.com> Date: Thu, 30 Apr 2020 20:59:05 +0300 Subject: [PATCH 1/2] add CFStringGetCString to getOSXSystemLang MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The functions that end with “Ptr” either return the desired pointer quickly, in constant time, or they return NULL. If the latter is the case, you should use CFStringGetCString. https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFStrings/Articles/AccessingContents.html --- src/translations.cpp | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/translations.cpp b/src/translations.cpp index b14ecc6388cc2..1d024ceb84dd9 100644 --- a/src/translations.cpp +++ b/src/translations.cpp @@ -246,15 +246,25 @@ std::string getOSXSystemLang() return "en_US"; } - const char *lang_code_raw = CFStringGetCStringPtr( - reinterpret_cast( CFArrayGetValueAtIndex( langs, 0 ) ), - kCFStringEncodingUTF8 ); - if( !lang_code_raw ) { - return "en_US"; + CFStringRef lang = static_cast( CFArrayGetValueAtIndex( langs, 0 ) ); + const char *lang_code_raw_fast = CFStringGetCStringPtr( lang, kCFStringEncodingUTF8 ); + std::string lang_code; + if( lang_code_raw_fast ) { // fast way, probably it's never works + lang_code = lang_code_raw_fast; + } else { // fallback to slow way + CFIndex length = CFStringGetLength( lang ) + 1; + char *lang_code_raw_slow = new char[length]; + bool success; + success = CFStringGetCString( lang, lang_code_raw_slow, length, kCFStringEncodingUTF8 ); + if( !success ) { + delete[] lang_code_raw_slow; + return "en_US"; + } + lang_code = lang_code_raw_slow; + delete[] lang_code_raw_slow; } // Convert to the underscore format expected by gettext - std::string lang_code( lang_code_raw ); std::replace( lang_code.begin(), lang_code.end(), '-', '_' ); /** From cf7d4223a02b5ad6d6d664358d2a6f1637fa6b5e Mon Sep 17 00:00:00 2001 From: akirashirosawa <38557723+akirashirosawa@users.noreply.github.com> Date: Thu, 30 Apr 2020 22:34:47 +0300 Subject: [PATCH 2/2] replace new[]-delete[] by vector Co-authored-by: John Bytheway --- src/translations.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/translations.cpp b/src/translations.cpp index 1d024ceb84dd9..ec591b98f6a41 100644 --- a/src/translations.cpp +++ b/src/translations.cpp @@ -253,15 +253,12 @@ std::string getOSXSystemLang() lang_code = lang_code_raw_fast; } else { // fallback to slow way CFIndex length = CFStringGetLength( lang ) + 1; - char *lang_code_raw_slow = new char[length]; - bool success; - success = CFStringGetCString( lang, lang_code_raw_slow, length, kCFStringEncodingUTF8 ); + std::vector lang_code_raw_slow( length, '\0' ); + bool success = CFStringGetCString( lang, lang_code_raw_slow.data(), length, kCFStringEncodingUTF8 ); if( !success ) { - delete[] lang_code_raw_slow; return "en_US"; } - lang_code = lang_code_raw_slow; - delete[] lang_code_raw_slow; + lang_code = lang_code_raw_slow.data(); } // Convert to the underscore format expected by gettext