2014年8月20日水曜日

開発環境

Head First C ―頭とからだで覚えるCの基本(David Griffiths (著)、Dawn Griffiths (著) 中田 秀基(監訳)(翻訳)、木下 哲也 (翻訳)、オライリージャパン)の3章(小さなツールの作成: 1つのことだけどうまくやる)、自分で考えてみよう(p.139)をSwiftで考えてみる。

自分で考えてみよう(p.139)

コード(Xcode)

string.swift

//
//  string.swift
//  string
//
//  Created by kamimura on 8/10/14.
//  Copyright (c) 2014 kamimura. All rights reserved.
//

import Foundation

extension String {
    var swapcaseString:String {
    var result: String = ""
        for ch in self {
            let s = String(ch)
            result += s.uppercaseString == s ? s.lowercaseString : s.uppercaseString
        }
        return result
    }
    var isUpper:Bool { return self.uppercaseString == self }
    var isLower:Bool { return self.lowercaseString == self }
    var length:Int { return (self as NSString).length }
    var strip:String { return self.stringByTrimmingCharactersInSet(NSCharacterSet.whitespaceAndNewlineCharacterSet())}
    var floatValue:Float { return (self as NSString).floatValue }
    var doubleValue:Double { return (self as NSString).doubleValue }
    func count(sub:String) -> Int {
        var result:Int = 0
        var s = self
        let index:String.Index = "a".endIndex
        while s != "" {
            if s.hasPrefix(sub) {
                result += 1
                s = s.substringFromIndex(sub.endIndex)
            } else {
                s = s.substringFromIndex(index)
            }
            
        }
        return result
    }
    func find(sub:String, start:Int = 0, end:Int? = nil) -> Int {
        var s = self as NSString
        var s_temp = s.substringFromIndex(start)
        let end_temp:Int = end ? end! : s.length
        for i in start..<end_temp {
            if (s_temp.hasPrefix(sub)) {
                return i
            }
            s_temp = s.substringFromIndex(i + 1) as NSString
        }
        return -1
    }
    func replace(old_str:String, new_str:String) -> String {
        return self.stringByReplacingOccurrencesOfString(old_str, withString: new_str)
    }
    func slice(start:Int=0, end:Int?=nil) -> String {
        let s = self as NSString
        var new_end:Int
        if (end) {
            new_end = end!
        } else {
            new_end = s.length
        }
        return  s.substringWithRange(NSRange(location: start, length:new_end - start))
    }
    func split(sep:String = "") -> [String] {
        var result:[String] = []
        if (sep == "") {
            for ch in self {
                result.append(String(ch))
            }
            return result
        }
        var s:String = self
        var len = sep.length
        var temp:String = ""
        while s != "" {
            if s.hasPrefix(sep) {
                if temp != "" {
                    result.append(temp)
                    temp = ""
                }
                s = s.slice(start: len)
            } else {
                temp += s.slice(start: 0, end: 1)
                s = s.slice(start: 1)
            }
        }
        if temp != "" {
            result.append(temp)
        }
        return result
    }
    var reversed:String { return "".join(self.split().reverse()) }
}

main.swift

//
//  main.swift
//  sample139
//
//  Created by kamimura on 8/20/14.
//  Copyright (c) 2014 kamimura. All rights reserved.
//

import Foundation

let in_fh = String.stringWithContentsOfFile("spooky.csv")
let file1 = "ufos.csv"
let file2 = "disappearances.csv"
let file3 = "others.csv"
// append modeで書き込む方法がなかなか見つからなかったからとりあえず文字列を作成してから書き込むことに(効率よくないかも…)
var result1 = ""
var result2 = ""
var result3 = ""

for line in in_fh!.split(sep: "\n") {
    let new_line = line.strip + "\n"
    if line.find("UFO") != -1 {
        result1 += new_line
    } else if line.find("Disappearance") != -1 {
        result2 += new_line
    } else {
        result3 += new_line
    }
}

result1.writeToFile(file1, atomically: true, encoding: NSUTF8StringEncoding, error: nil)
result2.writeToFile(file2, atomically: true, encoding: NSUTF8StringEncoding, error: nil)
result3.writeToFile(file3, atomically: true, encoding: NSUTF8StringEncoding, error: nil)

入出力結果(Console Output, Terminal)

$ cat spooky.csv 
30.685163,-68.137207,Type=Yeti
-100.0, 50,Type=Test1
28.304380,-74.575195,Type=UFO
29.132971,-71.136475,Type=Ship
28.343065,-62.753906,Type=Elvis
27.868217,-68.005371,Type=Goatsucker
30.496017,-73.333740,Type=Disappearance
26.224447,-71.477051,Type=UFO
29.401320,-66.027832,Type=Ship
37.879536,-69.477539,Type=Elvis
22.705256,-68.192139,Type=Elvis
27.166695,-87.484131,Type=Elvis
50,200,Type=Test2
$ ./sample139 
$ cat ufos.csv 
28.304380,-74.575195,Type=UFO
26.224447,-71.477051,Type=UFO
$ cat disappearances.csv 
30.496017,-73.333740,Type=Disappearance
$ cat others.csv 
30.685163,-68.137207,Type=Yeti
-100.0, 50,Type=Test1
29.132971,-71.136475,Type=Ship
28.343065,-62.753906,Type=Elvis
27.868217,-68.005371,Type=Goatsucker
29.401320,-66.027832,Type=Ship
37.879536,-69.477539,Type=Elvis
22.705256,-68.192139,Type=Elvis
27.166695,-87.484131,Type=Elvis
50,200,Type=Test2
$ cat spooky.csv 
30.685163,-68.137207,Type=Yeti
-100.0, 50,Type=Test1
28.304380,-74.575195,Type=UFO
29.132971,-71.136475,Type=Ship
28.343065,-62.753906,Type=Elvis
27.868217,-68.005371,Type=Goatsucker
30.496017,-73.333740,Type=Disappearance
26.224447,-71.477051,Type=UFO
29.401320,-66.027832,Type=Ship
37.879536,-69.477539,Type=Elvis
22.705256,-68.192139,Type=Elvis
27.166695,-87.484131,Type=Elvis
50,200,Type=Test2
$

0 コメント:

コメントを投稿