Merge pull request #113 from jpsim/nn-refactor-constructor

Refactor Constructor
This commit is contained in:
Norio Nomura 2018-04-02 18:12:56 +09:00 committed by GitHub
commit b0a7f7c945
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 25 additions and 26 deletions

View File

@ -124,13 +124,15 @@ extension Date: ScalarConstructible {
datecomponents.hour = components[3].flatMap { Int($0) }
datecomponents.minute = components[4].flatMap { Int($0) }
datecomponents.second = components[5].flatMap { Int($0) }
datecomponents.nanosecond = components[6].flatMap {
let length = $0.count
datecomponents.nanosecond = components[6].flatMap { fraction in
let length = fraction.count
let nanosecond: Int?
if length < 9 {
nanosecond = Int($0 + String(repeating: "0", count: 9 - length))
nanosecond = Int(fraction).map { number in
repeatElement(10, count: 9 - length).reduce(number, *)
}
} else {
nanosecond = Int($0[..<$0.index($0.startIndex, offsetBy: 9)])
nanosecond = Int(fraction[..<fraction.index(fraction.startIndex, offsetBy: 9)])
}
return nanosecond
}
@ -360,11 +362,16 @@ private extension String {
}
}
private extension String {
func substring(from offset: Int) -> Substring {
let index = self.index(startIndex, offsetBy: offset)
return self[index...]
private extension StringProtocol {
#if swift(>=4.1)
func substring(from offset: Int) -> SubSequence {
return self[index(startIndex, offsetBy: offset)...]
}
#else
func substring(from offset: IndexDistance) -> SubSequence {
return self[index(startIndex, offsetBy: offset)...]
}
#endif
}
// MARK: - SexagesimalConvertible
@ -422,24 +429,6 @@ private extension String {
}
}
private extension Substring {
#if os(Linux)
func hasPrefix(_ prefix: String) -> Bool {
return String(self).hasPrefix(prefix)
}
func components(separatedBy separator: String) -> [String] {
return String(self).components(separatedBy: separator)
}
#endif
func substring(from offset: Int) -> Substring {
if offset == 0 { return self }
let index = self.index(startIndex, offsetBy: offset)
return self[index...]
}
}
// MARK: - Unavailable
extension Constructor {

View File

@ -17,3 +17,13 @@
}
#endif
#if os(Linux) && !swift(>=4.2)
extension Substring {
func hasPrefix(_ prefix: String) -> Bool {
return String(self).hasPrefix(prefix)
}
}
#endif