@@ -128,6 +128,7 @@ func parse(_ content: String) -> [String: Any]? {
128
128
let range = NSRange ( location: 0 , length: content. utf16. count)
129
129
// return nil/fail if metablock missing
130
130
guard let match = regex. firstMatch ( in: content, options: [ ] , range: range) else {
131
+ logger? . debug ( " \( #function, privacy: . public) - Non matched content: \( content, privacy: . public) " )
131
132
return nil
132
133
}
133
134
@@ -136,15 +137,23 @@ func parse(_ content: String) -> [String: Any]? {
136
137
// rather than being too strict, text content can precede the opening userscript tag, however it will be ignored
137
138
// adjust start index of file content while assigning group numbers to account for any text content preceding opening tag
138
139
let contentStartIndex = content. index ( content. startIndex, offsetBy: match. range. lowerBound)
139
- var g1 , g2 , g3 : Int
140
- if ( content [ contentStartIndex..< content. endIndex] . starts ( with: " // " ) ) {
140
+ let contentEndIndex = content. index ( contentStartIndex, offsetBy: 15 )
141
+ let metaHeadContent = content [ contentStartIndex..< contentEndIndex]
142
+ var g1 , g2 , g3 : Int
143
+ if ( metaHeadContent. starts ( with: " // " ) ) {
141
144
g1 = 1 ; g2 = 2 ; g3 = 3
142
145
} else {
143
146
g1 = 4 ; g2 = 5 ; g3 = 6
144
147
}
145
148
149
+ // unlikely to happen but did see some crashes, add checking and logging
150
+ guard let metablockRange = Range ( match. range ( at: g1) , in: content) else {
151
+ logger? . error ( " \( #function, privacy: . public) - Nil range ( \( g1, privacy: . public) ): \( metaHeadContent, privacy: . public) " )
152
+ logger? . debug ( " \( #function, privacy: . public) - Nil range content: \( content, privacy: . public) " )
153
+ return nil
154
+ }
146
155
// can force unwrap metablock since nil check was done above
147
- let metablock = content [ Range ( match . range ( at : g1 ) , in : content ) ! ]
156
+ let metablock = content [ metablockRange ]
148
157
// create var to store separated metadata keys/values
149
158
var metadata = [ : ] as [ String : [ String ] ]
150
159
// iterate through the possible metadata keys in file
0 commit comments