Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated translator id parsing regex to include only proper translator ids #749

Merged
merged 1 commit into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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] })
}
}