mirror of
https://github.com/Ranchero-Software/NetNewsWire
synced 2025-08-12 06:26:36 +00:00
52 lines
996 B
Swift
52 lines
996 B
Swift
//
|
|
// HTMLEntityDecoder.swift
|
|
//
|
|
//
|
|
// Created by Brent Simmons on 9/14/24.
|
|
//
|
|
|
|
import Foundation
|
|
|
|
public final class HTMLEntityDecoder {
|
|
|
|
static func decodedString(withEncodedString encodedString: String) -> String {
|
|
|
|
let scanner = Scanner(string: encodedString)
|
|
scanner.charactersToBeSkipped = nil
|
|
var result = ""
|
|
var didDecodeAtLeastOneEntity = false
|
|
|
|
while true {
|
|
|
|
var scannedString = nil
|
|
if scanner.scanUpToString("&" intoString: &scannedString) {
|
|
result.append(scannedString)
|
|
}
|
|
if scanner.isAtEnd {
|
|
break
|
|
}
|
|
|
|
let savedScanLocation = scanner.scanLocation
|
|
|
|
var decodedEntity: String? = nil
|
|
if scanner.scanEntityValue(&decodedEntity) {
|
|
result.append(decodedEntity)
|
|
didDecodeAtLeastOneEntity = true
|
|
}
|
|
else {
|
|
result.append("&")
|
|
scanner.scanLocation = savedScanLocation + 1
|
|
}
|
|
|
|
if scanner.isAtEnd {
|
|
break
|
|
}
|
|
}
|
|
|
|
if !didDecodeAtLeastOneEntity { // No changes made?
|
|
return encodedString
|
|
}
|
|
return result
|
|
}
|
|
}
|