LMAssembly // Add replaceData() to certain submodels.

- LMCoreEX // Add replaceData(), etc.
- LMReplacements // Add replaceData(), etc.
- LMAssociates // Add replaceData(), etc.
This commit is contained in:
ShikiSuen 2022-12-02 17:07:09 +08:00
parent 2913c22d40
commit cd85f821d2
3 changed files with 69 additions and 39 deletions

View File

@ -42,8 +42,23 @@ extension vChewingLM {
LMConsolidator.consolidate(path: path, pragma: true)
do {
strData = try String(contentsOfFile: path, encoding: .utf8).replacingOccurrences(of: "\t", with: " ")
strData = strData.replacingOccurrences(of: "\r", with: "\n")
let rawStrData = try String(contentsOfFile: path, encoding: .utf8)
replaceData(textData: rawStrData)
} catch {
vCLog("\(error)")
vCLog("↑ Exception happened when reading data at: \(path).")
return false
}
return true
}
///
/// - parameters:
/// - path:
public mutating func replaceData(textData rawStrData: String) {
if strData == rawStrData { return }
strData = rawStrData
strData.ranges(splitBy: "\n").filter { !$0.isEmpty }.forEach {
let neta = strData[$0].split(separator: " ")
if neta.count >= 2 {
@ -56,13 +71,6 @@ extension vChewingLM {
}
}
}
} catch {
vCLog("\(error)")
vCLog("↑ Exception happened when reading data at: \(path).")
return false
}
return true
}
public mutating func clear() {

View File

@ -60,15 +60,36 @@ extension vChewingLM {
/// - path:
@discardableResult public mutating func open(_ path: String) -> Bool {
if isLoaded { return false }
var consolidated = false
if allowConsolidation {
LMConsolidator.fixEOF(path: path)
LMConsolidator.consolidate(path: path, pragma: true)
consolidated = true
}
do {
strData = try String(contentsOfFile: path, encoding: .utf8).replacingOccurrences(of: "\t", with: " ")
strData = strData.replacingOccurrences(of: "\r", with: "\n")
var rawStrData = try String(contentsOfFile: path, encoding: .utf8)
if !consolidated {
rawStrData = rawStrData.replacingOccurrences(of: "\t", with: " ")
rawStrData = rawStrData.replacingOccurrences(of: "\r", with: "\n")
}
replaceData(textData: rawStrData)
} catch {
vCLog("\(error)")
vCLog("↑ Exception happened when reading data at: \(path).")
return false
}
return true
}
///
/// - parameters:
/// - path:
public mutating func replaceData(textData rawStrData: String) {
if strData == rawStrData { return }
strData = rawStrData
strData.ranges(splitBy: "\n").filter { !$0.isEmpty }.forEach {
let neta = strData[$0].split(separator: " ")
if neta.count >= 2, String(neta[0]).first != "#" {
@ -81,13 +102,6 @@ extension vChewingLM {
}
}
temporaryMap.removeAll()
} catch {
vCLog("\(error)")
vCLog("↑ Exception happened when reading data at: \(path).")
return false
}
return true
}
///

View File

@ -29,8 +29,23 @@ extension vChewingLM {
LMConsolidator.consolidate(path: path, pragma: true)
do {
strData = try String(contentsOfFile: path, encoding: .utf8).replacingOccurrences(of: "\t", with: " ")
strData = strData.replacingOccurrences(of: "\r", with: "\n")
let rawStrData = try String(contentsOfFile: path, encoding: .utf8)
replaceData(textData: rawStrData)
} catch {
vCLog("\(error)")
vCLog("↑ Exception happened when reading data at: \(path).")
return false
}
return true
}
///
/// - parameters:
/// - path:
public mutating func replaceData(textData rawStrData: String) {
if strData == rawStrData { return }
strData = rawStrData
strData.ranges(splitBy: "\n").filter { !$0.isEmpty }.forEach {
let neta = strData[$0].split(separator: " ")
if neta.count >= 2 {
@ -41,13 +56,6 @@ extension vChewingLM {
}
}
}
} catch {
vCLog("\(error)")
vCLog("↑ Exception happened when reading data at: \(path).")
return false
}
return true
}
public mutating func clear() {