mastodon-ios/MastodonSDK/Sources/MastodonUI/Vendor/TwitterTextEditor+String.swift

55 lines
1.6 KiB
Swift
Raw Normal View History

//
// String.swift
// Example
//
// Copyright 2021 Twitter, Inc.
// SPDX-License-Identifier: Apache-2.0
//
import Foundation
extension String {
@inlinable
2021-07-19 11:12:45 +02:00
public var length: Int {
(self as NSString).length
}
@inlinable
2021-07-19 11:12:45 +02:00
public func substring(with range: NSRange) -> String {
(self as NSString).substring(with: range)
}
2021-07-19 11:12:45 +02:00
public func substring(with result: NSTextCheckingResult, at index: Int) -> String? {
guard index < result.numberOfRanges else {
return nil
}
let range = result.range(at: index)
guard range.location != NSNotFound else {
return nil
}
return substring(with: result.range(at: index))
}
2021-07-19 11:12:45 +02:00
public func firstMatch(pattern: String,
options: NSRegularExpression.Options = [],
range: NSRange? = nil) -> NSTextCheckingResult?
{
guard let regularExpression = try? NSRegularExpression(pattern: pattern, options: options) else {
return nil
}
let range = range ?? NSRange(location: 0, length: length)
return regularExpression.firstMatch(in: self, options: [], range: range)
}
2021-07-19 11:12:45 +02:00
public func matches(pattern: String,
options: NSRegularExpression.Options = [],
range: NSRange? = nil) -> [NSTextCheckingResult]
{
guard let regularExpression = try? NSRegularExpression(pattern: pattern, options: options) else {
return []
}
let range = range ?? NSRange(location: 0, length: length)
return regularExpression.matches(in: self, options: [], range: range)
}
}