Post

Replies

Boosts

Views

Activity

Reply to Swift Regex too slow als grep replacement?
Good catch. Did you also change the definition of the regex to remove the Capture { } ? Nope. I did just replace "firstMatch". Now I used a plain string and it is much faster. Also I use now another split which is way faster (20 vs. 50s). So here is the code let matchedLines = fullText.split(whereSeparator: \.isNewline) .filter{ $0.contains(yesterdayRegEx) } .filter{ $0.contains(botListRegEx) } Match time: 52.7 s let matchedLines = fullText.split(whereSeparator: \.isNewline) .filter{ $0.contains("26/Apr") } .filter{ $0.contains(botListRegEx) } Match time: 21.8 s So the slow part is now the Split which take most of the time (21 s).
Topic: Programming Languages SubTopic: Swift Tags:
Jun ’23
Reply to Swift Regex too slow als grep replacement?
There was another discussion on https://forums.swift.org/t/use-regexbuilder-als-grep-replacement/65782 where I was able to get much faster code. This code does now run in 3 seconds. let paths = ["regTest/logs/access.log", "/regTest/logs/access.log.1"] var startDate = Date() let matchedLines = try await withThrowingTaskGroup(of: [String].self, body: { group in for aPath in paths { group.addTask { let lines = FileHandle(forReadingAtPath: aPath)!.bytes.lines .filter { $0.contains("26/Apr") } .filter { $0.contains("\"GET ") } .filter{ !$0.contains(botListRegEx) } var matched : [String] = [] for try await line in lines { if let m = line.firstMatch(of: ipAtStartRegEx) { let (s, _) = m.output matched.append(String(s)) } } return matched } } var matchedLines : [String] = [] for try await partialMatchedLines in group { matchedLines.append(contentsOf: partialMatchedLines) } return matchedLines }) let uniqArray = Set(matchedLines) print("Match time: \(abs(startDate.timeIntervalSinceNow)) s") print("Found \(matchedLines.count) lines.") print("Found \(uniqArray.count) IP adresses.")
Topic: Programming Languages SubTopic: Swift Tags:
Jun ’23
Reply to Swift Regex too slow als grep replacement?
I note that none of your regular expressions actually need to be regular expressions, i.e. they are just fixed text strings. For example, where you have "non.sense", I think you only want it to literally match "non.sense", not "non0sense". Yes. The first two filters are fixed strings but the last one contains a lot of different search strings. Therefore RegEx makes sense. You're right that a simple string search may be faster but since it takes now just 3 seconds I'm fine with it. Thanks for sharing your thoughts on my code.
Topic: Programming Languages SubTopic: Swift Tags:
Jul ’23
Reply to SwiftUI alert buttons order issue
How about using no role at all? Without a role there is no reason for the OS to rearrange the buttons. Something like .alert( "Test alert", isPresented: $isAlertPresented, actions: { Button("Default role", action: { }) Button("Cancel role", role: .none, action: { isAlertPresented = false }) }, message: { Text("Test message") }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’23
Reply to "reset" users home dir to archived content
Meanwhile I got a solution I want to share here: It seems so that the OS has some "voodoo" to protect used home directories. So a change of users home dir via dscl . -change /Users/USER NFSHomeDirectory /Users/USER /doesNotExist allows removing the HOMEDIR and re-create it from a template. Afterwards the command dscl . -change /Users/USER NFSHomeDirectory /doesNotExist /Users/USER does set the homedir back to the old value.
Topic: App & System Services SubTopic: Core OS Tags:
Feb ’24
Reply to Print a SwiftUI view on macOS?
No idea. I've implemented a workaround by creating a PDF. This works well for me. Here is some sample code: func savePDF(docName : String?) { guard let saveURL = showSavePDFPanel(docName) else { return } var mediaBox = NSRect(origin: CGPoint(x: 0, y: 0), size: CGSize(width: 1100, height: 600)) if let dataConsumer = CGDataConsumer(url: saveURL as CFURL) { if let pdfContext = CGContext(consumer: dataConsumer, mediaBox: &mediaBox, nil) { let options: [CFString: Any] = [kCGPDFContextMediaBox: mediaBox] for sem in ItrSemester.allCases { pdfContext.beginPDFPage(options as CFDictionary) let renderer = ImageRenderer(content: MyView().environmentObject(appData)) renderer.render { size, renderFunction in pdfContext.translateBy(x: (mediaBox.width - size.width) / 2.0, y: (mediaBox.height - size.height) / 2.0) renderFunction(pdfContext) } /// Add a day header to each page with document name let titleString = "\(docName ?? "Ohne Titel")" let attrs : [NSAttributedString.Key : Any] = [.font: NSFont.boldSystemFont(ofSize: 16.0)] let day = NSAttributedString(string: titleString, attributes: attrs) let path = CGMutablePath() let strWidth = day.size().width + 1 let strHeight = day.size().height + 1 let dayXPos = 20.0 let dayYPos = mediaBox.height - strHeight - 32 path.addRect(CGRect(x: dayXPos, y: dayYPos, width: strWidth, height: strHeight)) let fSetter = CTFramesetterCreateWithAttributedString(day as CFAttributedString) let frame = CTFramesetterCreateFrame(fSetter, CFRangeMake(0, day.length), path, nil) CTFrameDraw(frame, pdfContext) pdfContext.endPDFPage() } pdfContext.closePDF() } } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Mar ’24