 //  Created by Norio Nomura on 12/21/16.
 //  Copyright (c) 2016 Yams. All rights reserved.
 //
+
+// swiftlint:disable file_length
 
 import Foundation
 

extension Int: ScalarConstructible {
    public static func construct(from node: Node) -> Int? {
        assert(node.isScalar) // swiftlint:disable:next force_unwrapping
        var scalar = node.scalar!.string
        scalar = scalar.replacingOccurrences(of: "_", with: "")
        if scalar == "0" {
            return 0
        }
        if scalar.hasPrefix("0x") {
#if swift(>=4.0)
            let hexadecimal = scalar[scalar.index(scalar.startIndex, offsetBy: 2)...]
#else
            let hexadecimal = scalar.substring(from: scalar.index(scalar.startIndex, offsetBy: 2))
#endif
            return Int(hexadecimal, radix: 16)
        }
        if scalar.hasPrefix("0b") {
#if swift(>=4.0)
            let octal = scalar[scalar.index(scalar.startIndex, offsetBy: 2)...]
#else
            let octal = scalar.substring(from: scalar.index(scalar.startIndex, offsetBy: 2))
#endif
            return Int(octal, radix: 2)
        }
        if scalar.hasPrefix("0o") {
#if swift(>=4.0)
            let octal = scalar[scalar.index(scalar.startIndex, offsetBy: 2)...]
#else
            let octal = scalar.substring(from: scalar.index(scalar.startIndex, offsetBy: 2))
#endif
            return Int(octal, radix: 8)
        }
        if scalar.hasPrefix("0") {
#if swift(>=4.0)
            let octal = scalar[scalar.index(after: scalar.startIndex)...]
#else
            let octal = scalar.substring(from: scalar.index(after: scalar.startIndex))
#endif
            return Int(octal, radix: 8)
        }
        if scalar.contains(":") {
            return Int(sexagesimal: scalar)
        }
        return Int(scalar)
    }
}


    fileprivate init(sexagesimal value: String) {
        self = value.sexagesimal()
    }
#if swift(>=4.0)
    fileprivate init(sexagesimal value: Substring) {
        self = value.sexagesimal()
    }
#endif
}

extension Double: SexagesimalConvertible {}
extension Int: SexagesimalConvertible {
#if swift(>=4.0)
    fileprivate init?(_ value: Substring) {
        self.init(value, radix: 10)
    }
#else
    fileprivate init?(_ value: String) {
        self.init(value, radix: 10)
    }
#endif
}

fileprivate extension String {
