From fd15be901768236ea21ff5a9010da66cf3f03308 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Crazy=E5=87=A1?= <827799383@qq.com> Date: Sat, 14 Nov 2020 22:31:57 +0800 Subject: [PATCH] Replace 'operator <==>' with Equatable --- .gitignore | 2 + K3Pinyin.podspec | 4 +- Package.swift | 3 +- Sources/K3Pinyin/K3Pinyin.swift | 5 ++- Sources/K3Pinyin/K3PinyinOptions.swift | 57 ++++++++------------------ 5 files changed, 27 insertions(+), 44 deletions(-) diff --git a/.gitignore b/.gitignore index 8a5914e..d0536e0 100644 --- a/.gitignore +++ b/.gitignore @@ -210,3 +210,5 @@ fastlane/test_output # pod **/Pods/* + +.build diff --git a/K3Pinyin.podspec b/K3Pinyin.podspec index 1644bfa..ce6c5cc 100644 --- a/K3Pinyin.podspec +++ b/K3Pinyin.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |s| s.name = "K3Pinyin" - s.version = "1.0.0" + s.version = "2.0.0" s.summary = "a simple wap to use pinyin with swift." s.description = <<-DESC @@ -15,5 +15,5 @@ Pod::Spec.new do |s| s.source = { :git => "https://github.com/CrazyFanFan/K3Pinyin.git", :tag => "#{s.version}" } s.source_files = "Sources/**/*.{swift}" s.requires_arc = true - s.swift_version = '5.0' + s.swift_version = '5.3' end diff --git a/Package.swift b/Package.swift index 39ba29f..99c7ef5 100644 --- a/Package.swift +++ b/Package.swift @@ -24,5 +24,6 @@ let package = Package( .testTarget( name: "K3PinyinTests", dependencies: ["K3Pinyin"]), - ] + ], + swiftLanguageVersions: [.v5] ) diff --git a/Sources/K3Pinyin/K3Pinyin.swift b/Sources/K3Pinyin/K3Pinyin.swift index 37311ce..f23635b 100644 --- a/Sources/K3Pinyin/K3Pinyin.swift +++ b/Sources/K3Pinyin/K3Pinyin.swift @@ -23,7 +23,10 @@ public extension K3Pinyin where BaseType == String { let cases = options ?? [] // get only first char when case onlyFirst... - let source = (cases.onlyFirstCharacter || cases.onlyFirstLetter ? "\(base.prefix(1))" : base) + var source = base + if cases.onlyFirstCharacter || cases.onlyFirstLetter { + source = String(base.prefix(1)) + } // get pinyin let cfString = CFStringCreateMutableCopy(nil, 0, source as CFString) diff --git a/Sources/K3Pinyin/K3PinyinOptions.swift b/Sources/K3Pinyin/K3PinyinOptions.swift index f03b11f..9c1bc5e 100644 --- a/Sources/K3Pinyin/K3PinyinOptions.swift +++ b/Sources/K3Pinyin/K3PinyinOptions.swift @@ -10,7 +10,7 @@ import Foundation public typealias K3PinyinOptions = [K3PinyinOption] -public enum K3PinyinOption { +public enum K3PinyinOption: Equatable { case stripCombiningMarks case separator(String) case onlyFirstCharacter @@ -19,53 +19,30 @@ public enum K3PinyinOption { case capitalized } -precedencegroup OptionComparisonPrecedence { - associativity: none - higherThan: LogicalConjunctionPrecedence -} - -infix operator <==>: OptionComparisonPrecedence - -/// This operator returns true if two `K3PinyinOption` enum is the same, -/// without considering the associated values. -func <==> (lhs: K3PinyinOption, rhs: K3PinyinOption) -> Bool { - switch (lhs, rhs) { - case (.stripCombiningMarks, .stripCombiningMarks): return true - case (.separator, .separator): return true - case (.onlyFirstCharacter, .onlyFirstCharacter): return true - case (.allFirstLetter, .allFirstLetter): return true - case (.onlyFirstLetter, .onlyFirstLetter): return true - case (.capitalized, .capitalized): return true - default: return false - } -} - public extension Collection where Iterator.Element == K3PinyinOption { - var stripCombiningMarks: Bool { - return contains { $0 <==> .stripCombiningMarks } - } + @inlinable + var stripCombiningMarks: Bool { contains(.stripCombiningMarks) } var separator: String? { - if let item = reversed().first(where: { $0 <==> .separator("") }), - case .separator(let separator) = item { - return separator + for option in reversed() { + switch option { + case .separator(let separator): + return separator + default: break + } } return nil } - var onlyFirstCharacter: Bool { - return contains { $0 <==> .onlyFirstCharacter } - } + @inlinable + var onlyFirstCharacter: Bool { contains(.onlyFirstCharacter) } - var allFirstLetter: Bool { - return contains { $0 <==> .allFirstLetter } - } + @inlinable + var allFirstLetter: Bool { contains(.allFirstLetter) } - var onlyFirstLetter: Bool { - return contains { $0 <==> .onlyFirstLetter } - } + @inlinable + var onlyFirstLetter: Bool { contains(.onlyFirstLetter) } - var capitalized: Bool { - return contains { $0 <==> .capitalized } - } + @inlinable + var capitalized: Bool { contains(.capitalized) } }