Skip to content
Permalink
Browse files

Merge pull request #2072 from liuteng/master

fallback solution for NSString decode error
  • Loading branch information...
dmoagx committed Feb 17, 2015
2 parents 2fda43e + b75d0c3 commit fc02994e768eece82442bed8e6d21134d392b7a1
@@ -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;
@@ -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
@@ -33,5 +33,6 @@

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

@end
@@ -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.
You can’t perform that action at this time.