NetNewsWire/Modules/Parser/Sources/SAX/Extensions/Data+SAX.swift
2024-08-26 22:39:46 -07:00

69 lines
1.4 KiB
Swift
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// Data+Parser.swift
//
//
// Created by Brent Simmons on 8/24/24.
//
import Foundation
public extension Data {
/// Return true if the data contains a given String.
///
/// Assumes that the data is UTF-8 or similar encoding
/// if its UTF-16 or UTF-32, for instance, this will always return false.
/// Luckily these are rare.
///
/// The String to search for should be something that could be encoded
/// in ASCII  like "<opml" or "<rss". (In other words,
/// the sequence of characters would always be the same in
/// commonly-used encodings.)
func containsASCIIString(_ searchFor: String) -> Bool {
contains(searchFor.utf8)
}
/// Return true if searchFor appears in self.
func contains(_ searchFor: Data) -> Bool {
let searchForCount = searchFor.count
let dataCount = self.count
guard searchForCount > 0, searchForCount <= dataCount else {
return false
}
let searchForInitialByte = searchFor[0]
var found = false
self.withUnsafeBytes { bytes in
let buffer = bytes.bindMemory(to: UInt8.self)
for i in 0...dataCount - searchForCount {
if buffer[i] == searchForInitialByte {
var match = true
for j in 1..<searchForCount {
if buffer[i + j] != searchFor[j] {
match = false
break
}
}
if match {
found = true
return
}
}
}
}
return found
}
}