Str.range result changes in macOS 14.0 Sonoma

My best guess is that they might have made the Foundation regular expression routines call into the Swift ones. The Foundation regex routines probably considered the \r and \n to be separate characters but if they're calling into the Swift routines then the the \r\n would be treated as a single character (in line with Unicode rules and the normal behavior of \r\n in Swift strings).

You could probably replace the [\r\n] with (\r|\n) to get consistent behavior with both implementations. Or you could just switch the order and use [\n\r] as I think they'd always be interpreted as separate characters in that order.

But I'd be tempted to switch to a solution that accepts any of the possible line endings, rather than only the classic Mac (\r) and UNIX (\n) line endings. (handling Windows-style \r\n is usually a good idea.)

str.firstIndex {$0.isNewline} is what pops to mind.

Furthermore, if possible I'd avoid all of the old NSString methods, as there are a lot of pitfalls to be found there.

4 Likes