diff options
author | teng.liu <teng.liu@dianping.com> | 2015-02-16 17:39:28 +0800 |
---|---|---|
committer | teng.liu <teng.liu@dianping.com> | 2015-02-16 17:39:28 +0800 |
commit | b75d0c396376e0d82eabdfe971436ec1b97a61cf (patch) | |
tree | a8668032caf6dd75f4ad4b74c9282f4df09e23aa | |
parent | b00142a846ceda985516a0bdec3c1d6a9f47bdee (diff) | |
download | sequelpro-b75d0c396376e0d82eabdfe971436ec1b97a61cf.tar.gz sequelpro-b75d0c396376e0d82eabdfe971436ec1b97a61cf.tar.bz2 sequelpro-b75d0c396376e0d82eabdfe971436ec1b97a61cf.zip |
move the string method to SPMySQLStringAdditions
3 files changed, 17 insertions, 13 deletions
diff --git a/Frameworks/SPMySQLFramework/Source/SPMySQLResult Categories/Data Conversion.m b/Frameworks/SPMySQLFramework/Source/SPMySQLResult Categories/Data Conversion.m index 0ed8a51e..639ff0b9 100644 --- a/Frameworks/SPMySQLFramework/Source/SPMySQLResult Categories/Data Conversion.m +++ b/Frameworks/SPMySQLFramework/Source/SPMySQLResult Categories/Data Conversion.m @@ -239,17 +239,6 @@ static inline NSString * _bitStringWithBytes(const char *bytes, NSUInteger lengt return returnString; } -static inline NSString * _convertStringDataSafely(const void *dataBytes, NSUInteger dataLength, NSStringEncoding aStringEncoding) -{ - NSString * result = [[[NSString alloc] initWithBytes:dataBytes length:dataLength encoding:aStringEncoding] autorelease]; - - if (result == nil) { - return [[[NSString alloc] initWithBytes:dataBytes length:dataLength encoding:NSASCIIStringEncoding] autorelease]; - } - - return result; -} - /** * Converts stored string data - which may contain nul bytes - to a native * Objective-C string, using the current class encoding. @@ -259,7 +248,7 @@ static inline NSString * _convertStringData(const void *dataBytes, NSUInteger da // 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 _convertStringDataSafely(dataBytes, dataLength, aStringEncoding); + return [NSString stringForDataBytes:dataBytes length:dataLength encoding:aStringEncoding]; } NSUInteger i = 0, characterLength = 0, byteLength = previewLength; @@ -405,7 +394,7 @@ static inline NSString * _convertStringData(const void *dataBytes, NSUInteger da // If returning the full string, use a fast path if (byteLength >= dataLength) { - return _convertStringDataSafely(dataBytes, dataLength, aStringEncoding); + return [NSString stringForDataBytes:dataBytes length:dataLength encoding:aStringEncoding]; } // Get a string using the calculated details diff --git a/Frameworks/SPMySQLFramework/Source/SPMySQLStringAdditions.h b/Frameworks/SPMySQLFramework/Source/SPMySQLStringAdditions.h index bb29c02b..60832c53 100644 --- a/Frameworks/SPMySQLFramework/Source/SPMySQLStringAdditions.h +++ b/Frameworks/SPMySQLFramework/Source/SPMySQLStringAdditions.h @@ -33,5 +33,6 @@ - (NSString *)mySQLBacktickQuotedString; - (NSString *)mySQLTickQuotedString; ++ (NSString *)stringForDataBytes:(const void *)dataBytes length:(NSUInteger)dataLength encoding:(NSStringEncoding)aStringEncoding; @end diff --git a/Frameworks/SPMySQLFramework/Source/SPMySQLStringAdditions.m b/Frameworks/SPMySQLFramework/Source/SPMySQLStringAdditions.m index 39688ea8..f987c98b 100644 --- a/Frameworks/SPMySQLFramework/Source/SPMySQLStringAdditions.m +++ b/Frameworks/SPMySQLFramework/Source/SPMySQLStringAdditions.m @@ -52,4 +52,18 @@ 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 |