Created
August 21, 2017 12:38
-
-
Save eienf/833371ef563b944f8ac69152641cb11f 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
// | |
// Use with String+Hankana.swift | |
// half2full | |
// | |
// half2full textfile.utf8 // all Half -> Full | |
// half2full -r textfile.utf8 // all Full -> Half | |
// half2full -k textfile.utf8 // only Half Kana -> Full Kana | |
// | |
import Foundation | |
func fileHandle(path:String) -> NSFileHandle? { | |
let url = NSURL(fileURLWithPath: path) | |
return NSFileHandle(forReadingAtPath: url.path!) | |
} | |
func convertString(dataString:NSString, toFull:Bool, hanKana:Bool) -> String { | |
if hanKana { | |
let string = dataString as String | |
let str = string.stringWithoutHankana | |
return str | |
} | |
let str = dataString.mutableCopy() as! NSMutableString | |
CFStringTransform(str, nil, kCFStringTransformFullwidthHalfwidth, toFull) | |
return str as String | |
} | |
let args = Process.arguments | |
var inputHandle: NSFileHandle? | |
var toFull = true | |
var hanKana = false | |
for i in 1..<args.count { | |
let arg = args[i] | |
if arg == "-k" { | |
hanKana = true | |
continue | |
} | |
if arg == "-r" { | |
toFull = false | |
continue | |
} | |
inputHandle = fileHandle(arg) | |
if inputHandle != nil { | |
break | |
} | |
} | |
let standardInput = inputHandle ?? NSFileHandle.fileHandleWithStandardInput() | |
let standardOut = NSFileHandle.fileHandleWithStandardOutput() | |
while true { | |
let input = standardInput.availableData | |
if input.length == 0 { break } | |
let dataString = NSString(data:input, encoding:NSUTF8StringEncoding) | |
let str = convertString(dataString!, toFull: toFull, hanKana: hanKana) | |
if let out = str.dataUsingEncoding(NSUTF8StringEncoding) { | |
standardOut.writeData(out) | |
} | |
} | |
standardOut.writeData(NSString(string: "\n").dataUsingEncoding(NSUTF8StringEncoding)!) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment