Skip to content

Commit fc02994

Browse files
committed
Merge pull request #2072 from liuteng/master
fallback solution for NSString decode error
2 parents 2fda43e + b75d0c3 commit fc02994

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

Frameworks/SPMySQLFramework/Source/SPMySQLResult Categories/Data Conversion.m

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -247,9 +247,9 @@ static inline SPMySQLResultFieldProcessor _processorForField(MYSQL_FIELD aField)
247247
{
248248

249249
// Fast case - if not using a preview length, or if the data length is shorter, return the requested data.
250-
if (previewLength == NSNotFound || dataLength <= previewLength) {
251-
return [[[NSString alloc] initWithBytes:dataBytes length:dataLength encoding:aStringEncoding] autorelease];
252-
}
250+
if (previewLength == NSNotFound || dataLength <= previewLength) {
251+
return [NSString stringForDataBytes:dataBytes length:dataLength encoding:aStringEncoding];
252+
}
253253

254254
NSUInteger i = 0, characterLength = 0, byteLength = previewLength;
255255
uint16_t continuationStart, continuationEnd;
@@ -394,7 +394,7 @@ static inline SPMySQLResultFieldProcessor _processorForField(MYSQL_FIELD aField)
394394

395395
// If returning the full string, use a fast path
396396
if (byteLength >= dataLength) {
397-
return [[[NSString alloc] initWithBytes:dataBytes length:dataLength encoding:aStringEncoding] autorelease];
397+
return [NSString stringForDataBytes:dataBytes length:dataLength encoding:aStringEncoding];
398398
}
399399

400400
// Get a string using the calculated details

Frameworks/SPMySQLFramework/Source/SPMySQLStringAdditions.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,6 @@
3333

3434
- (NSString *)mySQLBacktickQuotedString;
3535
- (NSString *)mySQLTickQuotedString;
36+
+ (NSString *)stringForDataBytes:(const void *)dataBytes length:(NSUInteger)dataLength encoding:(NSStringEncoding)aStringEncoding;
3637

3738
@end

Frameworks/SPMySQLFramework/Source/SPMySQLStringAdditions.m

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,18 @@ - (NSString *)mySQLTickQuotedString
5252
return [NSString stringWithFormat: @"'%@'", [self stringByReplacingOccurrencesOfString:@"'" withString:@"''"]];
5353
}
5454

55+
/**
56+
* Returns the string for the bytes according to the encoding, decode in ASCII if failed
57+
*/
58+
+ (NSString *) stringForDataBytes:(const void *)dataBytes length:(NSUInteger)dataLength encoding:(NSStringEncoding)aStringEncoding
59+
{
60+
NSString * string = [[[NSString alloc] initWithBytes:dataBytes length:dataLength encoding:aStringEncoding] autorelease];
61+
62+
if (string == nil) {
63+
return [[[NSString alloc] initWithBytes:dataBytes length:dataLength encoding:NSASCIIStringEncoding] autorelease];
64+
}
65+
66+
return string;
67+
}
68+
5569
@end

0 commit comments

Comments
 (0)