Last active
April 4, 2016 06:23
-
-
Save el-hoshino/40e66af5fda8e5442d791931ae08f3c3 to your computer and use it in GitHub Desktop.
ソースコードで想いを届ける(Swift コマンドライン編) ref: http://qiita.com/lovee/items/a225e27610a663035cd8
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
% swift iLoveYou.swift 春彦母 | |
私は春彦母のことが好きです |
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
$ swift iLoveYou.swift Haruhiko Mom | |
私はHaruhiko Momのことが好きです |
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
$ swift iLoveYou.swift 春彦母 | |
私は春彦母のことが好きです | |
$ swift iLoveYou.swift Haruhiko Mom | |
私は Haruhiko Mom のことが好きです |
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 NSCharacterSet { | |
class func alphabetAndNumberCharacterSet() -> NSCharacterSet { | |
let set = NSCharacterSet(charactersInString: "ABCDEFGHIJKLMNOPQRSTUVWXYZÅØÁÉÍÓÚÂÊÎÔÛÀÈÌÒÙÄËÏÖÜÃÕabcdefghijklmnopqrstuvwxyzåøáéíóúâêîôûàèìòùäëïöüãõß0123456789") | |
return set | |
} | |
} | |
extension String { | |
var startsWithAlphanumericCharacter: Bool { | |
guard self.characters.count > 0 else { | |
return false | |
} | |
let firstCharacter = self.substringToIndex(self.startIndex.successor()) | |
if firstCharacter.rangeOfCharacterFromSet(.alphabetAndNumberCharacterSet()) == nil { | |
return false | |
} else { | |
return true | |
} | |
} | |
var endsWithAlphanumericCharacter: Bool { | |
guard self.characters.count > 0 else { | |
return false | |
} | |
let firstCharacter = self.substringFromIndex(self.endIndex.predecessor()) | |
if firstCharacter.rangeOfCharacterFromSet(.alphabetAndNumberCharacterSet()) == nil { | |
return false | |
} else { | |
return true | |
} | |
} | |
} | |
func run() { | |
guard Process.arguments.count > 1 else { | |
print("ファイル名の後に名前を入力してください。例:$ swift example.swift HER NAME") | |
return | |
} | |
let name = Process.arguments.dropFirst().reduce("", combine: {$0.isEmpty ? $1 : $0 + " " + $1}) | |
let prefix = name.startsWithAlphanumericCharacter ? " " : "" | |
let suffix = name.endsWithAlphanumericCharacter ? " " : "" | |
let output = prefix + name + suffix | |
print("私は\(output)のことが好きです") | |
} | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment