@@ -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/// 檢測字串是否包含半形英數內容
8384extension 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
95100extension 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
0 commit comments