版本记录
版本号 | 时间 |
---|---|
V1.0 | 2018.03.20 |
前言
iOS圈内有几个人大家基本都知道,比如说王巍、唐巧,还有YYKit框架的作者现任职于滴滴的郭曜源 - ibireme等。这里有一篇唐巧对他的专访,还有他的 GitHub - Yaoyuan 和 博客,这里贴出来框架YYKit 框架。接下来几篇我们就一起来看一下这个框架。感兴趣的可以看上面写的几篇。
1. YYKit源码探究(一) —— 基本概览
2. YYKit源码探究(二) —— NSString分类之Hash(一)
回顾
上一篇我们分析了NSString
分类NSString+YYAdd
的Hash部分,这一篇我们就看一下Encode and decode
部分。
API接口
下面我们就看一下API接口。
/**
Returns an NSString for base64 encoded.
*/
- (nullable NSString *)base64EncodedString;
/**
Returns an NSString from base64 encoded string.
@param base64Encoding The encoded string.
*/
+ (nullable NSString *)stringWithBase64EncodedString:(NSString *)base64EncodedString;
/**
URL encode a string in utf-8.
@return the encoded string.
*/
- (NSString *)stringByURLEncode;
/**
URL decode a string in utf-8.
@return the decoded string.
*/
- (NSString *)stringByURLDecode;
/**
Escape common HTML to Entity.
Example: "a<b" will be escape to "a<b".
*/
- (NSString *)stringByEscapingHTML;
1. - (nullable NSString *)base64EncodedString;
这个方法的作用就是对给定的字符串进行base64编码。
示例调用
我们先看一下示例调用。
NSString *str = @"abcdefgh";
NSString *base64Str = [str base64EncodedString];
NSLog(@"base64Str = %@", base64Str);
NSLog(@"base64Str length = %ld", base64Str.length);
下面看一下输出结果
2018-03-16 17:51:49.252896+0800 JJWebImage[96399:6379065] base64Str = YWJjZGVmZ2g=
2018-03-16 17:51:49.253034+0800 JJWebImage[96399:6379065] base64Str length = 12
方法实现
下面看一下方法的实现原理。
- (NSString *)base64EncodedString {
return [[self dataUsingEncoding:NSUTF8StringEncoding] base64EncodedString];
}
然后是进入NSData的分类
- (NSString *)base64EncodedString {
NSUInteger length = self.length;
if (length == 0)
return @"";
NSUInteger out_length = ((length + 2) / 3) * 4;
uint8_t *output = malloc(((out_length + 2) / 3) * 4);
if (output == NULL)
return nil;
const char *input = self.bytes;
NSInteger i, value;
for (i = 0; i < length; i += 3) {
value = 0;
for (NSInteger j = i; j < i + 3; j++) {
value <<= 8;
if (j < length) {
value |= (0xFF & input[j]);
}
}
NSInteger index = (i / 3) * 4;
output[index + 0] = base64EncodingTable[(value >> 18) & 0x3F];
output[index + 1] = base64EncodingTable[(value >> 12) & 0x3F];
output[index + 2] = ((i + 1) < length)
? base64EncodingTable[(value >> 6) & 0x3F]
: '=';
output[index + 3] = ((i + 2) < length)
? base64EncodingTable[(value >> 0) & 0x3F]
: '=';
}
NSString *base64 = [[NSString alloc] initWithBytes:output
length:out_length
encoding:NSASCIIStringEncoding];
free(output);
return base64;
}
static const char base64EncodingTable[64]
= "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
2. + (nullable NSString *)stringWithBase64EncodedString:(NSString *)base64EncodedString;
该方法的作用就是对base64加密的字符串进行解密。
示例调用
下面我们就看一下示例调用。
NSString *base64Str = [NSString stringWithBase64EncodedString:@"YWJjZGVmZ2g="];
NSLog(@"base64Str = %@", base64Str);
NSLog(@"base64Str length = %ld", base64Str.length);
下面看一下输出结果
2018-03-16 18:51:54.918780+0800 JJWebImage[96911:6510428] base64Str = abcdefgh
2018-03-16 18:51:54.918904+0800 JJWebImage[96911:6510428] base64Str length = 8
方法实现
下面看一下方法的实现原理。
+ (NSString *)stringWithBase64EncodedString:(NSString *)base64EncodedString {
NSData *data = [NSData dataWithBase64EncodedString:base64EncodedString];
return [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
}
上面是调用NSData的类方法,将给定的编码过的字符串进行解密并转化为NSString类型。
+ (NSData *)dataWithBase64EncodedString:(NSString *)base64EncodedString {
NSInteger length = base64EncodedString.length;
const char *string = [base64EncodedString cStringUsingEncoding:NSASCIIStringEncoding];
if (string == NULL)
return nil;
while (length > 0 && string[length - 1] == '=')
length--;
NSInteger outputLength = length * 3 / 4;
NSMutableData *data = [NSMutableData dataWithLength:outputLength];
if (data == nil)
return nil;
if (length == 0)
return data;
uint8_t *output = data.mutableBytes;
NSInteger inputPoint = 0;
NSInteger outputPoint = 0;
while (inputPoint < length) {
char i0 = string[inputPoint++];
char i1 = string[inputPoint++];
char i2 = inputPoint < length ? string[inputPoint++] : 'A';
char i3 = inputPoint < length ? string[inputPoint++] : 'A';
output[outputPoint++] = (base64DecodingTable[i0] << 2)
| (base64DecodingTable[i1] >> 4);
if (outputPoint < outputLength) {
output[outputPoint++] = ((base64DecodingTable[i1] & 0xf) << 4)
| (base64DecodingTable[i2] >> 2);
}
if (outputPoint < outputLength) {
output[outputPoint++] = ((base64DecodingTable[i2] & 0x3) << 6)
| base64DecodingTable[i3];
}
}
return data;
}
同样用的是下面的解码表
static const short base64DecodingTable[256] = {
-2, -2, -2, -2, -2, -2, -2, -2, -2, -1, -1, -2, -1, -1, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-1, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, 62, -2, -2, -2, 63,
52, 53, 54, 55, 56, 57, 58, 59, 60, 61, -2, -2, -2, -2, -2, -2,
-2, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, -2, -2, -2, -2, -2,
-2, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2,
-2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2
};
3. - (NSString *)stringByURLEncode
该方法的作用就是对给定的字符串进行URL 的UTF - 8编码。
示例调用
NSString *str = @"http:\\baidu.com?my=我的";
NSString *urlEncodeStr = [str stringByURLEncode];
NSLog(@"urlEncodeStr = %@", urlEncodeStr);
NSLog(@"urlEncodeStr length = %ld", urlEncodeStr.length);
看一下输出结果
2018-03-16 19:03:51.935872+0800 JJWebImage[97043:6603499] urlEncodeStr = http%3A%5Cbaidu.com?my%3D%E6%88%91%E7%9A%84
2018-03-16 19:03:51.936090+0800 JJWebImage[97043:6603499] urlEncodeStr length = 43
方法实现
- (NSString *)stringByURLEncode {
if ([self respondsToSelector:@selector(stringByAddingPercentEncodingWithAllowedCharacters:)]) {
/**
AFNetworking/AFURLRequestSerialization.m
Returns a percent-escaped string following RFC 3986 for a query string key or value.
RFC 3986 states that the following characters are "reserved" characters.
- General Delimiters: ":", "#", "[", "]", "@", "?", "/"
- Sub-Delimiters: "!", "$", "&", "'", "(", ")", "*", "+", ",", ";", "="
In RFC 3986 - Section 3.4, it states that the "?" and "/" characters should not be escaped to allow
query strings to include a URL. Therefore, all "reserved" characters with the exception of "?" and "/"
should be percent-escaped in the query string.
- parameter string: The string to be percent-escaped.
- returns: The percent-escaped string.
*/
static NSString * const kAFCharactersGeneralDelimitersToEncode = @":#[]@"; // does not include "?" or "/" due to RFC 3986 - Section 3.4
static NSString * const kAFCharactersSubDelimitersToEncode = @"!$&'()*+,;=";
NSMutableCharacterSet * allowedCharacterSet = [[NSCharacterSet URLQueryAllowedCharacterSet] mutableCopy];
[allowedCharacterSet removeCharactersInString:[kAFCharactersGeneralDelimitersToEncode stringByAppendingString:kAFCharactersSubDelimitersToEncode]];
static NSUInteger const batchSize = 50;
NSUInteger index = 0;
NSMutableString *escaped = @"".mutableCopy;
while (index < self.length) {
NSUInteger length = MIN(self.length - index, batchSize);
NSRange range = NSMakeRange(index, length);
// To avoid breaking up character sequences such as 👴🏻👮🏽
range = [self rangeOfComposedCharacterSequencesForRange:range];
NSString *substring = [self substringWithRange:range];
NSString *encoded = [substring stringByAddingPercentEncodingWithAllowedCharacters:allowedCharacterSet];
[escaped appendString:encoded];
index += range.length;
}
return escaped;
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
CFStringEncoding cfEncoding = CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding);
NSString *encoded = (__bridge_transfer NSString *)
CFURLCreateStringByAddingPercentEscapes(
kCFAllocatorDefault,
(__bridge CFStringRef)self,
NULL,
CFSTR("!#$&'()*+,/:;=?@[]"),
cfEncoding);
return encoded;
#pragma clang diagnostic pop
}
}
4. - (NSString *)stringByURLDecode;
该方法的作用就是对给定的字符串进行UTF - 8解码。
示例调用
NSString *str = @"http%3A%5Cbaidu.com?my%3D%E6%88%91%E7%9A%84";
NSString *urlDecodeStr = [str stringByURLDecode];
NSLog(@"urlDecodeStr = %@", urlDecodeStr);
NSLog(@"urlDecodeStr length = %ld", urlDecodeStr.length);
看一下输出结果
2018-03-16 19:08:40.316664+0800 JJWebImage[97127:6649148] urlDecodeStr = http:\baidu.com?my=我的
2018-03-16 19:08:40.316785+0800 JJWebImage[97127:6649148] urlDecodeStr length = 21
方法实现
下面看一下方法的实现原理。
- (NSString *)stringByURLDecode {
if ([self respondsToSelector:@selector(stringByRemovingPercentEncoding)]) {
return [self stringByRemovingPercentEncoding];
} else {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
CFStringEncoding en = CFStringConvertNSStringEncodingToEncoding(NSUTF8StringEncoding);
NSString *decoded = [self stringByReplacingOccurrencesOfString:@"+"
withString:@" "];
decoded = (__bridge_transfer NSString *)
CFURLCreateStringByReplacingPercentEscapesUsingEncoding(
NULL,
(__bridge CFStringRef)decoded,
CFSTR(""),
en);
return decoded;
#pragma clang diagnostic pop
}
}
5. - (NSString *)stringByEscapingHTML;
将常见的HTML转义为实体
示例调用
NSString *str = @"a<b";
NSString *escapeStr = [str stringByEscapingHTML];
NSLog(@"escapeStr = %@", escapeStr);
NSLog(@"escapeStr length = %ld", escapeStr.length);
下面看输出结果
2018-03-16 19:14:34.942897+0800 JJWebImage[97214:6700533] urlDecodeStr = a<b
2018-03-16 19:14:34.943013+0800 JJWebImage[97214:6700533] urlDecodeStr length = 6
方法实现
下面看一下方法的实现
- (NSString *)stringByEscapingHTML {
NSUInteger len = self.length;
if (!len) return self;
unichar *buf = malloc(sizeof(unichar) * len);
if (!buf) return self;
[self getCharacters:buf range:NSMakeRange(0, len)];
NSMutableString *result = [NSMutableString string];
for (int i = 0; i < len; i++) {
unichar c = buf[i];
NSString *esc = nil;
switch (c) {
case 34: esc = @"""; break;
case 38: esc = @"&"; break;
case 39: esc = @"'"; break;
case 60: esc = @"<"; break;
case 62: esc = @">"; break;
default: break;
}
if (esc) {
[result appendString:esc];
} else {
CFStringAppendCharacters((CFMutableStringRef)result, &c, 1);
}
}
free(buf);
return result;
}
后记
本篇继续讲述NSString的分类,主要包括三个方面:base64的编码和解码、URL 的UTF - 8 编码以及将常见的HTML转义为实体。希望大家能喜欢,刚兴趣的可以给个赞和关注,谢谢~~~