LMConsolidator // Add consolidate(text:).

This commit is contained in:
ShikiSuen 2022-12-02 17:12:26 +08:00
parent d2046968cf
commit 2913c22d40
1 changed files with 50 additions and 40 deletions

View File

@ -77,6 +77,52 @@ extension vChewingLM {
return false return false
} }
///
/// - Parameters:
/// - text:
/// - shouldCheckPragma:
public static func consolidate(text strProcessed: inout String, pragma shouldCheckPragma: Bool) {
var pragmaResult: Bool {
let realPragmaHeader = kPragmaHeader + "\n"
if strProcessed.count <= kPragmaHeader.count { return false }
let range = 0..<(realPragmaHeader.count)
let fetchedPragma = ContiguousArray(strProcessed.utf8CString[range])
return fetchedPragma == realPragmaHeader.utf8CString
}
if shouldCheckPragma, pragmaResult { return }
// Step 1: Consolidating formats per line.
// -------
// CJKWhiteSpace (\x{3000}) to ASCII Space
// NonBreakWhiteSpace (\x{A0}) to ASCII Space
// Tab to ASCII Space
// ASCII
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) == " " { //
strProcessed.removeFirst()
}
if strProcessed.suffix(1) == " " { //
strProcessed.removeLast()
}
strProcessed = kPragmaHeader + "\n" + strProcessed // Add Pragma Header
// 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 4: Remove duplicated newlines at the end of the file.
strProcessed.regReplace(pattern: #"\n+"#, replaceWith: "\n")
}
/// ///
/// - Parameters: /// - Parameters:
/// - path: /// - path:
@ -92,49 +138,13 @@ extension vChewingLM {
let urlPath = URL(fileURLWithPath: path) let urlPath = URL(fileURLWithPath: path)
if FileManager.default.fileExists(atPath: path) { if FileManager.default.fileExists(atPath: path) {
var strProcessed = ""
do { do {
strProcessed += try String(contentsOf: urlPath, encoding: .utf8) var strProcessed = try String(contentsOf: urlPath, encoding: .utf8)
consolidate(text: &strProcessed, pragma: shouldCheckPragma)
// Step 1: Consolidating formats per line. // Write consolidated file contents.
// -------
// CJKWhiteSpace (\x{3000}) to ASCII Space
// NonBreakWhiteSpace (\x{A0}) to ASCII Space
// Tab to ASCII Space
// ASCII
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) == " " { //
strProcessed.removeFirst()
}
if strProcessed.suffix(1) == " " { //
strProcessed.removeLast()
}
// Step 2: Add Formatted Pragma, the Sorted Header:
if !pragmaResult {
strProcessed = kPragmaHeader + "\n" + strProcessed // Add Sorted Header
}
// 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 4: Remove duplicated newlines at the end of the file.
strProcessed.regReplace(pattern: #"\n+"#, replaceWith: "\n")
// Step 5: Write consolidated file contents.
try strProcessed.write(to: urlPath, atomically: false, encoding: .utf8) try strProcessed.write(to: urlPath, atomically: false, encoding: .utf8)
} catch { } catch {
vCLog("Consolidation Failed w/ File: \(path)") vCLog("Consolidation Failed w/ File: \(path), error: \(error)")
vCLog("Consolidation Failed w/ Error: \(error).")
return false return false
} }
vCLog("Either Consolidation Successful Or No-Need-To-Consolidate.") vCLog("Either Consolidation Successful Or No-Need-To-Consolidate.")