mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-11 09:28:38 +00:00
+ strip comments in config file
This commit is contained in:
parent
3375308386
commit
b4f62f6be1
@ -2,11 +2,9 @@ import Foundation
|
|||||||
import AppKit
|
import AppKit
|
||||||
|
|
||||||
extension Data {
|
extension Data {
|
||||||
|
|
||||||
func barItemDefinitions() -> [BarItemDefinition]? {
|
func barItemDefinitions() -> [BarItemDefinition]? {
|
||||||
return try? JSONDecoder().decode([BarItemDefinition].self, from: self)
|
return try? JSONDecoder().decode([BarItemDefinition].self, from: self.utf8string!.stripComments().data(using: .utf8)!)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
struct BarItemDefinition: Decodable {
|
struct BarItemDefinition: Decodable {
|
||||||
|
|||||||
@ -12,6 +12,67 @@ extension String {
|
|||||||
func trim() -> String {
|
func trim() -> String {
|
||||||
return self.trimmingCharacters(in: NSCharacterSet.whitespaces)
|
return self.trimmingCharacters(in: NSCharacterSet.whitespaces)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func substring(from: Int, to: Int) -> String {
|
||||||
|
let start = index(startIndex, offsetBy: from)
|
||||||
|
let end = index(start, offsetBy: to - from)
|
||||||
|
return String(self[start ..< end])
|
||||||
|
}
|
||||||
|
|
||||||
|
func substring(range: NSRange) -> String {
|
||||||
|
return substring(from: range.lowerBound, to: range.upperBound)
|
||||||
|
}
|
||||||
|
|
||||||
|
func stripComments() -> String {
|
||||||
|
let str = self
|
||||||
|
let singleComment = 1;
|
||||||
|
let multiComment = 2;
|
||||||
|
var insideString = false
|
||||||
|
var insideComment = 0
|
||||||
|
var offset = 0
|
||||||
|
var ret = ""
|
||||||
|
|
||||||
|
for var i in 0..<str.count - 1 {
|
||||||
|
let currentChar = Array(str)[i]
|
||||||
|
let nextChar = Array(str)[i+1]
|
||||||
|
|
||||||
|
if (insideComment == 0 && currentChar == "\"") {
|
||||||
|
let escaped = Array(str)[i - 1] == "\\" && Array(str)[i - 2] != "\\"
|
||||||
|
if (!escaped) {
|
||||||
|
insideString = !insideString
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (insideString) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
if (insideComment == 0 && String(currentChar) + String(nextChar) == "//") {
|
||||||
|
ret += str.substring(from: offset, to: i)
|
||||||
|
offset = i
|
||||||
|
insideComment = singleComment
|
||||||
|
i += 1
|
||||||
|
} else if (insideComment == singleComment && String(currentChar) + String(nextChar) == "\r\n") {
|
||||||
|
i += 1
|
||||||
|
insideComment = 0
|
||||||
|
offset = i
|
||||||
|
} else if (insideComment == singleComment && currentChar == "\n") {
|
||||||
|
insideComment = 0
|
||||||
|
offset = i
|
||||||
|
} else if (insideComment == 0 && String(currentChar) + String(nextChar) == "/*") {
|
||||||
|
ret += str.substring(from: offset, to: i)
|
||||||
|
offset = i
|
||||||
|
insideComment = multiComment
|
||||||
|
i += 1
|
||||||
|
} else if (insideComment == multiComment && String(currentChar) + String(nextChar) == "*/") {
|
||||||
|
i += 1
|
||||||
|
insideComment = 0
|
||||||
|
offset = i + 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ret + str.substring(from: offset, to: str.count)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension NSImage {
|
extension NSImage {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user