SPB Git

Repository Public

HTMLEntities.swift 5.9 KB 137 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// PROJECT METADATA7// -----------------------------------------------------------------------------8// Project          : SP Search iOS9// File             : Core/Sources/SPSearchKit/HTMLEntities.swift10// Description      : Decoding of HTML character references found in titles and11//                    snippets.12// Module           : SPSearchKit13// Repository       : https://git.spboucher.ai/sp-search-ios14// Project Website  : https://search.spboucher.ai15// Created          : 2026-12-0116// Last Modified    : 2026-12-0117//18// =============================================================================19// AUTHOR & PROFESSIONAL AFFILIATION20// =============================================================================21//22// Author           : Simon-Pierre Boucher23// Position         : Professor of Finance24// Institution      : Université du Québec en Outaouais (UQO)25// Department       : Département des sciences administratives26// Organization     : SP Boucher27//28// Website          : https://spboucher.ai29// Git              : https://git.spboucher.ai30//31// =============================================================================32// OFFICIAL CONTACT DIRECTORY33// =============================================================================34//35// General          : info@spboucher.ai36// Professional     : contact@spboucher.ai37// Direct           : simon@spboucher.ai38// Research         : research@spboucher.ai39// Development      : dev@spboucher.ai40// Support          : support@spboucher.ai41// Security/Admin   : admin@spboucher.ai42// Notifications    : notifications@spboucher.ai43//44// Preferred Contact: support@spboucher.ai45//46// =============================================================================47// COPYRIGHT & INTELLECTUAL PROPERTY48// =============================================================================49//50// Copyright (c) 202651// Simon-Pierre Boucher52//53// All rights reserved where applicable.54//55// This file is governed by the applicable project license,56// copyright notices, institutional agreements and relevant law.57//58// Unless expressly authorized by the applicable license or59// rights holder, copying, redistribution, sublicensing,60// publication and commercial exploitation are not permitted.61//62// Existing third-party rights and notices remain unaffected.63//64// =============================================================================65// CONFIDENTIALITY & SECURITY66// =============================================================================67//68// Classification   : Public69// Distribution     : Public repository; use is governed by the project license70// Security Contact : admin@spboucher.ai71//72// Access to this source does not independently grant permission73// to redistribute or disclose confidential materials.74//75// Do not embed credentials, private keys, access tokens,76// certificates or confidential personal information.77//78// =============================================================================79// LICENSE & MAINTENANCE80// =============================================================================81//82// License          : None specified (no license file in this repository)83// Maintainer       : Simon-Pierre Boucher84// Technical Contact: dev@spboucher.ai85//86// =============================================================================87// END OF SP BOUCHER SOURCE HEADER88// =============================================================================8990import Foundation9192public enum HTMLEntities {93    private static let named: [String: String] = [94        "amp": "&", "lt": "<", "gt": ">", "quot": "\"", "apos": "'", "nbsp": "\u{00A0}",95        "laquo": "«", "raquo": "»", "hellip": "…", "ndash": "–", "mdash": "—", "lsquo": "‘", "rsquo": "’",96        "ldquo": "“", "rdquo": "”", "bull": "•", "middot": "·", "copy": "©", "reg": "®", "trade": "™", "deg": "°",97        "euro": "€", "pound": "£", "times": "×", "eacute": "é", "egrave": "è", "ecirc": "ê", "euml": "ë",98        "agrave": "à", "acirc": "â", "auml": "ä", "ccedil": "ç", "ugrave": "ù", "ucirc": "û", "uuml": "ü",99        "ocirc": "ô", "ouml": "ö", "icirc": "î", "iuml": "ï", "oelig": "œ", "aelig": "æ", "szlig": "ß", "ntilde": "ñ",100        "Eacute": "É", "Egrave": "È", "Ecirc": "Ê", "Agrave": "À", "Acirc": "Â", "Ccedil": "Ç", "Ocirc": "Ô",101        "Icirc": "Î", "Ucirc": "Û", "OElig": "Œ", "AElig": "Æ", "Auml": "Ä", "Ouml": "Ö", "Uuml": "Ü",102    ]103104    /// One pass: `&amp;#039;` gives `&#039;`, never an apostrophe. An unknown or malformed reference is left as written.105    public static func decode(_ s: String) -> String {106        guard s.contains("&") else { return s }107        var out = ""108        out.reserveCapacity(s.count)109        var i = s.startIndex110        while i < s.endIndex {111            let ch = s[i]112            if ch == "&", let semi = s[i...].prefix(12).firstIndex(of: ";") {113                let body = s[s.index(after: i)..<semi]114                if let r = resolve(body) {115                    out += r116                    i = s.index(after: semi)117                    continue118                }119            }120            out.append(ch)121            i = s.index(after: i)122        }123        return out124    }125126    private static func resolve(_ body: Substring) -> String? {127        if body.hasPrefix("#") {128            let digits = body.dropFirst()129            let code: UInt32?130            if let f = digits.first, f == "x" || f == "X" { code = UInt32(digits.dropFirst(), radix: 16) } else { code = UInt32(digits, radix: 10) }131            guard let c = code, c != 0, let scalar = Unicode.Scalar(c) else { return nil }132            return String(Character(scalar))133        }134        return named[String(body)]135    }136}137
Files main
Menu

Theme