1
0
mirror of https://github.com/Toxblh/MTMR.git synced 2026-01-10 17:08:39 +00:00

+ strip comments in config file

This commit is contained in:
ad 2018-04-22 12:15:04 +03:00
parent 3375308386
commit b4f62f6be1
2 changed files with 62 additions and 3 deletions

View File

@ -2,11 +2,9 @@ import Foundation
import AppKit
extension Data {
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 {

View File

@ -12,6 +12,67 @@ extension String {
func trim() -> String {
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 {