oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

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