简体   繁体   English

与CFURLCreateStringByAddingPercentEscapes编码使用的大小写混淆

[英]Confusion with case used by CFURLCreateStringByAddingPercentEscapes encoding

I want URL encoding to be done. 我希望完成URL编码。 My input string is " ChBdgzQ3qUpNRBEHB+bOXQNjRTQ= " 我的输入字符串是“ ChBdgzQ3qUpNRBEHB + bOXQNjRTQ =

I get an output as " ChBdgzQ3qUpNRBEHB%2BbOXQNjRTQ%3D " which is totally correct except the case which gets encoded. 我得到的输出为“ ChBdgzQ3qUpNRBEHB%2BbOXQNjRTQ%3D ”,除了被编码的情况外,它是完全正确的。

Ideally, it should have been " ChBdgzQ3qUpNRBEHB%2bbOXQNjRTQ%3d " instead of the output I get. 理想情况下,应该是“ ChBdgzQ3qUpNRBEHB%2bbOXQNjRTQ%3d ”,而不是我得到的输出。 ie I should have got %2b and %3d instead of %2B and %3D . 即我应该有%2b和%3d而不是%2B和%3D

Could this be done? 能做到吗?

The code I used is as below : 我使用的代码如下:

NSString* inputStr = @"ChBdgzQ3qUpNRBEHB+bOXQNjRTQ=";
NSString* outputStr = (NSString *)CFURLCreateStringByAddingPercentEscapes(NULL,
                                                                          (CFStringRef)inputStr,
                                                                          NULL,
                                                                          (CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ",
                                                                          CFStringConvertNSStringEncodingToEncoding(encoding));

Another perhaps more elegant but slower way would be to loop over your string, converting each character in the string one by one (so you would get the length of your string, then get a substring from it from location 0 to length-1, with one character each time, then translate just that substring. If the returned string has a length > 1, then CFURLCreateStringByAddingPercentEscapes encoded the character, and you can safely turn the case into lower case. 另一种可能更优雅但较慢的方法是遍历字符串,将字符串中的每个字符一个接一个地转换(这样,您将获得字符串的长度,然后从位置0到length-1的位置获得一个子字符串,用每次只输入一个字符,然后仅翻译该子字符串。如果返回的字符串的长度> 1,则CFURLCreateStringByAddingPercentEscapes对该字符进行了编码,因此可以安全地将大小写变为小写。

In all cases you append the returned (and possibly modified) string to a mutable string, and when done you have exactly what you want for any possible string. 在所有情况下,您都将返回的(可能已修改的)字符串附加到可变的字符串上,完成后,您将完全拥有所需的任何可能的字符串。 Even though this would appear to be a real processor hog, the reality is you would probably never notice the extra consumed cycles. 即使这似乎是真正的处理器,但事实是您可能永远不会注意到额外的消耗周期。

Likewise, a second approach would be to just convert your whole string first, then copy it byte by byte to a mutable string, and if you find a "%", then turn the next two characters into lower case. 同样,第二种方法是先转换整个字符串,然后将其逐字节复制到可变字符串,如果找到“%”,则将接下来的两个字符转换为小写。 Just a slightly different way to slice the problem. 解决问题的方法略有不同。

You can use a regular expression to perform the post operation: 您可以使用正则表达式执行发布操作:

NSMutableString *finalStr = outputStr.mutableCopy;
NSRegularExpression *re = [[NSRegularExpression alloc] initWithPattern:@"(?<=%)[0-9A-F]{2}" options:0 error:nil];

for (NSTextCheckingResult *match in [re matchesInString:escaped options:0 range:NSMakeRange(0, escaped.length)]) {
    [finalStr replaceCharactersInRange:match.range withString:[[escaped substringWithRange:match.range] lowercaseString]];
}

The code uses this regular expression: 该代码使用以下正则表达式:

(<?=%)[0-9A-F]{2}

It matches two hexadecimal characters, only if preceded by a percent sign. 仅在以百分号开头的情况下,它匹配两个十六进制字符。 Each match is then iterated and replaced within a mutable string. 然后,将每个匹配项迭代并替换为可变字符串。 We don't have to worry about offset changes because the replacement string is always the same length. 我们不必担心偏移量的变化,因为替换字符串的长度始终相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM