Repo // Fix format consolidators in LMConsolidator and UPE.

This commit is contained in:
ShikiSuen 2022-06-23 10:03:18 +08:00
parent 6edbbe4aa6
commit ee7e7870b4
2 changed files with 26 additions and 9 deletions

View File

@ -28,6 +28,9 @@ extension vChewing {
public enum LMConsolidator {
public static let kPragmaHeader = "# 𝙵𝙾𝚁𝙼𝙰𝚃 𝚘𝚛𝚐.𝚊𝚝𝚎𝚕𝚒𝚎𝚛𝙸𝚗𝚖𝚞.𝚟𝚌𝚑𝚎𝚠𝚒𝚗𝚐.𝚞𝚜𝚎𝚛𝙻𝚊𝚗𝚐𝚞𝚊𝚐𝚎𝙼𝚘𝚍𝚎𝚕𝙳𝚊𝚝𝚊.𝚏𝚘𝚛𝚖𝚊𝚝𝚝𝚎𝚍"
///
/// - Parameter path:
/// - Returns:
public static func checkPragma(path: String) -> Bool {
if FileManager.default.fileExists(atPath: path) {
let fileHandle = FileHandle(forReadingAtPath: path)!
@ -51,12 +54,17 @@ extension vChewing {
return false
}
///
/// - Parameter path:
/// - Returns:
@discardableResult public static func fixEOF(path: String) -> Bool {
let urlPath = URL(fileURLWithPath: path)
if FileManager.default.fileExists(atPath: path) {
var strIncoming = ""
do {
strIncoming += try String(contentsOf: urlPath, encoding: .utf8)
/// Swift LMConsolidator EOF
/// consolidate()
if !strIncoming.hasSuffix("\n") {
IME.prtDebugIntel("EOF Fix Necessity Confirmed, Start Fixing.")
if let writeFile = FileHandle(forUpdatingAtPath: path),
@ -81,10 +89,14 @@ extension vChewing {
return false
}
///
/// - Parameters:
/// - path:
/// - shouldCheckPragma:
/// - Returns:
@discardableResult public static func consolidate(path: String, pragma shouldCheckPragma: Bool) -> Bool {
var pragmaResult = false
let pragmaResult = checkPragma(path: path)
if shouldCheckPragma {
pragmaResult = checkPragma(path: path)
if pragmaResult {
return true
}
@ -105,6 +117,7 @@ extension vChewing {
strProcessed.regReplace(pattern: #"( +| +| +|\t+)+"#, replaceWith: " ")
//
strProcessed.regReplace(pattern: #"(^ | $)"#, replaceWith: "")
strProcessed.regReplace(pattern: #"(\n | \n)"#, replaceWith: "\n")
// CR & FF to LF,
strProcessed.regReplace(pattern: #"(\f+|\r+|\n+)+"#, replaceWith: "\n")
if strProcessed.prefix(1) == " " { //
@ -114,21 +127,21 @@ extension vChewing {
strProcessed.removeLast()
}
// Step 3: Add Formatted Pragma, the Sorted Header:
// Step 2: Add Formatted Pragma, the Sorted Header:
if !pragmaResult {
strProcessed = kPragmaHeader + "\n" + strProcessed // Add Sorted Header
}
// Step 4: Deduplication.
// Step 3: Deduplication.
let arrData = strProcessed.split(separator: "\n")
// reversed override
let arrDataDeduplicated = Array(NSOrderedSet(array: arrData.reversed()).array as! [String])
strProcessed = arrDataDeduplicated.reversed().joined(separator: "\n") + "\n"
// Step 5: Remove duplicated newlines at the end of the file.
strProcessed.regReplace(pattern: "\\n+", replaceWith: "\n")
// Step 4: Remove duplicated newlines at the end of the file.
strProcessed.regReplace(pattern: #"\n+"#, replaceWith: "\n")
// Step 6: Write consolidated file contents.
// Step 5: Write consolidated file contents.
try strProcessed.write(to: urlPath, atomically: false, encoding: .utf8)
} catch {

View File

@ -52,14 +52,18 @@ extension String {
// Tab to ASCII Space
// ASCII
strProcessed.regReplace(pattern: #"( +| +| +|\t+)+"#, replaceWith: " ")
strProcessed.regReplace(pattern: #"(^ | $)"#, replaceWith: "") //
strProcessed.regReplace(pattern: #"(\f+|\r+|\n+)+"#, replaceWith: "\n") // CR & FF to LF,
//
strProcessed.regReplace(pattern: #"(^ | $)"#, replaceWith: "")
strProcessed.regReplace(pattern: #"(\n | \n)"#, replaceWith: "\n")
// CR & FF to LF,
strProcessed.regReplace(pattern: #"(\f+|\r+|\n+)+"#, replaceWith: "\n")
if strProcessed.prefix(1) == " " { //
strProcessed.removeFirst()
}
if strProcessed.suffix(1) == " " { //
strProcessed.removeLast()
}
if cnvHYPYtoBPMF {
// Step 2: Convert HanyuPinyin to Bopomofo.
//