KeyHandler // Fix the usage of CChar(charCode).

- This will be problematic since it can throw nil.
This commit is contained in:
ShikiSuen 2022-08-08 22:16:33 +08:00
parent 7e4ff5e509
commit dfefaf8c7f
2 changed files with 12 additions and 6 deletions

View File

@ -307,13 +307,15 @@ extension KeyHandler {
let punctuationNamePrefix: String = generatePunctuationNamePrefix(withKeyCondition: input) let punctuationNamePrefix: String = generatePunctuationNamePrefix(withKeyCondition: input)
let parser = currentMandarinParser let parser = currentMandarinParser
let arrCustomPunctuations: [String] = [ let arrCustomPunctuations: [String] = [
punctuationNamePrefix, parser, String(format: "%c", CChar(charCode)), punctuationNamePrefix, parser, String(format: "%c", charCode.isPrintableASCII ? CChar(charCode) : inputText),
] ]
let customPunctuation: String = arrCustomPunctuations.joined(separator: "") let customPunctuation: String = arrCustomPunctuations.joined(separator: "")
/// ///
let arrPunctuations: [String] = [punctuationNamePrefix, String(format: "%c", CChar(charCode))] let arrPunctuations: [String] = [
punctuationNamePrefix, String(format: "%c", charCode.isPrintableASCII ? CChar(charCode) : inputText),
]
let punctuation: String = arrPunctuations.joined(separator: "") let punctuation: String = arrPunctuations.joined(separator: "")
var shouldAutoSelectCandidate: Bool = var shouldAutoSelectCandidate: Bool =
@ -321,7 +323,9 @@ extension KeyHandler {
|| currentLM.hasUnigramsFor(key: punctuation) || currentLM.hasUnigramsFor(key: punctuation)
if !shouldAutoSelectCandidate, input.isUpperCaseASCIILetterKey { if !shouldAutoSelectCandidate, input.isUpperCaseASCIILetterKey {
let letter: String! = String(format: "%@%c", "_letter_", CChar(charCode)) let letter: String! = String(
format: "%@%c", "_letter_", charCode.isPrintableASCII ? CChar(charCode) : inputText
)
if currentLM.hasUnigramsFor(key: letter) { shouldAutoSelectCandidate = true } if currentLM.hasUnigramsFor(key: letter) { shouldAutoSelectCandidate = true }
} }

View File

@ -329,7 +329,7 @@ extension KeyHandler {
let punctuationNamePrefix: String = generatePunctuationNamePrefix(withKeyCondition: input) let punctuationNamePrefix: String = generatePunctuationNamePrefix(withKeyCondition: input)
let parser = currentMandarinParser let parser = currentMandarinParser
let arrCustomPunctuations: [String] = [ let arrCustomPunctuations: [String] = [
punctuationNamePrefix, parser, String(format: "%c", CChar(charCode)), punctuationNamePrefix, parser, String(format: "%c", charCode.isPrintableASCII ? CChar(charCode) : inputText),
] ]
let customPunctuation: String = arrCustomPunctuations.joined(separator: "") let customPunctuation: String = arrCustomPunctuations.joined(separator: "")
if handlePunctuation( if handlePunctuation(
@ -344,7 +344,9 @@ extension KeyHandler {
/// ///
let arrPunctuations: [String] = [punctuationNamePrefix, String(format: "%c", CChar(charCode))] let arrPunctuations: [String] = [
punctuationNamePrefix, String(format: "%c", charCode.isPrintableASCII ? CChar(charCode) : inputText),
]
let punctuation: String = arrPunctuations.joined(separator: "") let punctuation: String = arrPunctuations.joined(separator: "")
if handlePunctuation( if handlePunctuation(
@ -359,7 +361,7 @@ extension KeyHandler {
// 使 2.2 // 使 2.2
if input.isUpperCaseASCIILetterKey { if input.isUpperCaseASCIILetterKey {
let letter: String! = String(format: "%@%c", "_letter_", CChar(charCode)) let letter: String! = String(format: "%@%c", "_letter_", charCode.isPrintableASCII ? CChar(charCode) : inputText)
if handlePunctuation( if handlePunctuation(
letter, letter,
state: state, state: state,