0001    //
0002    //  Roman.swift
0003    //  Roman
0004    //
0005    //  The MIT License (MIT)
0006    //
0007    //  Copyright (c) 2016 Nikolai Vazquez
0008    //
0009    //  Permission is hereby granted, free of charge, to any person obtaining a copy
0010    //  of this software and associated documentation files (the "Software"), to deal
0011    //  in the Software without restriction, including without limitation the rights
0012    //  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
0013    //  copies of the Software, and to permit persons to whom the Software is
0014    //  furnished to do so, subject to the following conditions:
0015    //
0016    //  The above copyright notice and this permission notice shall be included in
0017    //  all copies or substantial portions of the Software.
0018    //
0019    //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
0020    //  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
0021    //  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
0022    //  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
0023    //  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
0024    //  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
0025    //  THE SOFTWARE.
0026    //
0027    
0028    private let _pairs
Roman.swift:87
        for (numeral, value) in _pairs {
: [(String, IntMax)] = _orderedPairs().filter({ $0.0 != "M" }) 0029 0030 private func _numeralPairs
Roman.swift:49
    return _numeralPairs().sort({ $0.1 > $1.1 })
Roman.swift:135
        let pairs: [String: Self] = _numeralPairs()
<I: IntegerType>() -> [String: I] { 0031 return [ 0032 "M": 1000, 0033 "CM": 900, 0034 "D": 500, 0035 "CD": 400, 0036 "C": 100, 0037 "XC": 90, 0038 "L": 50, 0039 "XL": 40, 0040 "X": 10, 0041 "IX": 9, 0042 "V": 5, 0043 "IV": 4, 0044 "I": 1 0045 ] 0046 } 0047 0048 private func _orderedPairs
Roman.swift:28
private let _pairs: [(String, IntMax)] = _orderedPairs().filter({ $0.0 != "M" })
<I: IntegerType>() -> [(String, I)] { 0049 return _numeralPairs().sort({ $0.1 > $1.1 }) 0050 } 0051 0052 private func _with
Roman.swift:159
                return _with(value, createFrom(rest), combine: +)
<T>(first: T?, _ second: T?, combine: (T, T) -> T?) -> T? { 0053 guard let first = first, second = second else { 0054 return nil 0055 } 0056 return combine(first, second) 0057 } 0058 0059 private func _repeat
Roman.swift:78
    return string + _repeat(string, count: count - 1)
Roman.swift:84
        let values = _repeat("M", count: Int(int / 1000))
(string: String, count: Int) -> String { 0060 func repeatString(string: String, count: Int) -> String { 0061 return Repeat(count: count, repeatedValue: string) 0062 .reduce("", combine: +) 0063 } 0064 let values = [ 0065 10000, 7000, 5000, 3000, 2000, 0066 1000, 700, 500, 300, 200, 0067 100, 70, 50, 30, 20, 0068 10, 7, 5, 3, 2 0069 ] 0070 for value in values { 0071 if count % value == 0 { 0072 return repeatString( 0073 repeatString(string, count: value), 0074 count: count / value 0075 ) 0076 } 0077 } 0078 return string + _repeat(string, count: count - 1) 0079 } 0080 0081 private func _createFrom
Roman.swift:85
        return values + _createFrom(integer % 1000)
Roman.swift:89
                return numeral + _createFrom(int - value)
Roman.swift:110
        self = _createFrom(integer)
<I: IntegerType>(integer: I) -> String { 0082 let int = integer.toIntMax() 0083 if int >= 1000 { 0084 let values = _repeat("M", count: Int(int / 1000)) 0085 return values + _createFrom(integer % 1000) 0086 } else { 0087 for (numeral, value) in _pairs { 0088 if int >= value { 0089 return numeral + _createFrom(int - value) 0090 } 0091 } 0092 return "" 0093 } 0094 } 0095 0096 extension String { 0097 0098 private subscript
Roman.swift:146
                let head = numeral[0 ..< index]
Roman.swift:150
                let rest = numeral[index ..< count]
Roman.swift:153
                        ? (pairs[rest[0 ..< 2]] ?? pairs[rest[0 ..< 1]])
Roman.swift:153
                        ? (pairs[rest[0 ..< 2]] ?? pairs[rest[0 ..< 1]])
Roman.swift:154
                        :  pairs[rest[0 ..< 1]]
(range: Range<Int>) -> String { 0099 return self[startIndex.advancedBy(range.startIndex) 0100 ..< startIndex.advancedBy(range.endIndex)] 0101 } 0102 0103 /// Create a Roman numeral string from an integer in its most compact form. 0104 /// 0105 /// Returns `nil` if the integer is `<= 0`. 0106 public init
Roman.swift:117
        guard let value = integer.flatMap({ String(roman: $0) }) else {
?<I: IntegerType>(roman integer: I) { 0107 guard integer > 0 else { 0108 return nil 0109 } 0110 self = _createFrom(integer) 0111 } 0112 0113 /// Create a Roman numeral string from an integer in its most compact form. 0114 /// 0115 /// Returns `nil` if the integer is `<= 0`. 0116 public init?<I: IntegerType>(roman integer: I?) { 0117 guard let value = integer.flatMap({ String(roman: $0) }) else { 0118 return nil 0119 } 0120 self = value 0121 } 0122 0123 } 0124 0125 extension IntegerType { 0126 0127 /// Create an integer from a valid Roman numeral string. 0128 public init
Roman.swift:172
        guard let value = numeral.flatMap({ Self(roman: $0) }) else {
Roman.swift:184
        guard let value = Int(roman: numeral).flatMap({ Self($0) }) else {
?(roman numeral: String) { 0129 0130 guard !numeral.isEmpty else { 0131 return nil 0132 } 0133 0134 typealias Integer = Self 0135 let pairs: [String: Self] = _numeralPairs() 0136 0137 func createFrom(numeral: String) -> Integer? { 0138 guard !numeral.isEmpty else { 0139 return 0 0140 } 0141 for index in [2, 1] { 0142 let count = numeral.characters.count 0143 guard index <= count else { 0144 continue 0145 } 0146 let head = numeral[0 ..< index] 0147 guard let value = pairs[head] else { 0148 continue 0149 } 0150 let rest = numeral[index ..< count] 0151 if !rest.isEmpty { 0152 let partValue = rest.characters.count >= 2 0153 ? (pairs[rest[0 ..< 2]] ?? pairs[rest[0 ..< 1]]) 0154 : pairs[rest[0 ..< 1]] 0155 guard partValue <= value else { 0156 return nil 0157 } 0158 } 0159 return _with(value, createFrom(rest), combine: +) 0160 } 0161 return nil 0162 } 0163 0164 guard let value = createFrom(numeral.uppercaseString) else { 0165 return nil 0166 } 0167 self = value 0168 } 0169 0170 /// Create an integer from a valid Roman numeral string. 0171 public init?(roman numeral: String?) { 0172 guard let value = numeral.flatMap({ Self(roman: $0) }) else { 0173 return nil 0174 } 0175 self = value 0176 } 0177 0178 } 0179 0180 extension FloatingPointType { 0181 0182 /// Create an instance from a valid Roman numeral string. 0183 public init
Roman.swift:192
        guard let value = numeral.flatMap({ Self(roman: $0) }) else {
?(roman numeral: String) { 0184 guard let value = Int(roman: numeral).flatMap({ Self($0) }) else { 0185 return nil 0186 } 0187 self = value 0188 } 0189 0190 /// Create an instance from a valid Roman numeral string. 0191 public init?(roman numeral: String?) { 0192 guard let value = numeral.flatMap({ Self(roman: $0) }) else { 0193 return nil 0194 } 0195 self = value 0196 } 0197 0198 } 0199