| 1 | // Copied from Equipo's EquipoKit (oficina/equipo 00d09aa7, |
| 2 | // native/apple/Sources/EquipoKit/Markdown/MarkdownEntities.swift). Base's changes are |
| 3 | // marked "Base:"; keep the rest in step with Equipo's. |
| 4 | import Foundation |
| 5 | import cmark_gfm |
| 6 | |
| 7 | /// The web has distinct text and destination decoders. Text uses Goldmark's |
| 8 | /// numeric pass followed by its name pass; destinations decode proper HTML |
| 9 | /// entities once. Keeping these separate preserves nested escapes and queries. |
| 10 | enum MarkdownEntities { |
| 11 | private enum Mode { case link, numericText, namedText } |
| 12 | static func link(_ value: String) -> String { decode(value, mode: .link) } |
| 13 | static func text(_ value: String) -> String { |
| 14 | decode(decode(value, mode: .numericText), mode: .namedText) |
| 15 | } |
| 16 | private static func alphaNumeric(_ b: UInt8) -> Bool { (48...57).contains(b) || (65...90).contains(b) || (97...122).contains(b) } |
| 17 | private static func decode(_ value: String, mode: Mode) -> String { |
| 18 | let bytes = Array(value.utf8) |
| 19 | var out: [UInt8] = [], at = 0 |
| 20 | while at < bytes.count { |
| 21 | guard bytes[at] == 38 else { out.append(bytes[at]); at += 1; continue } |
| 22 | let begin = at |
| 23 | var end = at + 1 |
| 24 | let numeric = end < bytes.count && bytes[end] == 35 |
| 25 | if numeric { end += 1 } |
| 26 | while end < bytes.count, alphaNumeric(bytes[end]) { end += 1 } |
| 27 | guard end < bytes.count, bytes[end] == 59, end > at + (numeric ? 2 : 1) else { |
| 28 | out.append(bytes[at]); at += 1; continue |
| 29 | } |
| 30 | let entity = String(decoding: bytes[begin...end], as: UTF8.self) |
| 31 | var replacement: String? |
| 32 | if numeric, mode != .namedText { |
| 33 | var digits = Array(bytes[(begin + 2)..<end]) |
| 34 | let hex = digits.first == 120 || digits.first == 88 |
| 35 | if hex { digits.removeFirst() } |
| 36 | if !digits.isEmpty, digits.allSatisfy({ hex ? (48...57).contains($0) || (65...70).contains($0) || (97...102).contains($0) : (48...57).contains($0) }) { |
| 37 | replacement = number(digits, hex: hex, text: mode == .numericText) |
| 38 | } |
| 39 | } else if !numeric, mode != .numericText { |
| 40 | var buffer = cmark_strbuf() |
| 41 | cmark_strbuf_init(cmark_get_default_mem_allocator(), &buffer, 0) |
| 42 | let raw = Array(entity.utf8) |
| 43 | raw.withUnsafeBufferPointer { houdini_unescape_html_f(&buffer, $0.baseAddress, Int32($0.count)) } |
| 44 | replacement = String(decoding: UnsafeBufferPointer(start: buffer.ptr, count: Int(buffer.size)), as: UTF8.self) |
| 45 | cmark_strbuf_free(&buffer) |
| 46 | } |
| 47 | out.append(contentsOf: (replacement ?? entity).utf8) |
| 48 | at = end + 1 |
| 49 | } |
| 50 | return String(decoding: out, as: UTF8.self) |
| 51 | } |
| 52 | private static func number(_ digits: [UInt8], hex: Bool, text: Bool) -> String? { |
| 53 | if text, !hex, digits.count >= 8 { return nil } |
| 54 | // Goldmark's decimal text pass uses ParseUint(base: 0), so a leading |
| 55 | // zero selects octal. Its hexadecimal pass accepts any digit count. |
| 56 | let radix: UInt32 = hex ? 16 : text && digits.first == 48 ? 8 : 10 |
| 57 | var value: UInt32 = 0 |
| 58 | for byte in digits { |
| 59 | let digit = UInt32(byte <= 57 ? byte - 48 : (byte | 32) - 87) |
| 60 | if text { |
| 61 | if digit >= radix { value = 0; break } |
| 62 | let multiplied = value.multipliedReportingOverflow(by: radix) |
| 63 | let added = multiplied.partialValue.addingReportingOverflow(digit) |
| 64 | if multiplied.overflow || added.overflow { value = .max; break } |
| 65 | value = added.partialValue |
| 66 | } else { |
| 67 | // html.UnescapeString accumulates a signed 32-bit rune. |
| 68 | value = value &* radix &+ digit |
| 69 | } |
| 70 | } |
| 71 | if !text, (0x80...0x9f).contains(value) { |
| 72 | let windows: [UInt32] = [0x20ac,0x81,0x201a,0x192,0x201e,0x2026,0x2020,0x2021,0x2c6,0x2030,0x160,0x2039,0x152,0x8d,0x17d,0x8f,0x90,0x2018,0x2019,0x201c,0x201d,0x2022,0x2013,0x2014,0x2dc,0x2122,0x161,0x203a,0x153,0x9d,0x17e,0x178] |
| 73 | value = windows[Int(value - 0x80)] |
| 74 | } |
| 75 | guard value != 0, let scalar = Unicode.Scalar(value) else { return "\u{fffd}" } |
| 76 | return String(scalar) |
| 77 | } |
| 78 | } |
| 79 | |