Six weeks ago there was a discussion [1] (and a subsequent fix [2]) about the behavior of `String.hasPrefix(_:)` and `String.hasSuffix(_:)` with empty string arguments. After the fix, `str.hasPrefix("")` always returns `true`.
This leads to inconsistent behavior with the `NSString.contains(_:)` method, which returns `false` for an empty string argument:
let str = "Hello"
str.contains("") // returns false
If `contains` were part of the stdlib like `hasPrefix` and `hasSuffix` are, I would consider this a bug. But `contains` comes from Foundation and I'm not sure how that changes things. Would it make sense to add a variant of `contains(_:)` to the stdlib and fix the behavior there?
I'm assuming it's too late for Swift 3 and it will be fixed with the String remodeling in Swift 4 anyway, but I just noticed this and I thought I'd ask.
Six weeks ago there was a discussion [1] (and a subsequent fix [2])
about the behavior of `String.hasPrefix(_:)` and
`String.hasSuffix(_:)` with empty string arguments. After the fix,
`str.hasPrefix("")` always returns `true`.
This leads to inconsistent behavior with the `NSString.contains(_:)`
method, which returns `false` for an empty string argument:
let str = "Hello"
str.contains("") // returns false
If `contains` were part of the stdlib like `hasPrefix` and `hasSuffix`
are, I would consider this a bug. But `contains` comes from Foundation
and I'm not sure how that changes things. Would it make sense to add a
variant of `contains(_:)` to the stdlib and fix the behavior there?
It might be worth doing something for Swift 3.1
I'm assuming it's too late for Swift 3 and it will be fixed with the
String remodeling in Swift 4 anyway,
That's my plan.
···
on Mon Sep 05 2016, Ole Begemann <swift-evolution@swift.org> wrote: