[英]swift removingPercentEncoding not work with a gb2312 string
服務器返回一個經過 urlencode 函數處理的 gb2312 字符串:
%D7%CF%BD%FB%B3%C7%C4%A7%D6%E4_%CE%DE%CF%DE%D0%A1%CB%B5%CD%F8_www.55x.cn.rar
如何將其解碼回 gb2312 字符串:
紫禁城魔咒_無限小說網_www.55x.cn.rar
在最近的 www 世界中,除 UTF-8 之外的其他編碼的百分比編碼不被認為是推薦的方式,因此您可能需要自己實現此類轉換。
它可能是這樣的:
extension String.Encoding {
static let gb_18030_2000 = String.Encoding(rawValue: CFStringConvertEncodingToNSStringEncoding(CFStringEncoding(CFStringEncodings.GB_18030_2000.rawValue)))
}
extension String {
func bytesByRemovingPercentEncoding(using encoding: String.Encoding) -> Data {
struct My {
static let regex = try! NSRegularExpression(pattern: "(%[0-9A-F]{2})|(.)", options: .caseInsensitive)
}
var bytes = Data()
let nsSelf = self as NSString
for match in My.regex.matches(in: self, range: NSRange(0..<self.utf16.count)) {
if match.rangeAt(1).location != NSNotFound {
let hexString = nsSelf.substring(with: NSMakeRange(match.rangeAt(1).location+1, 2))
bytes.append(UInt8(hexString, radix: 16)!)
} else {
let singleChar = nsSelf.substring(with: match.rangeAt(2))
bytes.append(singleChar.data(using: encoding) ?? "?".data(using: .ascii)!)
}
}
return bytes
}
func removingPercentEncoding(using encoding: String.Encoding) -> String? {
return String(data: bytesByRemovingPercentEncoding(using: encoding), encoding: encoding)
}
}
let origStr = "%D7%CF%BD%FB%B3%C7%C4%A7%D6%E4_%CE%DE%CF%DE%D0%A1%CB%B5%CD%F8_www.55x.cn.rar"
print(origStr.removingPercentEncoding(using: .gb_18030_2000)) //->Optional("紫禁城魔咒_無限小說網_www.55x.cn.rar")
NSString 確實在已棄用的函數中包含此功能。
https://developer.apple.com/documentation/foundation/nsstring/1407783-replacingpercentescapes
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.