[swift-evolution] Strings in Swift 4

Dave Abrahams dabrahams at apple.com
Mon Jan 23 23:05:59 CST 2017

Sent from my iPad

> On Jan 23, 2017, at 7:49 AM, Joshua Alvarado <alvaradojoshua0 at gmail.com> wrote:
> The strings proposal is a warm welcome to the Swift Language and I believe many developers are happy to see Strings become a priority. String processing may be one of the most common tasks for a developer day to day. That being said one thing in the proposal I believe is not correct is the leaving out regular expressions. 
>> "Addressing regular expressions is out of scope for this proposal." 

We shouldn't have said "this proposal," because it's a manifesto, not a proposal.  Addressing Regexps in Swift proper is out of scope for Swift 4, because there's simply no time to design and add the necessary language features.

> Working with regular expressions in both Objective-c and Swift is a real pain. I don't believe that because NSRegularExpression exists is a good enough reason to leave it out of Swift 4 string improvements. NSRegularExpression is NOT easily retrofitted to strings.

It should be easy to retrofit it to the pattern matching system so NSRegularExpression it works smoothly in Swift, but NSRegularExpression is outside the standard library so this is something to take up with the Foundation maintainers.

> Perl, ruby, javascript and many more programming languages have native easy to use regular expression functionality built in. 
> Examples:
> Ruby:
> /hey/ =~ 'hey what's up'
> /world/.match('hello world')
> Javascript:
> 'javascript regex'.search(/regex/)
> 'hello replace'.replace(/replace/, 'world')
> Perl
> $statement = "The quick brown fox";
> if ($statement = /quick/) {
>    print "this is what's up\n";
> }
> Now let's look at NSRegularExpression...
> Swift:
> do {
>     let pattern = "\\w" // escape everything
>     let regex = NSRegularExpression(pattern, options)
>     let results = regex.matches(in: str, options: .reportCompletion, range: NSRange(location: 0, length: str.characters.distance(from: str.startIndex, to: str.endIndex)))
>    results.forEach {
>       print($0) // why is this a NSTextCheckResult?!
>     }
> } catch {
>    // welp out of luck
> }
> Yes I'm fully aware of the method:
>     str.replaceOccurences(of: "pattern" with: "something" options: .regularExpression, range: nil) 
> but it is just not enough for what is needed. Also, it is confusing to have a replace regex method separate from NSRegularExpression. It was not easy to find. 
> Taken from NSHipster:
>> Happily, on one thing we can all agree. In NSRegularExpression, Cocoa has the most long-winded and byzantine regular expression interface you’re ever likely to come across.
> There is no way to achieve the goal of being better at string processing than Perl without regular expressions being addressed. It just should not be ignored. 

It's not being ignored.  We are laying the crucial foundation required for both Regexp support and more general pattern-matching.  We do, however, have to do some things later, and a full treatment of regexps is one such thing.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.swift.org/pipermail/swift-evolution/attachments/20170123/7c80b5e4/attachment.html>

More information about the swift-evolution mailing list