Skip to content

Commit 5cee971

Browse files
committed
Patch cnvHanyuPinyinToPhona.
1 parent 32bb684 commit 5cee971

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

Sources/Tekkon/Tekkon_Utilities.swift

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,9 @@ extension Tekkon {
6464
newToneOne: String = ""
6565
)
6666
-> String {
67-
/// 如果當前內容有任何除了半形英數內容以外的內容的話,就直接放棄轉換。
68-
if targetJoined.contains("_") || !targetJoined.isNotPureAlphanumerical { return targetJoined }
67+
/// 如果當前內容含有底線或包含任何不在允許列表中的字元(英數、空白、Tab、連字號),則放棄轉換。
68+
if targetJoined.contains("_") || targetJoined
69+
.isNotAllowedCharOfPinyinChain { return targetJoined }
6970
var result = targetJoined
7071
for key in Tekkon.mapHanyuPinyin.keys.sorted(by: { $0.count > $1.count }) {
7172
guard let value = Tekkon.mapHanyuPinyin[key] else { continue }
@@ -81,23 +82,31 @@ extension Tekkon {
8182

8283
/// 檢測字串是否包含半形英數內容
8384
extension String {
84-
fileprivate var isNotPureAlphanumerical: Bool {
85+
fileprivate var isNotAllowedCharOfPinyinChain: Bool {
8586
let x = unicodeScalars.map(\.value).filter {
87+
// allowed: 0-9, A-Z, a-z, space(32), tab(9), dash(45)
8688
if $0 >= 48, $0 <= 57 { return false }
8789
if $0 >= 65, $0 <= 90 { return false }
8890
if $0 >= 97, $0 <= 122 { return false }
91+
if $0 == 32 { return false } // space
92+
if $0 == 9 { return false } // tab
93+
if $0 == 45 { return false } // -
8994
return true
9095
}
9196
return !x.isEmpty
9297
}
9398
}
9499

95100
extension Character {
96-
fileprivate var isNotPureAlphanumerical: Bool {
101+
fileprivate var isNotAllowedCharOfPinyinChain: Bool {
97102
let x = unicodeScalars.map(\.value).filter {
103+
// allowed: 0-9, A-Z, a-z, space(32), tab(9), dash(45)
98104
if $0 >= 48, $0 <= 57 { return false }
99105
if $0 >= 65, $0 <= 90 { return false }
100106
if $0 >= 97, $0 <= 122 { return false }
107+
if $0 == 32 { return false } // space
108+
if $0 == 9 { return false } // tab
109+
if $0 == 45 { return false } // -
101110
return true
102111
}
103112
return !x.isEmpty

Tests/TekkonTests/TekkonTests_Basic.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ final class TekkonTestsBasic: XCTestCase {
201201
XCTAssertEqual(Tekkon.cnvPhonaToTextbookStyle(target: "ㄓㄜ˙"), "˙ㄓㄜ")
202202
XCTAssertEqual(Tekkon.cnvPhonaToHanyuPinyin(targetJoined: "ㄍㄢˋ"), "gan4")
203203
XCTAssertEqual(Tekkon.cnvHanyuPinyinToTextbookStyle(targetJoined: "起(qi3)居(ju1)"), "起(qǐ)居(jū)")
204+
XCTAssertEqual(Tekkon.cnvHanyuPinyinToPhona(targetJoined: "bian4"), "ㄅㄧㄢˋ")
204205
XCTAssertEqual(Tekkon.cnvHanyuPinyinToPhona(targetJoined: "bian4-le5-tian1"), "ㄅㄧㄢˋ-ㄌㄜ˙-ㄊㄧㄢ")
205206
// 測試這種情形:「如果傳入的字串不包含任何半形英數內容的話,那麼應該直接將傳入的字串原樣返回」。
206207
XCTAssertEqual(Tekkon.cnvHanyuPinyinToPhona(targetJoined: "ㄅㄧㄢˋ-˙ㄌㄜ-ㄊㄧㄢ"), "ㄅㄧㄢˋ-˙ㄌㄜ-ㄊㄧㄢ")

0 commit comments

Comments
 (0)