Bug Fix: Handle invalid UTF-8 decoding errors
Apparently, stringWithUTF8String: just returns nil when the encoding is not UTF-8, rather than throwing an exception. Signed-off-by: Christopher Snowhill <kode54@gmail.com>
This commit is contained in:
parent
845b33e422
commit
2b52d2a766
1 changed files with 15 additions and 7 deletions
|
@ -112,13 +112,21 @@
|
|||
|
||||
static NSString *guess_encoding_of_string(const char *input) {
|
||||
NSString *ret = @"";
|
||||
@try {
|
||||
ret = [NSString stringWithUTF8String:input];
|
||||
}
|
||||
@catch(NSException *e) {
|
||||
// This method is incredibly slow
|
||||
NSData *stringData = [NSData dataWithBytes:input length:strlen(input)];
|
||||
[NSString stringEncodingForData:stringData encodingOptions:nil convertedString:&ret usedLossyConversion:nil];
|
||||
if(input && *input) {
|
||||
@try {
|
||||
ret = [NSString stringWithUTF8String:input];
|
||||
}
|
||||
@catch(NSException *e) {
|
||||
ret = nil;
|
||||
}
|
||||
if(!ret) {
|
||||
// This method is incredibly slow
|
||||
NSData *stringData = [NSData dataWithBytes:input length:strlen(input)];
|
||||
[NSString stringEncodingForData:stringData encodingOptions:nil convertedString:&ret usedLossyConversion:nil];
|
||||
if(!ret) {
|
||||
ret = @"";
|
||||
}
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue