Last active
May 3, 2019 07:16
-
-
Save ElonPark/9dae8cc9234990871bf3b919546dc169 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func euckrEncoding(_ query: String) -> String { //EUC-KR 인코딩 | |
let rawEncoding = CFStringConvertEncodingToNSStringEncoding(CFStringEncoding(CFStringEncodings.EUC_KR.rawValue)) | |
let encoding = String.Encoding(rawValue: rawEncoding) | |
let eucKRStringData = query.data(using: encoding) ?? Data() | |
let outputQuery = eucKRStringData.map {byte->String in | |
if byte >= UInt8(ascii: "A") && byte <= UInt8(ascii: "Z") | |
|| byte >= UInt8(ascii: "a") && byte <= UInt8(ascii: "z") | |
|| byte >= UInt8(ascii: "0") && byte <= UInt8(ascii: "9") | |
|| byte == UInt8(ascii: "_") || byte == UInt8(ascii: ".") || byte == UInt8(ascii: "-") | |
{ | |
return String(Character(UnicodeScalar(UInt32(byte))!)) | |
} else if byte == UInt8(ascii: " ") { | |
return "+" | |
} else { | |
return String(format: "%%%02X", byte) | |
} | |
}.joined() | |
return outputQuery | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment