Created
March 5, 2024 15:29
-
-
Save ShikiSuen/c470a0455429928bc0184d1887a1f171 to your computer and use it in GitHub Desktop.
ApplyTransformFW2HW
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// (c) 2021 and onwards The vChewing Project (MIT-NTL License). | |
// ==================== | |
// This code is released under the MIT license (SPDX-License-Identifier: MIT) | |
// ... with NTL restriction stating that: | |
// No trademark license is granted to use the trade names, trademarks, service | |
// marks, or product names of Contributor, except as required to fulfill notice | |
// requirements defined in MIT License. | |
extension String { | |
// This only works with ASCII chars. | |
func applyingTransformFW2HW(reverse: Bool) -> String { | |
var arr: [Character] = map { $0 } | |
for i in 0..<arr.count { | |
let oldChar = arr[i] | |
guard oldChar.unicodeScalars.count == 1 else { continue } | |
guard let oldCodePoint = oldChar.unicodeScalars.first?.value else { continue } | |
if reverse { | |
guard oldChar.isASCII else { continue } | |
} else { | |
guard oldCodePoint > 0xFEE0 || oldCodePoint == 0x3000 else { continue } | |
} | |
var newCodePoint: Int32 = reverse ? (Int32(oldCodePoint) + 0xFEE0) : (Int32(oldCodePoint) - 0xFEE0) | |
checkSpace: switch (oldCodePoint, reverse) { | |
case (0x3000, false): newCodePoint = 0x20 | |
case (0x20, true): newCodePoint = 0x3000 | |
default: break checkSpace | |
} | |
guard newCodePoint > 0 else { continue } | |
guard let newScalar = Unicode.Scalar(UInt16(newCodePoint)) else { continue } | |
let newChar = Character(newScalar) | |
arr[i] = newChar | |
} | |
return String(arr) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment