1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- //
- // String+SwiftyMarkdown.swift
- // SwiftyMarkdown
- //
- // Created by Simon Fairbairn on 08/12/2019.
- // Copyright © 2019 Voyage Travel Apps. All rights reserved.
- //
- import Foundation
- /// Some helper functions based on this:
- /// https://stackoverflow.com/questions/32305891/index-of-a-substring-in-a-string-with-swift/32306142#32306142
- extension StringProtocol {
- func index<S: StringProtocol>(of string: S, options: String.CompareOptions = []) -> Index? {
- range(of: string, options: options)?.lowerBound
- }
- func endIndex<S: StringProtocol>(of string: S, options: String.CompareOptions = []) -> Index? {
- range(of: string, options: options)?.upperBound
- }
- func indices<S: StringProtocol>(of string: S, options: String.CompareOptions = []) -> [Index] {
- var indices: [Index] = []
- var startIndex = self.startIndex
- while startIndex < endIndex,
- let range = self[startIndex...]
- .range(of: string, options: options) {
- indices.append(range.lowerBound)
- startIndex = range.lowerBound < range.upperBound ? range.upperBound :
- index(range.lowerBound, offsetBy: 1, limitedBy: endIndex) ?? endIndex
- }
- return indices
- }
- func ranges<S: StringProtocol>(of string: S, options: String.CompareOptions = []) -> [Range<String.Index>] {
- var result: [Range<Index>] = []
- var startIndex = self.startIndex
- while startIndex < endIndex,
- let range = self[startIndex...]
- .range(of: string, options: options) {
- result.append(range)
- startIndex = range.lowerBound < range.upperBound ? range.upperBound :
- index(range.lowerBound, offsetBy: 1, limitedBy: endIndex) ?? endIndex
- }
- return result
- }
- }
|