Skip to content

Commit

Permalink
Updated translator id parsing regex to include only proper translator…
Browse files Browse the repository at this point in the history
…s ids (#749)
  • Loading branch information
michalrentka committed Aug 16, 2023
1 parent d431d67 commit 3c174ba
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
4 changes: 2 additions & 2 deletions Zotero/Controllers/TranslatorsAndStylesController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ final class TranslatorsAndStylesController {
weak var coordinator: TranslatorsControllerCoordinatorDelegate?
private lazy var uuidExpression: NSRegularExpression? = {
do {
return try NSRegularExpression(pattern: #"[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12}"#)
return try NSRegularExpression(pattern: #"setTranslator\(['"](?<uuid>[0-9a-fA-F]{8}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{4}\-[0-9a-fA-F]{12})['"]\)"#)
} catch let error {
DDLogError("TranslatorsAndStylesController: can't create uuid expression - \(error)")
return nil
Expand Down Expand Up @@ -688,7 +688,7 @@ final class TranslatorsAndStylesController {
guard let uuidRegex = self.uuidExpression else { return [] }

let matches = uuidRegex.matches(in: code, options: [], range: NSRange(code.startIndex..., in: code))
return Set(matches.compactMap({ $0.substring(at: 0, in: code).flatMap(String.init) }))
return Set(matches.compactMap({ $0.substring(withName: "uuid", in: code).flatMap(String.init) }))
}

/// Finds `endIndex` of metadata part in translator file (translator file consists of json metadata and code).
Expand Down
16 changes: 16 additions & 0 deletions Zotero/Extensions/RegularExpression+Extensions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,27 @@ extension NSTextCheckingResult {
return Range(self.range(at: index), in: string)
}

/// Creates Swift `Range` from result for given group name in string.
/// - parameter name: Group name of matched range in this result.
/// - parameter string: String from which this result originates.
/// - returns: `Range` if index is in bounds, `nil` otherwise.
func swiftRange(withName name: String, in string: String) -> Range<String.Index>? {
return Range(self.range(withName: name), in: string)
}

/// Creates substring from result at given index in string.
/// - parameter index: Index of matched substring in this result.
/// - parameter string: String from which this result originates.
/// - returns: Substring if index is in bounds, `nil` otherwise.
func substring(at index: Int, in string: String) -> Substring? {
return self.swiftRange(at: index, in: string).flatMap({ string[$0] })
}

/// Creates substring from result for given group name in string.
/// - parameter name: Group name of matched substring in this result.
/// - parameter string: String from which this result originates.
/// - returns: Substring if index is in bounds, `nil` otherwise.
func substring(withName name: String, in string: String) -> Substring? {
return self.swiftRange(withName: name, in: string).flatMap({ string[$0] })
}
}

0 comments on commit 3c174ba

Please sign in to comment.