Skip to content
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
22 changes: 18 additions & 4 deletions Sources/CodeCatchCore/CodeExtractor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down Expand Up @@ -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 }
}
Expand All @@ -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)
}
Expand Down
6 changes: 3 additions & 3 deletions Tests/CodeCatchCoreTests/CodeExtractorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading