-
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathOptionDirectInputResolver.swift
More file actions
43 lines (40 loc) · 1.34 KB
/
OptionDirectInputResolver.swift
File metadata and controls
43 lines (40 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import Foundation
public enum OptionDirectInputResolver {
public static func resolve(
characters: String?,
modifierFlags: KeyEventCore.ModifierFlag,
inputLanguage: InputLanguage,
inputState: InputState,
typeBackSlash: Bool
) -> String? {
guard inputLanguage == .japanese, inputState == .none else {
return nil
}
guard modifierFlags == [.option] || modifierFlags == [.option, .shift] else {
return nil
}
guard let characters,
!characters.isEmpty,
isPrintable(characters)
else {
return nil
}
let normalized = normalize(characters, typeBackSlash: typeBackSlash)
return normalized.applyingTransform(.fullwidthToHalfwidth, reverse: true)
}
private static func isPrintable(_ text: String) -> Bool {
let printable: CharacterSet = [.alphanumerics, .symbols, .punctuationCharacters]
.reduce(into: CharacterSet()) {
$0.formUnion($1)
}
return CharacterSet(text.unicodeScalars).isSubset(of: printable)
}
private static func normalize(_ text: String, typeBackSlash: Bool) -> String {
switch text {
case "¥", "\\":
typeBackSlash ? "\\" : "¥"
default:
text
}
}
}