mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2025-08-12 06:26:36 +00:00
Move some extensions into SAX package.
This commit is contained in:
68
Modules/Parser/Sources/SAX/Extensions/Data+SAX.swift
Normal file
68
Modules/Parser/Sources/SAX/Extensions/Data+SAX.swift
Normal file
@@ -0,0 +1,68 @@
|
||||
//
|
||||
// Data+Parser.swift
|
||||
//
|
||||
//
|
||||
// Created by Brent Simmons on 8/24/24.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
extension Data {
|
||||
|
||||
/// Return true if the data contains a given String.
|
||||
///
|
||||
/// Assumes that the data is UTF-8 or similar encoding —
|
||||
/// if it’s 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
|
||||
}
|
||||
}
|
||||
28
Modules/Parser/Sources/SAX/Extensions/Dictionary+SAX.swift
Normal file
28
Modules/Parser/Sources/SAX/Extensions/Dictionary+SAX.swift
Normal file
@@ -0,0 +1,28 @@
|
||||
//
|
||||
// Dictionary+Parser.swift
|
||||
//
|
||||
//
|
||||
// Created by Brent Simmons on 8/18/24.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
extension Dictionary where Key == String, Value == String {
|
||||
|
||||
func object(forCaseInsensitiveKey key: String) -> String? {
|
||||
|
||||
if let object = self[key] {
|
||||
return object
|
||||
}
|
||||
|
||||
let lowercaseKey = key.lowercased()
|
||||
|
||||
for (oneKey, oneValue) in self {
|
||||
if lowercaseKey.caseInsensitiveCompare(oneKey) == .orderedSame {
|
||||
return oneValue
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
}
|
||||
23
Modules/Parser/Sources/SAX/Extensions/String+SAX.swift
Normal file
23
Modules/Parser/Sources/SAX/Extensions/String+SAX.swift
Normal file
@@ -0,0 +1,23 @@
|
||||
//
|
||||
// String+RSParser.swift
|
||||
// RSParser
|
||||
//
|
||||
// Created by Nate Weaver on 2020-01-19.
|
||||
// Copyright © 2020 Ranchero Software, LLC. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
extension String {
|
||||
|
||||
var nilIfEmptyOrWhitespace: String? {
|
||||
return self.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty ? nil : self
|
||||
}
|
||||
|
||||
static func isEmptyOrNil(_ s: String?) -> Bool {
|
||||
if let s {
|
||||
return s.isEmpty
|
||||
}
|
||||
return true
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user