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

String NSRange extension #499

Merged
merged 2 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,8 @@ public class TrackerResolver {
fileprivate extension KnownTracker.Rule {

func isMatchingUrl(_ urlString: String) -> Bool {
guard let pattern = rule, let regex = try? NSRegularExpression(pattern: pattern, options: []) else {
return false
}
let range = NSRange(location: 0, length: urlString.utf16.count)
return regex.firstMatch(in: urlString, options: [], range: range) != nil
guard let pattern = rule, let regex = try? NSRegularExpression(pattern: pattern, options: []) else { return false }
return regex.firstMatch(in: urlString, options: [], range: urlString.fullRange) != nil
}

func action(type: String, host: String) -> TrackerResolver.RuleAction? {
Expand Down
30 changes: 22 additions & 8 deletions Sources/Common/Extensions/StringExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,7 @@ public extension String {

static let localhost = "localhost"

func length() -> Int {
self.utf16.count
}

var fullRange: NSRange {
return NSRange(location: 0, length: length())
}
// MARK: Prefix/Suffix

func trimmingWhitespace() -> String {
return trimmingCharacters(in: .whitespacesAndNewlines)
Expand Down Expand Up @@ -93,6 +87,8 @@ public extension String {
return normalizedString
}

// MARK: Host name validation

var isValidHost: Bool {
return isValidHostname || isValidIpHost
}
Expand All @@ -108,6 +104,8 @@ public extension String {
return false
}

// MARK: Regex

func matches(_ regex: NSRegularExpression) -> Bool {
let matches = regex.matches(in: self, options: .anchored, range: self.fullRange)
return matches.count == 1
Expand All @@ -121,7 +119,7 @@ public extension String {
}

func replacing(_ regex: NSRegularExpression, with replacement: String) -> String {
regex.stringByReplacingMatches(in: self, range: NSRange(location: 0, length: utf16.count), withTemplate: replacement)
regex.stringByReplacingMatches(in: self, range: self.fullRange, withTemplate: replacement)
}

func replacing(regex pattern: String, with replacement: String) -> String {
Expand All @@ -132,6 +130,22 @@ public extension String {

public extension StringProtocol {

// MARK: NSRange

var fullRange: NSRange {
NSRange(startIndex..<endIndex, in: self)
}

func length() -> Int {
self.fullRange.length
}

subscript (_ range: NSRange) -> Self.SubSequence? {
Range(range, in: self).map { self[$0] }
}

// MARK: Percent encoding

// Replaces plus symbols in a string with the space character encoding
// Space UTF-8 encoding is 0x20
func encodingPlusesAsSpaces() -> String {
Expand Down