From fa7366cf25f6baa545050a58277265d042c5907e Mon Sep 17 00:00:00 2001 From: Uros Gazvoda <25570106+ugzv@users.noreply.github.com> Date: Tue, 10 Nov 2026 10:35:00 +0100 Subject: [PATCH] fix(detect): bound the line read around each candidate --- Sources/CodeCatchCore/CodeExtractor.swift | 22 +++++++++++++++---- .../CodeExtractorTests.swift | 6 ++--- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/Sources/CodeCatchCore/CodeExtractor.swift b/Sources/CodeCatchCore/CodeExtractor.swift index ef983c9..3237d2f 100644 --- a/Sources/CodeCatchCore/CodeExtractor.swift +++ b/Sources/CodeCatchCore/CodeExtractor.swift @@ -49,7 +49,7 @@ public enum CodeExtractor { // Cut from a window, not from everything on that side: copying the rest of a long mail per candidate stalled it. let before = ns.substring(with: NSRange(location: max(0, range.location - 640), length: min(640, range.location))).suffix(40) let after = ns.substring(with: NSRange(location: range.upperBound, length: min(640, ns.length - range.upperBound))).prefix(40) - let line = ns.lineRange(for: range) + let line = line(around: range, in: ns) // A letters-only word set straight after the keyword is just the next word ("KODE RAHASIA utk", // "NEW API TOKEN CREATED"); "YOUR CODE IS SZBPM", "code: SZBPM" and "SZBPM is your code" name one. if !isNumeric, !raw.contains(where: \.isNumber), !matches(introducer, String(before)), !matches(explicitAfter, String(after)) { @@ -310,7 +310,7 @@ public enum CodeExtractor { private static func previousLineStart(_ location: Int, in ns: NSString) -> Int { var p = location while p > 0 { - let prev = ns.lineRange(for: NSRange(location: p - 1, length: 0)) + let prev = line(around: NSRange(location: p - 1, length: 0), in: ns) p = prev.location if !ns.substring(with: prev).trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { break } } @@ -321,15 +321,29 @@ public enum CodeExtractor { private static func nextLineEnd(_ location: Int, in ns: NSString) -> Int { var p = location while p < ns.length { - let next = ns.lineRange(for: NSRange(location: p, length: 0)) + let next = line(around: NSRange(location: p, length: 0), in: ns) p = next.upperBound if !ns.substring(with: next).trimmingCharacters(in: .whitespacesAndNewlines).isEmpty { break } } return p } + /// The line around `range` as `lineRange(for:)` gives it, looking at most `reach` UTF-16 units each way: + /// a mail that is one very long line was read whole for every candidate, which froze detection. + private static func line(around range: NSRange, in ns: NSString, reach: Int = 1000) -> NSRange { + let from = max(0, range.location - reach) + let to = min(ns.length, range.upperBound + reach) + let before = ns.rangeOfCharacter(from: .newlines, options: .backwards, range: NSRange(location: from, length: range.location - from)) + let start = before.location == NSNotFound ? from : before.upperBound + let after = ns.rangeOfCharacter(from: .newlines, range: NSRange(location: range.upperBound, length: to - range.upperBound)) + guard after.location != NSNotFound else { return NSRange(location: start, length: to - start) } + // "\r\n" ends a line as one. + let crlf = ns.character(at: after.location) == 13 && after.upperBound < ns.length && ns.character(at: after.upperBound) == 10 + return NSRange(location: start, length: after.upperBound + (crlf ? 1 : 0) - start) + } + private static func aloneOnLine(_ range: NSRange, in ns: NSString) -> Bool { - let line = ns.substring(with: ns.lineRange(for: range)) + let line = ns.substring(with: line(around: range, in: ns)) let trimmed = line.trimmingCharacters(in: .whitespacesAndNewlines.union(.punctuationCharacters)) return trimmed == ns.substring(with: range) } diff --git a/Tests/CodeCatchCoreTests/CodeExtractorTests.swift b/Tests/CodeCatchCoreTests/CodeExtractorTests.swift index b9e187d..e5545eb 100644 --- a/Tests/CodeCatchCoreTests/CodeExtractorTests.swift +++ b/Tests/CodeCatchCoreTests/CodeExtractorTests.swift @@ -17,11 +17,11 @@ import Testing } /// Detection runs on the main thread: work that grew with the square of a mail's candidates froze -/// the app for about ten seconds on one long mail. -@Test func longMailCannotStallDetection() { +/// the app for about ten seconds on one long mail, and for close to a minute when that mail was one line. +@Test(arguments: ["\n", " "]) func longMailCannotStallDetection(separator: String) { // This thread's CPU time: other tests running beside this one can't stretch it. func cost(_ count: Int) -> UInt64 { - let text = String(repeating: "Your code: 123456\n", count: count) + let text = String(repeating: "Your code: 123456" + separator, count: count) let start = clock_gettime_nsec_np(CLOCK_THREAD_CPUTIME_ID) #expect(CodeExtractor.code(in: text) == "123456") return clock_gettime_nsec_np(CLOCK_THREAD_CPUTIME_ID) - start