range(of:options:range:locale:) with partial ranges

The range(of:options:range:locale:) method of StringProtocol does not accept a partial range as search range. Example (Xcode 9, Swift 4):

    let str = "foo"
    let pos = str.startIndex
    let r = str.range(of: "f", range: pos...)
    // error: cannot convert value of type 'PartialRangeFrom<String.Index>'
    // to expected argument type 'Range<String.Index>?'

Is that because it is an NSString method imported to Swift? Would it make sense to file an enhancement request?

Regards, Martin

In order to be able to use pos… this function would need to be converted from it’s current signature (that takes a concrete Range) to be generic over any RangeExpression.In this case of this method, the range is optional – and in case of nil (which is the default) there would be insufficient context to determine the actual type of the RangeExpression. So we’d need to have an overload instead.

But none of this ought to be necessary, because constant time slicing and index sharing means we don’t need to have functions take optional range arguments any more. Instead you can just write str[pos...].range(of: “f”).

Except... this technique has a bug in it for this particular method when used on substrings, that we need to fix before that can be used instead. Hoping for that to be fixed on master soon...

···

On Sep 21, 2017, at 2:44 AM, Martin R via swift-users <swift-users@swift.org> wrote:

The range(of:options:range:locale:) method of StringProtocol does not accept a partial range as search range. Example (Xcode 9, Swift 4):

    let str = "foo"
    let pos = str.startIndex
    let r = str.range(of: "f", range: pos...)
    // error: cannot convert value of type 'PartialRangeFrom<String.Index>'
    // to expected argument type 'Range<String.Index>?'

Is that because it is an NSString method imported to Swift? Would it make sense to file an enhancement request?

Regards, Martin

_______________________________________________
swift-users mailing list
swift-users@swift.org
https://lists.swift.org/mailman/listinfo/swift-users

In order to be able to use pos… this function would need to be converted from it’s current signature (that takes a concrete Range) to be generic over any RangeExpression.In this case of this method, the range is optional – and in case of nil (which is the default) there would be insufficient context to determine the actual type of the RangeExpression. So we’d need to have an overload instead.

But none of this ought to be necessary, because constant time slicing and index sharing means we don’t need to have functions take optional range arguments any more. Instead you can just write str[pos...].range(of: “f”).

That makes sense.

Except... this technique has a bug in it for this particular method when used on substrings, that we need to fix before that can be used instead. Hoping for that to be fixed on master soon...

Good to know!

Regards, Martin

···

On 22. Sep 2017, at 23:55, Ben Cohen <ben_cohen@apple.com> wrote:

On Sep 21, 2017, at 2:44 AM, Martin R via swift-users <swift-users@swift.org <mailto:swift-users@swift.org>> wrote:

The range(of:options:range:locale:) method of StringProtocol does not accept a partial range as search range. Example (Xcode 9, Swift 4):

    let str = "foo"
    let pos = str.startIndex
    let r = str.range(of: "f", range: pos...)
    // error: cannot convert value of type 'PartialRangeFrom<String.Index>'
    // to expected argument type 'Range<String.Index>?'

Is that because it is an NSString method imported to Swift? Would it make sense to file an enhancement request?

Regards, Martin

_______________________________________________
swift-users mailing list
swift-users@swift.org <mailto:swift-users@swift.org>
https://lists.swift.org/mailman/listinfo/swift-users