From 5345765ec9a40495705953169e0a976b2c41fb69 Mon Sep 17 00:00:00 2001 From: Vitaly Posadnev Date: Mon, 19 Aug 2024 22:26:08 +0300 Subject: [PATCH] fix: change type of cycle --- .../Services/Search/StringSearchService.swift | 44 +++++++++---------- 1 file changed, 20 insertions(+), 24 deletions(-) diff --git a/Sources/Shared/Publication/Services/Search/StringSearchService.swift b/Sources/Shared/Publication/Services/Search/StringSearchService.swift index 1ddbee7c2..9fc396325 100644 --- a/Sources/Shared/Publication/Services/Search/StringSearchService.swift +++ b/Sources/Shared/Publication/Services/Search/StringSearchService.swift @@ -120,35 +120,31 @@ public class _StringSearchService: _SearchService { } private func findNext(_ cancellable: CancellableObject, _ completion: @escaping (SearchResult<_LocatorCollection?>) -> Void) { - guard index < publication.readingOrder.count - 1 else { - completion(.success(nil)) - return - } - - index += 1 + while index < publication.readingOrder.count - 1 { + index += 1 - let link = publication.readingOrder[index] - let resource = publication.get(link) + let link = publication.readingOrder[index] + let resource = publication.get(link) - do { - guard let extractor = extractorFactory.makeExtractor(for: resource) else { - log(.warning, "Cannot extract text from resource: \(link.href)") - return findNext(cancellable, completion) - } - let text = try extractor.extractText(of: resource).get() + do { + guard let extractor = extractorFactory.makeExtractor(for: resource) else { + log(.warning, "Cannot extract text from resource: \(link.href)") + continue + } + let text = try extractor.extractText(of: resource).get() - let locators = findLocators(in: link, resourceIndex: index, text: text, cancellable: cancellable) - // If no occurrences were found in the current resource, skip to the next one automatically. - guard !locators.isEmpty else { - return findNext(cancellable, completion) + let locators = findLocators(in: link, resourceIndex: index, text: text, cancellable: cancellable) + if !locators.isEmpty { + resultCount = (resultCount ?? 0) + locators.count + completion(.success(_LocatorCollection(locators: locators))) + } + } catch { + completion(.failure(.wrap(error))) + return } - - resultCount = (resultCount ?? 0) + locators.count - completion(.success(_LocatorCollection(locators: locators))) - - } catch { - completion(.failure(.wrap(error))) } + + completion(.success(nil)) } private func findLocators(in link: Link, resourceIndex: Int, text: String, cancellable: CancellableObject) -> [Locator] {