gpt4 book ai didi

swift - 允许的字符被百分比编码

转载 作者:行者123 更新时间:2023-12-03 20:46:21 24 4
gpt4 key购买 nike

字符串方法的文档 addingPercentEncoding(withAllowedCharacters:) :

Returns a new string made from the receiver by replacing all characters not in the specified set with percent-encoded characters.


预定义集 CharacterSet.alphanumerics说:

Returns a character set containing the characters in Unicode General Categories L*, M*, and N*.

L (信件)类别包含5个子类别: Ll , Lm , Lt , Lu , Lo .所以我假设 L*表示 L 的所有子类别。
我会选择看 Ll子类别( https://www.compart.com/en/unicode/category/Ll#UNC_SCRIPTS ),然后选择字符 "æ" (U+00E6)。
然后我可以看到字母数字字符集确实包含这个字符。但是当我将百分比编码添加到包含此字符的字符串时,它会得到百分比编码。
"\u{E6}" // "æ"
CharacterSet.alphanumerics.contains("\u{E6}") // true
"æ".addingPercentEncoding(withAllowedCharacters: .alphanumerics) // "%C3%A6" 🤨

// Let's try with "a"
"\u{61}" // "a"
CharacterSet.alphanumerics.contains("\u{61}") // true
"a".addingPercentEncoding(withAllowedCharacters: .alphanumerics) // "a"

为什么会发生这种情况?它在我传入的允许字符集中,所以不应该被替换,对吧?
我觉得这与 "a" 的事实有关。 (U+0061) 也是 0x61UTF-8但是 "æ" (U+00E6) 是 [0xC3, 0xA6] ;不是 0xE6 .或者它占用了超过 1 个字节?
String(data: Data([0x61]), encoding: .utf8)! // "a"
String(data: Data([0xC3, 0xA6]), encoding: .utf8)! // "æ"
String(data: Data([0xE6]), encoding: .utf8)! // crashes 🌋
更新
是不是因为百分比编码算法把字符串转换成 Data并一次通过 1 个字节?所以它会看 0xC3这不是一个允许的字符,所以它被编码成百分比。然后它会看 0xA6这也不是允许的字符,因此它也被编码为百分比。那么技术上允许的字符必须是单个字节?

最佳答案

真正允许的字符必须在允许的字符集中,并且是 ASCII 字符。感谢@alobaili 指出这一点。
如果你好奇,预定义集 CharacterSet.alphanumerics包含 129172总共字符,但只有 62当这个集合被传递到一个字符串的 addingPercentEncoding(allowedSet:) 时,才真正被允许方法。
检查特定 CharacterSet 中所有真正允许的字符的快速方法可以这样做:

func inspect(charSet: CharacterSet) {
var characters: [String] = []
for char: UInt8 in 0..<128 { // ASCII range
let u = UnicodeScalar(char)
if charSet.contains(u) {
characters.append(String(u))
}
}
print("Characters:", characters.count)
print(characters)
}

inspect(charSet: .alphanumerics) // [a-z, A-Z, 0-9]
这很方便,因为您不能简单地遍历 CharacterSet .了解那些允许的元素是什么会很有用。例如,预定义的 CharacterSet.urlQueryAllowed只说:

Returns the character set for characters allowed in a query URLcomponent.


我们可以知道那些允许的字符是什么:
inspect(charSet: .urlQueryAllowed)

// Characters: 81
// ["!", "$", "&", "\'", "(", ")", "*", "+", ",", "-", ".", "/", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", ":", ";", "=", "?", "@", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z", "_", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "~"]
只是为了好玩
还有另一种(长而可靠的)方法,它查看集合中的所有字符(不仅仅是 ASCII 字符),并将字符本身的字符串与添加百分比编码后的字符串进行比较允许设置。当这两个相等时,您就知道它确实是一个允许的字符。代码改编自 this有用的文章。
func inspect(charSet: CharacterSet) {
var characters: [String] = []
var allowed: [String] = []
var asciiCount = 0
for plane: UInt8 in 0..<17 {
if charSet.hasMember(inPlane: plane) {
let planeStart = UInt32(plane) << 16
let nextPlaneStart = (UInt32(plane) + 1) << 16
for char: UTF32Char in planeStart..<nextPlaneStart {
if let u = UnicodeScalar(char), charSet.contains(u) {
let s = String(u)
characters.append(s)

if s.addingPercentEncoding(withAllowedCharacters: CharacterSet([u])) == s {
allowed.append(s)
}

if u.isASCII {
asciiCount += 1
}
}
}
}
}
print("Characters:", characters.count)
print("Allowed:", allowed.count)
print("ASCII:", asciiCount)
}

inspect(charSet: .alphanumerics)

// Characters: 129172
// Allowed: 62
// ASCII: 62

关于swift - 允许的字符被百分比编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65373772/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com