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,24 +77,20 @@ extension vChewingLM {
return false return false
} }
/// ///
/// - Parameters: /// - Parameters:
/// - path: /// - text:
/// - shouldCheckPragma: /// - shouldCheckPragma:
/// - Returns: public static func consolidate(text strProcessed: inout String, pragma shouldCheckPragma: Bool) {
@discardableResult public static func consolidate(path: String, pragma shouldCheckPragma: Bool) -> Bool { var pragmaResult: Bool {
let pragmaResult = checkPragma(path: path) let realPragmaHeader = kPragmaHeader + "\n"
if shouldCheckPragma { if strProcessed.count <= kPragmaHeader.count { return false }
if pragmaResult { let range = 0..<(realPragmaHeader.count)
return true let fetchedPragma = ContiguousArray(strProcessed.utf8CString[range])
} return fetchedPragma == realPragmaHeader.utf8CString
} }
let urlPath = URL(fileURLWithPath: path) if shouldCheckPragma, pragmaResult { return }
if FileManager.default.fileExists(atPath: path) {
var strProcessed = ""
do {
strProcessed += try String(contentsOf: urlPath, encoding: .utf8)
// Step 1: Consolidating formats per line. // Step 1: Consolidating formats per line.
// ------- // -------
@ -115,10 +111,7 @@ extension vChewingLM {
strProcessed.removeLast() strProcessed.removeLast()
} }
// Step 2: Add Formatted Pragma, the Sorted Header: strProcessed = kPragmaHeader + "\n" + strProcessed // Add Pragma Header
if !pragmaResult {
strProcessed = kPragmaHeader + "\n" + strProcessed // Add Sorted Header
}
// Step 3: Deduplication. // Step 3: Deduplication.
let arrData = strProcessed.split(separator: "\n") let arrData = strProcessed.split(separator: "\n")
@ -128,13 +121,30 @@ extension vChewingLM {
// Step 4: Remove duplicated newlines at the end of the file. // Step 4: Remove duplicated newlines at the end of the file.
strProcessed.regReplace(pattern: #"\n+"#, replaceWith: "\n") strProcessed.regReplace(pattern: #"\n+"#, replaceWith: "\n")
}
// Step 5: Write consolidated file contents. ///
/// - Parameters:
/// - path:
/// - shouldCheckPragma:
/// - Returns:
@discardableResult public static func consolidate(path: String, pragma shouldCheckPragma: Bool) -> Bool {
let pragmaResult = checkPragma(path: path)
if shouldCheckPragma {
if pragmaResult {
return true
}
}
let urlPath = URL(fileURLWithPath: path)
if FileManager.default.fileExists(atPath: path) {
do {
var strProcessed = try String(contentsOf: urlPath, encoding: .utf8)
consolidate(text: &strProcessed, pragma: shouldCheckPragma)
// 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.")