2023-11-03 13:31:39 +00:00
// This file Copyright © Mnemosyne LLC.
2023-03-19 15:36:16 +00:00
// It may be used under GPLv2 (SPDX: GPL-2.0-only), GPLv3 (SPDX: GPL-3.0-only),
// or any future license endorsed by Mnemosyne LLC.
// License text can be found in the licenses/ folder.
#import <Foundation/Foundation.h>
#include <string>
#include <string_view>
2023-11-03 17:03:26 +00:00
#include "libtransmission/utils.h"
2023-03-19 15:36:16 +00:00
// macOS implementation of tr_strv_convert_utf8() that autodetects the encoding.
// This replaces the generic implementation of the function in utils.cc.
std::string tr_strv_convert_utf8(std::string_view sv)
{
2023-04-11 22:36:22 +00:00
// local pool for non-app tools like transmission-daemon, transmission-remote, transmission-create, ...
@autoreleasepool
2023-03-19 15:36:16 +00:00
{
2023-04-11 22:36:22 +00:00
// UTF-8 encoding
char const* validUTF8 = [[NSString alloc] initWithBytes:std::data(sv) length:std::size(sv) encoding:NSUTF8StringEncoding]
.UTF8String;
2023-03-19 15:36:16 +00:00
if (validUTF8)
{
return std::string(validUTF8);
}
2023-04-11 22:36:22 +00:00
// autodetection of the encoding (#3434)
NSString* convertedString;
NSStringEncoding stringEncoding = [NSString
stringEncodingForData:[NSData dataWithBytes:std::data(sv) length:std::size(sv)]
encodingOptions:@{
// We disallow lossy conversion, and will leave it to `utf8::unchecked::replace_invalid`.
NSStringEncodingDetectionAllowLossyKey : @NO,
// We only set the likely language.
// If we were to set suggested encodings, then whatever is listed first would take precedence on all others, making for instance kCFStringEncodingDOSJapanese (cp932) and kCFStringEncodingDOSRussian (cp866) taking priority on each other.
NSStringEncodingDetectionLikelyLanguageKey : NSLocale.currentLocale.languageCode
}
convertedString:&convertedString
usedLossyConversion:nil];
if (stringEncoding)
{
validUTF8 = convertedString.UTF8String;
if (validUTF8)
{
return std::string(validUTF8);
}
}
// invalid encoding
return tr_strv_replace_invalid(sv);
}
2023-03-19 15:36:16 +00:00
}