Skip to content

Commit

Permalink
Merge pull request #2072 from liuteng/master
Browse files Browse the repository at this point in the history
fallback solution for NSString decode error
  • Loading branch information
dmoagx committed Feb 17, 2015
2 parents 2fda43e + b75d0c3 commit fc02994
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -247,9 +247,9 @@ static inline SPMySQLResultFieldProcessor _processorForField(MYSQL_FIELD aField)
{

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

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

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

// Get a string using the calculated details
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,6 @@

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

@end
14 changes: 14 additions & 0 deletions Frameworks/SPMySQLFramework/Source/SPMySQLStringAdditions.m
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,18 @@ - (NSString *)mySQLTickQuotedString
return [NSString stringWithFormat: @"'%@'", [self stringByReplacingOccurrencesOfString:@"'" withString:@"''"]];
}

/**
* Returns the string for the bytes according to the encoding, decode in ASCII if failed
*/
+ (NSString *) stringForDataBytes:(const void *)dataBytes length:(NSUInteger)dataLength encoding:(NSStringEncoding)aStringEncoding
{
NSString * string = [[[NSString alloc] initWithBytes:dataBytes length:dataLength encoding:aStringEncoding] autorelease];

if (string == nil) {
return [[[NSString alloc] initWithBytes:dataBytes length:dataLength encoding:NSASCIIStringEncoding] autorelease];
}

return string;
}

@end

0 comments on commit fc02994

Please sign in to comment.