Additional String Processing APIs

One thing that I feel is missing from strings in Swift is parsing.

A primary aspect of parsing that is currently clunky, difficult to use, and lacking in some regards is regular expressions. To start off, you have to import Foundation to be able to do anything with regular expressions in the first place. While there are currently a few methods that allow you to somewhat easily work with regular expressions by using .regularExpression option (replacingOccurrences(of:with:options:) and range(of:options:)), they aren't always sufficient and you often need to work with NSRegularExpression. This is were I largely take issue. I also got some discussion going regarding some of the issues with regular expressions here by the way. Anyways, NSRegularExpression is not a very swifty API and is often a pain to use. Working with capture groups and matches is also not very easy. I feel that a native Swift implementation that provides an API that leverages Swift's features would be quite helpful. Maybe even provide first-class regex support with a unique literal syntax with highlighting :man_shrugging:. @Michael_Ilseman came up with some ideas regarding how this could work in his State of String: ABI, Performance, Ergonomics, and You! doc. And even if it is not feasible to provide custom syntax and whatnot, I feel that a native Swift solution in some capacity is needed.

On a more general note though, I feel that we should look into what can be done to make parsing strings easer. I'm not sure how we can go about this, but, when trying to create a program that tokenized and parsed strings, I found that it got very complicated very quickly. I feel like there must be a better way, though I'm not really sure.

2 Likes