swift removingPercentEncoding not work with a gb2312 string

岁酱吖の 提交于 2019-12-22 01:08:02

问题


The server returns a gb2312 string that has been processed by the urlencode function:

%D7%CF%BD%FB%B3%C7%C4%A7%D6%E4_%CE%DE%CF%DE%D0%A1%CB%B5%CD%F8_www.55x.cn.rar

How to decode it back to gb2312 string:

紫禁城魔咒_无限小说网_www.55x.cn.rar


回答1:


Percent encoding on other encodings than UTF-8 is not considered to be a recommended way in recent www world, so you may need to implement such conversion by yourself.

It may be something like this:

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")


来源:https://stackoverflow.com/questions/41477013/swift-removingpercentencoding-not-work-with-a-gb2312-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!