index indexes and ranges of String


(Carlos García) #1

Hi,

I think this is important enough and useful for we to be in the core standard library

extension String {

    func index(of string: String, options: String.CompareOptions = .literal) -> String.Index? {
        return range(of: string, options: options)?.lowerBound
    }

    func indexes(of string: String, options: String.CompareOptions = .literal) -> [String.Index] {
        var result: [String.Index] = []
        var start = startIndex
        while let range = range(of: string, options: options, range: start..<endIndex) {
            result.append(range.lowerBound)
            start = range.upperBound
        }
        return result
    }

    func ranges(of string: String, options: String.CompareOptions = .literal) -> [Range<String.Index>] {
        var result: [Range<String.Index>] = []
        var start = startIndex
        while let range = range(of: string, options: options, range: start..<endIndex) {
            result.append(range)
            start = range.upperBound
        }
        return result
    }

}

Source is not mine, I’ve found here: http://stackoverflow.com/a/32306142/4550651

Best and happy 2017

Carlos García


(Tony Parker) #2

Hi Carlos,

Thanks for proposing this. Would you mind sending it out to swift-dev or swift-evolution as well? The authors of the Swift standard library are more likely to see your message there.

- Tony

···

On Dec 31, 2016, at 11:53 AM, Carlos García via swift-corelibs-dev <swift-corelibs-dev@swift.org> wrote:

Hi,

I think this is important enough and useful for we to be in the core standard library

extension String {

    func index(of string: String, options: String.CompareOptions = .literal) -> String.Index? {
        return range(of: string, options: options)?.lowerBound
    }

    func indexes(of string: String, options: String.CompareOptions = .literal) -> [String.Index] {
        var result: [String.Index] = []
        var start = startIndex
        while let range = range(of: string, options: options, range: start..<endIndex) {
            result.append(range.lowerBound)
            start = range.upperBound
        }
        return result
    }

    func ranges(of string: String, options: String.CompareOptions = .literal) -> [Range<String.Index>] {
        var result: [Range<String.Index>] = []
        var start = startIndex
        while let range = range(of: string, options: options, range: start..<endIndex) {
            result.append(range)
            start = range.upperBound
        }
        return result
    }

}

Source is not mine, I’ve found here: http://stackoverflow.com/a/32306142/4550651

Best and happy 2017

Carlos García

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


(Carlos García) #3

Thanks Tony

Done!

Carlos

···

On 04 Jan 2017, at 17:12, Tony Parker <anthony.parker@apple.com> wrote:

Hi Carlos,

Thanks for proposing this. Would you mind sending it out to swift-dev or swift-evolution as well? The authors of the Swift standard library are more likely to see your message there.

- Tony

On Dec 31, 2016, at 11:53 AM, Carlos García via swift-corelibs-dev <swift-corelibs-dev@swift.org <mailto:swift-corelibs-dev@swift.org>> wrote:

Hi,

I think this is important enough and useful for we to be in the core standard library

extension String {

    func index(of string: String, options: String.CompareOptions = .literal) -> String.Index? {
        return range(of: string, options: options)?.lowerBound
    }

    func indexes(of string: String, options: String.CompareOptions = .literal) -> [String.Index] {
        var result: [String.Index] = []
        var start = startIndex
        while let range = range(of: string, options: options, range: start..<endIndex) {
            result.append(range.lowerBound)
            start = range.upperBound
        }
        return result
    }

    func ranges(of string: String, options: String.CompareOptions = .literal) -> [Range<String.Index>] {
        var result: [Range<String.Index>] = []
        var start = startIndex
        while let range = range(of: string, options: options, range: start..<endIndex) {
            result.append(range)
            start = range.upperBound
        }
        return result
    }

}

Source is not mine, I’ve found here: http://stackoverflow.com/a/32306142/4550651

Best and happy 2017

Carlos García

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