Commit 9d407be245
Verified · cmc
Layout: unified · split
Sources/OrgCore/Parser/Inline.swift +108 −51
| @@ -1,22 +1,41 @@ | ||
| 1 | 1 | /// Characters allowed before an emphasis opener, besides whitespace and the start of the run. |
| 2 | private let emphasisPre: Set<Character> = ["-", "(", "{", "'", "\""] | |
| 2 | private let emphasisPre: Set<Unicode.Scalar> = ["-", "(", "{", "'", "\""] | |
| 3 | 3 | |
| 4 | 4 | /// Characters allowed after an emphasis closer, besides whitespace and the end of the run. |
| 5 | private let emphasisPost: Set<Character> = ["-", ".", ",", ":", "!", "?", ";", "'", "\"", ")", "}", "\\", "["] | |
| 6 | ||
| 7 | private let emphasisKinds: [Character: SyntaxKind] = [ | |
| 8 | "*": .bold, "/": .italic, "_": .underline, "+": .strikeThrough, "=": .verbatim, "~": .code, | |
| 9 | ] | |
| 5 | private let emphasisPost: Set<Unicode.Scalar> = ["-", ".", ",", ":", "!", "?", ";", "'", "\"", ")", "}", "\\", "["] | |
| 10 | 6 | |
| 11 | 7 | private let angleLinkSchemes: Set<String> = [ |
| 12 | 8 | "http", "https", "mailto", "file", "id", "doi", "ftp", "news", "shell", "elisp", "info", "help", "attachment", |
| 13 | 9 | ] |
| 14 | 10 | |
| 15 | private let plainLinkPrefixes = ["https://", "http://", "mailto:", "file:"] | |
| 11 | private let plainLinkPrefixes: [[Unicode.Scalar]] = ["https://", "http://", "mailto:", "file:"].map { Array($0.unicodeScalars) } | |
| 12 | ||
| 13 | private func emphasisKind(_ c: Unicode.Scalar) -> SyntaxKind? { | |
| 14 | switch c { | |
| 15 | case "*": .bold | |
| 16 | case "/": .italic | |
| 17 | case "_": .underline | |
| 18 | case "+": .strikeThrough | |
| 19 | case "=": .verbatim | |
| 20 | case "~": .code | |
| 21 | default: nil | |
| 22 | } | |
| 23 | } | |
| 24 | ||
| 25 | func isNewline(_ c: Unicode.Scalar) -> Bool { | |
| 26 | c == "\n" | |
| 27 | } | |
| 16 | 28 | |
| 17 | /// "\r\n" is a single Character, so both forms count. | |
| 18 | func isNewline(_ c: Character) -> Bool { | |
| 19 | c == "\n" || c == "\r\n" | |
| 29 | func isSpace(_ c: Unicode.Scalar) -> Bool { | |
| 30 | c.isASCII ? (c == " " || c == "\t" || c == "\n" || c == "\r" || c == "\u{0B}" || c == "\u{0C}") : c.properties.isWhitespace | |
| 31 | } | |
| 32 | ||
| 33 | func isWordScalar(_ c: Unicode.Scalar) -> Bool { | |
| 34 | if c.isASCII { | |
| 35 | let v = c.value | |
| 36 | return (v >= 48 && v <= 57) || (v >= 65 && v <= 90) || (v >= 97 && v <= 122) | |
| 37 | } | |
| 38 | return c.properties.isAlphabetic || c.properties.numericType != nil | |
| 20 | 39 | } |
| 21 | 40 | |
| 22 | 41 | enum InlineMatch { |
| @@ -33,16 +52,38 @@ enum InlineMatch { | ||
| 33 | 52 | } |
| 34 | 53 | } |
| 35 | 54 | |
| 36 | /// Turns a run of text into text, newline and object tokens. Every character ends up in | |
| 37 | /// exactly one token. | |
| 55 | /// Turns a run of text into text, newline and object tokens. Every scalar ends up in exactly | |
| 56 | /// one token. Works on unicode scalars: every recognizer starts on an ASCII character, so | |
| 57 | /// grapheme clusters never need to be formed. | |
| 38 | 58 | struct InlineScanner { |
| 39 | let chars: [Character] | |
| 59 | let chars: [Unicode.Scalar] | |
| 60 | /// Where each scalar starts in `source`, plus its end, so token text is sliced from the | |
| 61 | /// source instead of rebuilt scalar by scalar. | |
| 62 | let source: Substring.UnicodeScalarView | |
| 63 | let starts: [String.Index] | |
| 64 | ||
| 65 | init(_ text: Substring) { | |
| 66 | source = text.unicodeScalars | |
| 67 | var chars: [Unicode.Scalar] = [] | |
| 68 | var starts: [String.Index] = [] | |
| 69 | chars.reserveCapacity(text.utf8.count) | |
| 70 | starts.reserveCapacity(text.utf8.count + 1) | |
| 71 | var index = source.startIndex | |
| 72 | while index < source.endIndex { | |
| 73 | chars.append(source[index]) | |
| 74 | starts.append(index) | |
| 75 | index = source.index(after: index) | |
| 76 | } | |
| 77 | starts.append(source.endIndex) | |
| 78 | self.chars = chars | |
| 79 | self.starts = starts | |
| 80 | } | |
| 40 | 81 | |
| 41 | 82 | func scan(_ range: Range<Int>, into b: inout GreenBuilder, inLink: Bool = false) { |
| 42 | 83 | var textStart = range.lowerBound |
| 43 | 84 | var i = range.lowerBound |
| 44 | 85 | while i < range.upperBound { |
| 45 | if let match = match(at: i, in: range, inLink: inLink) { | |
| 86 | if Self.mayStartObject(chars[i]), let match = match(at: i, in: range, inLink: inLink) { | |
| 46 | 87 | emitText(textStart..<i, into: &b) |
| 47 | 88 | emit(match, into: &b, inLink: inLink) |
| 48 | 89 | i = match.end |
| @@ -54,12 +95,26 @@ struct InlineScanner { | ||
| 54 | 95 | emitText(textStart..<range.upperBound, into: &b) |
| 55 | 96 | } |
| 56 | 97 | |
| 98 | /// The first characters any recognizer accepts. | |
| 99 | static func mayStartObject(_ c: Unicode.Scalar) -> Bool { | |
| 100 | switch c { | |
| 101 | case "[", "<", "{", "\\", "^", "s", "h", "m", "f", "*", "/", "_", "+", "=", "~": true | |
| 102 | default: false | |
| 103 | } | |
| 104 | } | |
| 105 | ||
| 106 | /// Text with each line break as its own newline token; "\r\n" stays one token. | |
| 57 | 107 | func emitText(_ range: Range<Int>, into b: inout GreenBuilder) { |
| 58 | 108 | var start = range.lowerBound |
| 59 | for k in range where isNewline(chars[k]) { | |
| 60 | if start < k { b.token(.text, string(start..<k)) } | |
| 61 | b.token(.newline, String(chars[k])) | |
| 62 | start = k + 1 | |
| 109 | var k = range.lowerBound | |
| 110 | while k < range.upperBound { | |
| 111 | if chars[k] == "\n" { | |
| 112 | let breakStart = k > start && chars[k - 1] == "\r" ? k - 1 : k | |
| 113 | if start < breakStart { b.token(.text, string(start..<breakStart)) } | |
| 114 | b.token(.newline, string(breakStart..<(k + 1))) | |
| 115 | start = k + 1 | |
| 116 | } | |
| 117 | k += 1 | |
| 63 | 118 | } |
| 64 | 119 | if start < range.upperBound { b.token(.text, string(start..<range.upperBound)) } |
| 65 | 120 | } |
| @@ -98,24 +153,25 @@ struct InlineScanner { | ||
| 98 | 153 | } |
| 99 | 154 | |
| 100 | 155 | func string(_ range: Range<Int>) -> String { |
| 101 | String(chars[range]) | |
| 156 | String(source[starts[range.lowerBound]..<starts[range.upperBound]]) | |
| 102 | 157 | } |
| 103 | 158 | |
| 104 | func hasPrefix(_ s: String, at i: Int, _ limit: Int) -> Bool { | |
| 105 | var j = i | |
| 106 | for c in s { | |
| 107 | guard j < limit, chars[j] == c else { return false } | |
| 108 | j += 1 | |
| 109 | } | |
| 159 | func hasPrefix(_ s: [Unicode.Scalar], at i: Int, _ limit: Int) -> Bool { | |
| 160 | guard i + s.count <= limit else { return false } | |
| 161 | for (offset, c) in s.enumerated() where chars[i + offset] != c { return false } | |
| 110 | 162 | return true |
| 111 | 163 | } |
| 112 | 164 | |
| 165 | func hasPrefix(_ s: String, at i: Int, _ limit: Int) -> Bool { | |
| 166 | hasPrefix(Array(s.unicodeScalars), at: i, limit) | |
| 167 | } | |
| 168 | ||
| 113 | 169 | // MARK: - Recognizers |
| 114 | 170 | |
| 115 | 171 | func match(at i: Int, in range: Range<Int>, inLink: Bool) -> InlineMatch? { |
| 116 | 172 | let limit = range.upperBound |
| 117 | let previous: Character? = i > range.lowerBound ? chars[i - 1] : nil | |
| 118 | let afterWord = previous.map { $0.isLetter || $0.isNumber } ?? false | |
| 173 | let previous: Unicode.Scalar? = i > range.lowerBound ? chars[i - 1] : nil | |
| 174 | let afterWord = previous.map(isWordScalar) ?? false | |
| 119 | 175 | |
| 120 | 176 | switch chars[i] { |
| 121 | 177 | case "[": |
| @@ -136,14 +192,14 @@ struct InlineScanner { | ||
| 136 | 192 | if afterWord, let end = superscript(i, limit) { return .object(.superscript, i..<end) } |
| 137 | 193 | case "s": |
| 138 | 194 | if !afterWord, let end = inlineSourceBlock(i, limit) { return .object(.inlineSourceBlock, i..<end) } |
| 195 | case "h", "m", "f": | |
| 196 | if !inLink, !afterWord, let end = plainLink(i, limit) { return .object(.link, i..<end) } | |
| 139 | 197 | default: |
| 140 | 198 | break |
| 141 | 199 | } |
| 142 | 200 | |
| 143 | if !inLink, !afterWord, let end = plainLink(i, limit) { return .object(.link, i..<end) } | |
| 144 | ||
| 145 | if let kind = emphasisKinds[chars[i]], | |
| 146 | previous.map({ $0.isWhitespace || emphasisPre.contains($0) }) ?? true, | |
| 201 | if let kind = emphasisKind(chars[i]), | |
| 202 | previous.map({ isSpace($0) || emphasisPre.contains($0) }) ?? true, | |
| 147 | 203 | let close = emphasisClose(i, limit) { |
| 148 | 204 | return .emphasis(kind, open: i, close: close) |
| 149 | 205 | } |
| @@ -154,15 +210,15 @@ struct InlineScanner { | ||
| 154 | 210 | /// one line break, and the closer is followed by whitespace, punctuation or the end. |
| 155 | 211 | func emphasisClose(_ i: Int, _ limit: Int) -> Int? { |
| 156 | 212 | let marker = chars[i] |
| 157 | guard i + 1 < limit, !chars[i + 1].isWhitespace else { return nil } | |
| 213 | guard i + 1 < limit, !isSpace(chars[i + 1]) else { return nil } | |
| 158 | 214 | var newlines = 0 |
| 159 | 215 | var j = i + 1 |
| 160 | 216 | while j < limit { |
| 161 | 217 | if isNewline(chars[j]) { |
| 162 | 218 | newlines += 1 |
| 163 | 219 | if newlines > 1 { return nil } |
| 164 | } else if chars[j] == marker, j > i + 1, !chars[j - 1].isWhitespace { | |
| 165 | if j + 1 == limit || chars[j + 1].isWhitespace || emphasisPost.contains(chars[j + 1]) { return j } | |
| 220 | } else if chars[j] == marker, j > i + 1, !isSpace(chars[j - 1]) { | |
| 221 | if j + 1 == limit || isSpace(chars[j + 1]) || emphasisPost.contains(chars[j + 1]) { return j } | |
| 166 | 222 | } |
| 167 | 223 | j += 1 |
| 168 | 224 | } |
| @@ -202,7 +258,7 @@ struct InlineScanner { | ||
| 202 | 258 | func footnoteReference(_ i: Int, _ limit: Int) -> Int? { |
| 203 | 259 | guard hasPrefix("[fn:", at: i, limit) else { return nil } |
| 204 | 260 | var j = i + 4 |
| 205 | while j < limit, chars[j].isLetter || chars[j].isNumber || chars[j] == "_" || chars[j] == "-" { j += 1 } | |
| 261 | while j < limit, isWordScalar(chars[j]) || chars[j] == "_" || chars[j] == "-" { j += 1 } | |
| 206 | 262 | guard j < limit else { return nil } |
| 207 | 263 | if chars[j] == "]" { return j > i + 4 ? j + 1 : nil } |
| 208 | 264 | guard chars[j] == ":" else { return nil } |
| @@ -223,13 +279,13 @@ struct InlineScanner { | ||
| 223 | 279 | /// `[1/3]`, `[/]`, `[50%]` or `[%]`. |
| 224 | 280 | func statisticsCookie(_ i: Int, _ limit: Int) -> Int? { |
| 225 | 281 | var j = i + 1 |
| 226 | while j < limit, chars[j].isASCII, chars[j].isNumber { j += 1 } | |
| 282 | while j < limit, isASCIIDigit(chars[j]) { j += 1 } | |
| 227 | 283 | guard j < limit else { return nil } |
| 228 | 284 | if chars[j] == "%" { |
| 229 | 285 | j += 1 |
| 230 | 286 | } else if chars[j] == "/" { |
| 231 | 287 | j += 1 |
| 232 | while j < limit, chars[j].isASCII, chars[j].isNumber { j += 1 } | |
| 288 | while j < limit, isASCIIDigit(chars[j]) { j += 1 } | |
| 233 | 289 | } else { |
| 234 | 290 | return nil |
| 235 | 291 | } |
| @@ -247,14 +303,14 @@ struct InlineScanner { | ||
| 247 | 303 | j += 1 |
| 248 | 304 | } |
| 249 | 305 | guard j > start, j + 1 < limit, chars[j + 1] == ">", |
| 250 | !chars[start].isWhitespace, !chars[j - 1].isWhitespace else { return nil } | |
| 306 | !isSpace(chars[start]), !isSpace(chars[j - 1]) else { return nil } | |
| 251 | 307 | return j + 2 |
| 252 | 308 | } |
| 253 | 309 | |
| 254 | 310 | /// `<scheme:path>` for a known scheme. |
| 255 | 311 | func angleLink(_ i: Int, _ limit: Int) -> Int? { |
| 256 | 312 | var j = i + 1 |
| 257 | while j < limit, chars[j].isLetter { j += 1 } | |
| 313 | while j < limit, chars[j].properties.isAlphabetic { j += 1 } | |
| 258 | 314 | guard j < limit, chars[j] == ":", angleLinkSchemes.contains(string((i + 1)..<j).lowercased()) else { return nil } |
| 259 | 315 | j += 1 |
| 260 | 316 | let bodyStart = j |
| @@ -268,12 +324,11 @@ struct InlineScanner { | ||
| 268 | 324 | |
| 269 | 325 | /// A bare URL. Trailing sentence punctuation stays outside the link. |
| 270 | 326 | func plainLink(_ i: Int, _ limit: Int) -> Int? { |
| 271 | guard "hmf".contains(chars[i]), | |
| 272 | let prefix = plainLinkPrefixes.first(where: { hasPrefix($0, at: i, limit) }) else { return nil } | |
| 327 | guard let prefix = plainLinkPrefixes.first(where: { hasPrefix($0, at: i, limit) }) else { return nil } | |
| 273 | 328 | let bodyStart = i + prefix.count |
| 274 | 329 | var j = bodyStart |
| 275 | while j < limit, !chars[j].isWhitespace, !"()<>[]\"".contains(chars[j]) { j += 1 } | |
| 276 | while j > bodyStart, ".,;:!?'".contains(chars[j - 1]) { j -= 1 } | |
| 330 | while j < limit, !isSpace(chars[j]), !"()<>[]\"".unicodeScalars.contains(chars[j]) { j += 1 } | |
| 331 | while j > bodyStart, ".,;:!?'".unicodeScalars.contains(chars[j - 1]) { j -= 1 } | |
| 277 | 332 | return j > bodyStart ? j : nil |
| 278 | 333 | } |
| 279 | 334 | |
| @@ -281,8 +336,8 @@ struct InlineScanner { | ||
| 281 | 336 | func macro(_ i: Int, _ limit: Int) -> Int? { |
| 282 | 337 | guard hasPrefix("{{{", at: i, limit) else { return nil } |
| 283 | 338 | var j = i + 3 |
| 284 | guard j < limit, chars[j].isLetter else { return nil } | |
| 285 | while j < limit, chars[j].isLetter || chars[j].isNumber || chars[j] == "-" || chars[j] == "_" { j += 1 } | |
| 339 | guard j < limit, chars[j].properties.isAlphabetic else { return nil } | |
| 340 | while j < limit, isWordScalar(chars[j]) || chars[j] == "-" || chars[j] == "_" { j += 1 } | |
| 286 | 341 | if j < limit, chars[j] == "(" { |
| 287 | 342 | var k = j + 1 |
| 288 | 343 | while k < limit, !hasPrefix(")}}}", at: k, limit) { |
| @@ -294,11 +349,12 @@ struct InlineScanner { | ||
| 294 | 349 | return hasPrefix("}}}", at: j, limit) ? j + 3 : nil |
| 295 | 350 | } |
| 296 | 351 | |
| 297 | /// `\\` at the end of a line, before optional trailing blanks. The newline stays outside. | |
| 352 | /// `\\` at the end of a line, before optional trailing blanks. The line break stays outside. | |
| 298 | 353 | func lineBreak(_ i: Int, _ limit: Int) -> Int? { |
| 299 | 354 | guard hasPrefix("\\\\", at: i, limit) else { return nil } |
| 300 | 355 | var j = i + 2 |
| 301 | 356 | while j < limit, chars[j] == " " || chars[j] == "\t" { j += 1 } |
| 357 | if j + 1 < limit, chars[j] == "\r", chars[j + 1] == "\n" { return j } | |
| 302 | 358 | guard j == limit || isNewline(chars[j]) else { return nil } |
| 303 | 359 | return j |
| 304 | 360 | } |
| @@ -312,9 +368,10 @@ struct InlineScanner { | ||
| 312 | 368 | case "[": closer = "\\]" |
| 313 | 369 | default: return nil |
| 314 | 370 | } |
| 371 | let closing = Array(closer.unicodeScalars) | |
| 315 | 372 | var j = i + 2 |
| 316 | 373 | while j < limit { |
| 317 | if hasPrefix(closer, at: j, limit) { return j + 2 } | |
| 374 | if hasPrefix(closing, at: j, limit) { return j + 2 } | |
| 318 | 375 | j += 1 |
| 319 | 376 | } |
| 320 | 377 | return nil |
| @@ -333,7 +390,7 @@ struct InlineScanner { | ||
| 333 | 390 | return j < limit ? j + 1 : nil |
| 334 | 391 | } |
| 335 | 392 | let start = j |
| 336 | while j < limit, chars[j].isLetter || chars[j].isNumber { j += 1 } | |
| 393 | while j < limit, isWordScalar(chars[j]) { j += 1 } | |
| 337 | 394 | return j > start ? j : nil |
| 338 | 395 | } |
| 339 | 396 | |
| @@ -342,7 +399,7 @@ struct InlineScanner { | ||
| 342 | 399 | guard hasPrefix("src_", at: i, limit) else { return nil } |
| 343 | 400 | var j = i + 4 |
| 344 | 401 | let languageStart = j |
| 345 | while j < limit, !chars[j].isWhitespace, chars[j] != "[", chars[j] != "{" { j += 1 } | |
| 402 | while j < limit, !isSpace(chars[j]), chars[j] != "[", chars[j] != "{" { j += 1 } | |
| 346 | 403 | guard j > languageStart, j < limit else { return nil } |
| 347 | 404 | if chars[j] == "[" { |
| 348 | 405 | while j < limit, chars[j] != "]" { |
| @@ -370,7 +427,7 @@ struct InlineScanner { | ||
| 370 | 427 | |
| 371 | 428 | extension Parser { |
| 372 | 429 | mutating func inline(_ text: Substring) { |
| 373 | let chars = Array(text) | |
| 374 | InlineScanner(chars: chars).scan(0..<chars.count, into: &builder) | |
| 430 | let scanner = InlineScanner(text) | |
| 431 | scanner.scan(0..<scanner.chars.count, into: &builder) | |
| 375 | 432 | } |
| 376 | 433 | } |
Sources/OrgCore/Timestamp.swift +19 −9
| @@ -42,14 +42,14 @@ public struct Timestamp: Sendable, Equatable { | ||
| 42 | 42 | |
| 43 | 43 | /// Parses exactly one timestamp or range, with nothing before or after it. |
| 44 | 44 | public static func parse(_ text: some StringProtocol) -> Timestamp? { |
| 45 | let chars = Array(text) | |
| 45 | let chars = Array(text.unicodeScalars) | |
| 46 | 46 | guard let result = scanTimestamp(chars, at: 0, limit: chars.count), result.end == chars.count else { return nil } |
| 47 | 47 | return result.stamp |
| 48 | 48 | } |
| 49 | 49 | } |
| 50 | 50 | |
| 51 | 51 | /// A timestamp or `--` range starting at `start`, and the index after it. |
| 52 | func scanTimestamp(_ chars: [Character], at start: Int, limit: Int) -> (stamp: Timestamp, end: Int)? { | |
| 52 | func scanTimestamp(_ chars: [Unicode.Scalar], at start: Int, limit: Int) -> (stamp: Timestamp, end: Int)? { | |
| 53 | 53 | guard let first = scanSingleTimestamp(chars, at: start, limit: limit) else { return nil } |
| 54 | 54 | if first.stamp.end == nil, first.end + 2 < limit, chars[first.end] == "-", chars[first.end + 1] == "-", |
| 55 | 55 | let second = scanSingleTimestamp(chars, at: first.end + 2, limit: limit), |
| @@ -62,22 +62,23 @@ func scanTimestamp(_ chars: [Character], at start: Int, limit: Int) -> (stamp: T | ||
| 62 | 62 | } |
| 63 | 63 | |
| 64 | 64 | /// `<YYYY-MM-DD DAY HH:MM-HH:MM REPEATER WARNING>`, or the same in `[...]` for inactive. |
| 65 | private func scanSingleTimestamp(_ chars: [Character], at start: Int, limit: Int) -> (stamp: Timestamp, end: Int)? { | |
| 65 | private func scanSingleTimestamp(_ chars: [Unicode.Scalar], at start: Int, limit: Int) -> (stamp: Timestamp, end: Int)? { | |
| 66 | 66 | guard start < limit, chars[start] == "<" || chars[start] == "[" else { return nil } |
| 67 | 67 | let active = chars[start] == "<" |
| 68 | let close: Character = active ? ">" : "]" | |
| 68 | let close: Unicode.Scalar = active ? ">" : "]" | |
| 69 | 69 | var j = start + 1 |
| 70 | 70 | |
| 71 | 71 | func number(_ minDigits: Int, _ maxDigits: Int) -> Int? { |
| 72 | 72 | var k = j |
| 73 | while k < limit, k - j < maxDigits, chars[k].isASCII, chars[k].isNumber { k += 1 } | |
| 73 | while k < limit, k - j < maxDigits, isASCIIDigit(chars[k]) { k += 1 } | |
| 74 | 74 | guard k - j >= minDigits else { return nil } |
| 75 | let value = Int(String(chars[j..<k]))! | |
| 75 | var value = 0 | |
| 76 | for digit in chars[j..<k] { value = value * 10 + Int(digit.value - 48) } | |
| 76 | 77 | j = k |
| 77 | 78 | return value |
| 78 | 79 | } |
| 79 | 80 | |
| 80 | func take(_ c: Character) -> Bool { | |
| 81 | func take(_ c: Unicode.Scalar) -> Bool { | |
| 81 | 82 | guard j < limit, chars[j] == c else { return false } |
| 82 | 83 | j += 1 |
| 83 | 84 | return true |
| @@ -85,7 +86,7 @@ private func scanSingleTimestamp(_ chars: [Character], at start: Int, limit: Int | ||
| 85 | 86 | |
| 86 | 87 | func interval() -> Timestamp.Interval? { |
| 87 | 88 | let before = j |
| 88 | guard let value = number(1, 9), j < limit, let unit = Timestamp.Unit(rawValue: chars[j]) else { | |
| 89 | guard let value = number(1, 9), j < limit, let unit = Timestamp.Unit(rawValue: Character(chars[j])) else { | |
| 89 | 90 | j = before |
| 90 | 91 | return nil |
| 91 | 92 | } |
| @@ -145,7 +146,7 @@ private func scanSingleTimestamp(_ chars: [Character], at start: Int, limit: Int | ||
| 145 | 146 | // Day name: anything but digits, whitespace, `+`, `-`, `]` and `>`, in any language. |
| 146 | 147 | if j < limit, chars[j] == " " { |
| 147 | 148 | var k = j + 1 |
| 148 | while k < limit, !(chars[k].isNumber || chars[k].isWhitespace || "+-]>".contains(chars[k])) { k += 1 } | |
| 149 | while k < limit, !(isDigit(chars[k]) || chars[k].properties.isWhitespace || "+-]>".unicodeScalars.contains(chars[k])) { k += 1 } | |
| 149 | 150 | if k > j + 1 { j = k } |
| 150 | 151 | } |
| 151 | 152 | |
| @@ -179,3 +180,12 @@ private func scanSingleTimestamp(_ chars: [Character], at start: Int, limit: Int | ||
| 179 | 180 | guard take(close) else { return nil } |
| 180 | 181 | return (stamp, j) |
| 181 | 182 | } |
| 183 | ||
| 184 | func isASCIIDigit(_ c: Unicode.Scalar) -> Bool { | |
| 185 | c.value >= 48 && c.value <= 57 | |
| 186 | } | |
| 187 | ||
| 188 | /// Any numeric character, as `Character.isNumber` would see it in a day name. | |
| 189 | func isDigit(_ c: Unicode.Scalar) -> Bool { | |
| 190 | c.properties.numericType != nil | |
| 191 | } | |