Last active
June 18, 2016 00:43
-
-
Save Code-Hex/dd22fa33ee33fcf5c9a88219250f31ec to your computer and use it in GitHub Desktop.
SemiEncode, SemiDecode
This file contains hidden or 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
| import Foundation | |
| extension String { | |
| var binary: String { | |
| let chars = self.characters.map { String($0).unicodeScalars.first!.value } | |
| return chars.map({ String($0, radix: 2).pad(24) }).joinWithSeparator("") | |
| } | |
| func bintostr() -> String { | |
| let binaries = self.match("[01]{1,24}") | |
| var orig = "" | |
| for b in binaries { | |
| orig += String(UnicodeScalar(Int(strtoul(b, nil, 2)))) | |
| } | |
| return orig | |
| } | |
| func pad(toSize: Int) -> String { | |
| var padded = self | |
| for _ in 0..<toSize - self.characters.count { | |
| padded = "0" + padded | |
| } | |
| return padded | |
| } | |
| func match(pattern: String) -> [String] { | |
| let regex = try! NSRegularExpression(pattern: pattern, options: []) | |
| let range = NSMakeRange(0, self.characters.count) | |
| let matches = regex.matchesInString(self, options: [], range: range) | |
| var matchedTokens = [String]() | |
| for result in matches { | |
| for r in [result] { | |
| if r.range.location <= self.characters.count && r.range.length > 0 { | |
| matchedTokens.append((self as NSString).substringWithRange(r.range)) | |
| } | |
| break | |
| } | |
| } | |
| return matchedTokens | |
| } | |
| func replace(pattern: String, _ withString: String) -> String { | |
| let regex = try! NSRegularExpression(pattern: pattern, options: []) | |
| let range = NSMakeRange(0, self.characters.count) | |
| return regex.stringByReplacingMatchesInString(self, options: [], range: range, withTemplate: withString) | |
| } | |
| } | |
| // SemiEncode, Semidecode | |
| extension String { | |
| func SemiEncode() -> String { | |
| return self.binary.replace("0", "ミン").replace("1", "ミーン").replace("(ミーンミンミン)", "$0…") | |
| } | |
| func SemiDecode() -> String { | |
| return self.replace("…", "").replace("ミン", "0").replace("ミーン", "1").bintostr() | |
| } | |
| } | |
| let str = "お前のカーチャン🍣ィィィィィ〜www" | |
| let crows = str.SemiEncode() | |
| crows.SemiDecode() |
Code-Hex
commented
Jun 18, 2016
Author



Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment