|
| 1 | +// Copyright (c) 2022 and onwards The McBopomofo Authors. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person |
| 4 | +// obtaining a copy of this software and associated documentation |
| 5 | +// files (the "Software"), to deal in the Software without |
| 6 | +// restriction, including without limitation the rights to use, |
| 7 | +// copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +// copies of the Software, and to permit persons to whom the |
| 9 | +// Software is furnished to do so, subject to the following |
| 10 | +// conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be |
| 13 | +// included in all copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 17 | +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 18 | +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 19 | +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 20 | +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 | +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 22 | +// OTHER DEALINGS IN THE SOFTWARE. |
| 23 | + |
| 24 | +import Foundation |
| 25 | + |
| 26 | +@objc |
| 27 | +public enum RomanNumbersErrors: Int, Error, LocalizedError { |
| 28 | + case tooLarge |
| 29 | + case tooSmall |
| 30 | + case invalidInput |
| 31 | + |
| 32 | + public var errorDescription: String? { |
| 33 | + switch self { |
| 34 | + case .tooLarge: |
| 35 | + "Cannot be larger than 3999" |
| 36 | + case .tooSmall: |
| 37 | + "Cannot be less than 0" |
| 38 | + case .invalidInput: |
| 39 | + "Input is not a valid integer" |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +@objc public enum RomanNumbersStyle: Int { |
| 45 | + case alphabets |
| 46 | + case fullWidthUpper |
| 47 | + case fullWidthLower |
| 48 | +} |
| 49 | + |
| 50 | +struct DigitsMap { |
| 51 | + let digits: [String] |
| 52 | + let tens: [String] |
| 53 | + let hundreds: [String] |
| 54 | + let thousands: [String] |
| 55 | +} |
| 56 | + |
| 57 | +extension RomanNumbersStyle { |
| 58 | + var digitsMap: DigitsMap { |
| 59 | + switch self { |
| 60 | + case .alphabets: |
| 61 | + DigitsMap( |
| 62 | + digits: ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"], |
| 63 | + tens: ["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"], |
| 64 | + hundreds: ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"], |
| 65 | + thousands: ["", "M", "MM", "MMM"]) |
| 66 | + case .fullWidthUpper: |
| 67 | + DigitsMap( |
| 68 | + digits: ["", "Ⅰ", "Ⅱ", "Ⅲ", "Ⅳ", "Ⅴ", "Ⅵ", "Ⅶ", "Ⅷ", "Ⅸ"], |
| 69 | + tens: ["", "Ⅹ", "ⅩⅩ", "ⅩⅩⅩ", "ⅩⅬ", "Ⅼ", "ⅬⅩ", "ⅬⅩⅩ", "ⅬⅩⅩⅩ", "ⅩⅭ"], |
| 70 | + hundreds: ["", "Ⅽ", "ⅭⅭ", "ⅭⅭⅭ", "ⅭⅮ", "Ⅾ", "ⅮⅭ", "ⅮⅭⅭ", "ⅮⅭⅭⅭ", "ⅭⅯ"], |
| 71 | + thousands: ["", "Ⅿ", "ⅯⅯ", "ⅯⅯⅯ"]) |
| 72 | + case .fullWidthLower: |
| 73 | + DigitsMap( |
| 74 | + digits: ["", "ⅰ", "ⅱ", "ⅲ", "ⅳ", "ⅴ", "ⅵ", "ⅶ", "ⅷ", "ⅸ"], |
| 75 | + tens: ["", "ⅹ", "ⅹⅹ", "ⅹⅹⅹ", "ⅹⅼ", "ⅼ", "ⅼⅹ", "ⅼⅹⅹ", "ⅼⅹⅹⅹ", "ⅹⅽ"], |
| 76 | + hundreds: ["", "ⅽ", "ⅽⅽ", "ⅽⅽⅽ", "ⅽⅾ", "ⅾ", "ⅾⅽ", "ⅾⅽⅽ", "ⅾⅽⅽⅽ", "ⅽⅿ"], |
| 77 | + thousands: ["", "ⅿ", "ⅿⅿ", "ⅿⅿⅿ"]) |
| 78 | + } |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +@objc |
| 83 | +public class RomanNumbers: NSObject { |
| 84 | + @objc(convertWithInt:style:error:) |
| 85 | + public static func convert(input: Int, style: RomanNumbersStyle = .alphabets) throws -> String { |
| 86 | + if input > 3999 { |
| 87 | + throw RomanNumbersErrors.tooLarge |
| 88 | + } |
| 89 | + if input < 0 { |
| 90 | + throw RomanNumbersErrors.tooSmall |
| 91 | + } |
| 92 | + |
| 93 | + if style == .fullWidthUpper { |
| 94 | + switch input { |
| 95 | + case 11: |
| 96 | + return "Ⅺ" |
| 97 | + case 12: |
| 98 | + return "Ⅻ" |
| 99 | + default: |
| 100 | + break |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + if style == .fullWidthLower { |
| 105 | + switch input { |
| 106 | + case 11: |
| 107 | + return "ⅺ" |
| 108 | + case 12: |
| 109 | + return "ⅻ" |
| 110 | + default: |
| 111 | + break |
| 112 | + } |
| 113 | + |
| 114 | + } |
| 115 | + |
| 116 | + let thou = input / 1000 |
| 117 | + let hund = (input % 1000) / 100 |
| 118 | + let ten = (input % 100) / 10 |
| 119 | + let digit = input % 10 |
| 120 | + |
| 121 | + let map = style.digitsMap |
| 122 | + |
| 123 | + let result = map.thousands[thou] + map.hundreds[hund] + map.tens[ten] + map.digits[digit] |
| 124 | + return result |
| 125 | + } |
| 126 | + |
| 127 | + @objc(convertWithString:style:error:) |
| 128 | + public static func convert(string: String, style: RomanNumbersStyle = .alphabets) throws |
| 129 | + -> String |
| 130 | + { |
| 131 | + guard let number = Int(string) else { |
| 132 | + throw RomanNumbersErrors.invalidInput |
| 133 | + } |
| 134 | + return try convert(input: number, style: style) |
| 135 | + } |
| 136 | +} |
0 commit comments