Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-9955] String.Index.init?<S: StringProtocol>(_ sourcePosition: Swift.String.Index, within target: S) is missing #52358

Closed
norio-nomura opened this issue Feb 19, 2019 · 5 comments
Assignees
Labels
feature A feature request or implementation standard library Area: Standard library umbrella String Area → standard library: The `String` type swift evolution implemented Flag → feature: A feature that was approved through the Swift evolution process and implemented

Comments

@norio-nomura
Copy link
Contributor

Previous ID SR-9955
Radar rdar://problem/32340799
Original Reporter @norio-nomura
Type New Feature
Status Resolved
Resolution Done
Environment

swift-5.0-DEVELOPMENT-SNAPSHOT-2019-02-17-a
swift-DEVELOPMENT-SNAPSHOT-2019-02-14-a

Additional Detail from JIRA
Votes 0
Component/s Standard Library
Labels New Feature
Assignee @milseman
Priority Medium

md5: 6ba3b0d9475f51dabd794e1b6e524822

Issue Description:

That makes it impossible to write the code below.

extension StringProtocol where Index == String.Index, UTF16View.Index == String.Index {
    func toUTF16Indices(_ range: NSRange) -> Range<Index>? {
        precondition(range.location != NSNotFound)
        guard let start = utf16.index(utf16.startIndex, offsetBy: range.lowerBound, limitedBy: utf16.endIndex),
            let end = utf16.index(start, offsetBy: range.length, limitedBy: utf16.endIndex),
            let lowerBound = String.Index(start, within: self),
            let upperBound = String.Index(end, within: self)
            else { return nil }
        return Range(uncheckedBounds: (lower: lowerBound, upper: upperBound))
    }
}
@belkadan
Copy link
Contributor

cc @milseman

@milseman
Copy link
Mannequin

milseman mannequin commented Feb 19, 2019

You filed this as a bug. Is this a regression in 5.0, or is this an API request? The above code emits the same errors for me in 4.2 as in 5.0.

If this is an API request, then I agree that we should make a generic initializer on String.Index over StringProtocol, but we should also make https://github.com/apple/swift/blob/master/stdlib/public/Darwin/Foundation/NSRange.swift#L178 generic, so that you don't even need to write this code.

@norio-nomura
Copy link
Contributor Author

Sorry, it should be filed as a new feature request that we should make a generic initializer on String.Index over StringProtocol.

so that you don't even need to write this code.

I know that API, but I also encountered to this issue on writing another similar implementation for UTF-8.

extension StringProtocol where Index == String.Index, UTF8View.Index == String.Index {
    func toUTF8Indices(_ range: UTF8Range) -> Range<Index>? {
        precondition(!range.isNotFound)
        guard let start = utf8.index(utf8.startIndex, offsetBy: range.lowerBound, limitedBy: utf8.endIndex),
            let end = utf8.index(start, offsetBy: range.utf8Length, limitedBy: utf8.endIndex),
            let lowerBound = String.Index(start, within: self),
            let upperBound = String.Index(end, within: self)
            else { return nil }
        return Range(uncheckedBounds: (lower: lowerBound, upper: upperBound))
    }
}

For writing above, it needs a generic initializer on String.Index over StringProtocol.

@milseman
Copy link
Mannequin

milseman mannequin commented Feb 27, 2019

@milseman
Copy link
Mannequin

milseman mannequin commented Apr 10, 2019

SE-0248

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@AnthonyLatsis AnthonyLatsis added feature A feature request or implementation String Area → standard library: The `String` type swift evolution implemented Flag → feature: A feature that was approved through the Swift evolution process and implemented and removed new feature labels Feb 24, 2023
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature A feature request or implementation standard library Area: Standard library umbrella String Area → standard library: The `String` type swift evolution implemented Flag → feature: A feature that was approved through the Swift evolution process and implemented
Projects
None yet
Development

No branches or pull requests

3 participants