From fa53cee975da65da1d0f9eb14a7afde2494555ee Mon Sep 17 00:00:00 2001 From: Blake Latchford Date: Thu, 19 Nov 2026 21:31:32 -0500 Subject: [PATCH 1/2] fix: Expand MECP person count matches per MECPv1.4.0 Second commit in a series for issue #494 Co-Authored-By: Claude Opus 5.5 --- .../Sources/MC1Services/MECP/MECPDecode.swift | 48 ++++++++++++------- .../MC1ServicesTests/MECPDecodeTests.swift | 44 +++++++++++++++++ 2 files changed, 76 insertions(+), 16 deletions(-) diff --git a/MC1Services/Sources/MC1Services/MECP/MECPDecode.swift b/MC1Services/Sources/MC1Services/MECP/MECPDecode.swift index 8080a6921d..6b95875437 100644 --- a/MC1Services/Sources/MC1Services/MECP/MECPDecode.swift +++ b/MC1Services/Sources/MC1Services/MECP/MECPDecode.swift @@ -63,29 +63,45 @@ public enum MECPDecode { private static let maxHour = 23 private static let maxMinute = 59 private static let clockDigits = 4 - private static let paxSuffix = "pax" private static func isASCIIDigit(_ character: Character) -> Bool { character.isASCII && character.isNumber } + /// MECP 1.4.0 §6.1 person count decoding pattern, without the boundaries `isBounded` checks. + private nonisolated(unsafe) static let paxPattern = #/0*([1-9][0-9]*) ?[Pp][Aa][Xx]/# + .matchingSemantics(.unicodeScalar) + + /// The first bounded person count. A zero count does not match, so a later value is used. Checking the + /// boundaries after matching finds the same value as the wrapped pattern, because the only boundary a + /// match can contain is the space before `pax`, so no bounded match can start inside a rejected one. private static func firstPax(in text: String) -> (count: Int, range: Range)? { - var index = text.startIndex - while index < text.endIndex { - guard isASCIIDigit(text[index]) else { - index = text.index(after: index) - continue - } - let start = index - while index < text.endIndex, isASCIIDigit(text[index]) { - index = text.index(after: index) - } - guard text[index...].hasPrefix(paxSuffix) else { continue } - guard let count = Int(text[start.. = { + let listed = "\t\n\u{0C}\r ,.;:!?'\"()[]{}" + + "\u{00A0}\u{1680}\u{202F}\u{205F}\u{3000}" + + "\u{060C}\u{061B}\u{061F}\u{06D4}" + + "\u{00AB}\u{00BB}\u{2013}\u{2014}\u{2026}" + + "\u{3001}\u{3002}\u{3010}\u{3011}" + + "\u{FF01}\u{FF08}\u{FF09}\u{FF0C}\u{FF0E}\u{FF1A}\u{FF1B}\u{FF1F}" + let ranges: [ClosedRange] = [0x2000...0x200A, 0x2018...0x201D, 0x300C...0x300F] + return Set(listed.unicodeScalars).union(ranges.joined().compactMap(Unicode.Scalar.init)) + }() + + /// MECP 1.4.0 §6.1 rule 4: a decoding match needs the start of the free text or one B scalar before + /// it, and one B scalar or the end of the free text after it. + private static func isBounded(_ range: Range, in text: String) -> Bool { + let scalars = text.unicodeScalars + let before = range.lowerBound == scalars.startIndex || boundary.contains(scalars[scalars.index(before: range.lowerBound)]) + let after = range.upperBound == scalars.endIndex || boundary.contains(scalars[range.upperBound]) + return before && after } private static func firstTimestamp(in text: String) -> (digits: String, range: Range)? { diff --git a/MC1Services/Tests/MC1ServicesTests/MECPDecodeTests.swift b/MC1Services/Tests/MC1ServicesTests/MECPDecodeTests.swift index 167d209090..d5a17bfdf0 100644 --- a/MC1Services/Tests/MC1ServicesTests/MECPDecodeTests.swift +++ b/MC1Services/Tests/MC1ServicesTests/MECPDecodeTests.swift @@ -101,6 +101,50 @@ struct MECPDecodeTests { #expect(kept.displayTail == "near") } + @Test + func `a person count must stand between boundaries`() { + for tail in ["x4pax", "3paxmore", "#4pax", "*4pax*", "4pax\u{0B}water", "0pax"] { + #expect(MECPDecode.decode("MECP/1/S01 \(tail)")?.personCount == nil, "\(tail)") + } + } + + @Test + func `a person count accepts a space, any case, leading zeros, and boundary punctuation`() { + for tail in ["4 pax", "4PAX", "4Pax.", "04pax", "(4pax)", "4pax\u{A0}water", "4pax\u{2009}water", "\u{201C}4pax\u{201D}", "need,4pax"] { + #expect(MECPDecode.decode("MECP/1/S01 \(tail)")?.personCount == 4, "\(tail)") + } + } + + @Test + func `a zero person count is skipped for a later one`() throws { + let message = try #require(MECPDecode.decode("MECP/1/S01 0pax 2pax")) + #expect(message.personCount == 2) + #expect(message.displayTail == "0pax") + } + + @Test + func `a person count without boundaries is skipped for a later one`() throws { + let before = try #require(MECPDecode.decode("MECP/1/S01 x4pax 2pax")) + #expect(before.personCount == 2) + #expect(before.displayTail == "x4pax") + + let after = try #require(MECPDecode.decode("MECP/1/S01 4paxy 2pax")) + #expect(after.personCount == 2) + #expect(after.displayTail == "4paxy") + } + + @Test + func `a spaced person count is removed from the tail`() throws { + let message = try #require(MECPDecode.decode("MECP/1/S01 4 pax water")) + #expect(message.personCount == 4) + #expect(message.displayTail == "water") + } + + @Test + func `a person count too large for Int is clamped`() { + #expect(MECPDecode.decode("MECP/1/S01 99999999999999999999pax")?.personCount == Int.max) + } + @Test func `a timestamp must be four digits at a token boundary and in range`() throws { let punctuated = try #require(MECPDecode.decode("MECP/1/M01 arrived @1430.")) From 0313f25537310e02ef3acfc5cf5dc1aa6a1f5a12 Mon Sep 17 00:00:00 2001 From: Blake Latchford Date: Fri, 20 Nov 2026 11:56:18 -0500 Subject: [PATCH 2/2] Fix crash on --- .../Sources/MC1Services/MECP/MECPDecode.swift | 11 +++++---- .../MC1ServicesTests/MECPDecodeTests.swift | 23 +++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/MC1Services/Sources/MC1Services/MECP/MECPDecode.swift b/MC1Services/Sources/MC1Services/MECP/MECPDecode.swift index 6b95875437..8329eebd60 100644 --- a/MC1Services/Sources/MC1Services/MECP/MECPDecode.swift +++ b/MC1Services/Sources/MC1Services/MECP/MECPDecode.swift @@ -40,9 +40,10 @@ public enum MECPDecode { var removal: [Range] = [] var extraction = Extraction() - if let match = firstPax(in: freetext) { - extraction.personCount = match.count - removal.append(match.range) + let pax = firstPax(in: freetext) + if let pax { + extraction.personCount = pax.count + removal.append(pax.range) } if let match = firstTimestamp(in: freetext), isAcceptedClock(match.digits) { @@ -50,7 +51,9 @@ public enum MECPDecode { removal.append(match.range) } - if codes.contains(etaCode), let match = leadingEta(in: freetext) { + // mecp-org/MECP#48: a bare eta that is the number of a person count at the same start is not an eta. + if codes.contains(etaCode), let match = leadingEta(in: freetext), + pax?.range.lowerBound != match.range.lowerBound { extraction.eta = match.minutes removal.append(match.range) } diff --git a/MC1Services/Tests/MC1ServicesTests/MECPDecodeTests.swift b/MC1Services/Tests/MC1ServicesTests/MECPDecodeTests.swift index d5a17bfdf0..cc342d33bf 100644 --- a/MC1Services/Tests/MC1ServicesTests/MECPDecodeTests.swift +++ b/MC1Services/Tests/MC1ServicesTests/MECPDecodeTests.swift @@ -140,6 +140,29 @@ struct MECPDecodeTests { #expect(message.displayTail == "water") } + @Test + func `a leading spaced person count is not an R03 eta`() throws { + let alone = try #require(MECPDecode.decode("MECP/2/R03 4 pax")) + #expect(alone.eta == nil) + #expect(alone.personCount == 4) + #expect(alone.displayTail == nil) + + let followed = try #require(MECPDecode.decode("MECP/2/R03 4 pax water")) + #expect(followed.eta == nil) + #expect(followed.personCount == 4) + #expect(followed.displayTail == "water") + + let padded = try #require(MECPDecode.decode("MECP/2/R03 04 pax water")) + #expect(padded.eta == nil) + #expect(padded.personCount == 4) + #expect(padded.displayTail == "water") + + let separate = try #require(MECPDecode.decode("MECP/2/R03 45 4pax")) + #expect(separate.eta == 45) + #expect(separate.personCount == 4) + #expect(separate.displayTail == nil) + } + @Test func `a person count too large for Int is clamped`() { #expect(MECPDecode.decode("MECP/1/S01 99999999999999999999pax")?.personCount == Int.max)