From 64bd50b9cb812655986f853d0f15294c2840873f Mon Sep 17 00:00:00 2001 From: Blake Latchford Date: Fri, 20 Nov 2026 16:56:37 -0500 Subject: [PATCH 1/4] fix: Decode MECP timestamps per MECPv1.4.1 Accept @630, @6:30 and expand boundary conditions. Third commit in a series for issue #494. Co-Authored-By: Claude Opus 5.5 --- .../Sources/MC1Services/MECP/MECPDecode.swift | 52 +++++++++---------- .../MC1ServicesTests/MECPDecodeTests.swift | 37 +++++++++++-- 2 files changed, 56 insertions(+), 33 deletions(-) diff --git a/MC1Services/Sources/MC1Services/MECP/MECPDecode.swift b/MC1Services/Sources/MC1Services/MECP/MECPDecode.swift index 8329eebd60..1ac917abe4 100644 --- a/MC1Services/Sources/MC1Services/MECP/MECPDecode.swift +++ b/MC1Services/Sources/MC1Services/MECP/MECPDecode.swift @@ -1,3 +1,5 @@ +import RegexBuilder + /// The MECP wire grammar. A string that does not match returns nil. public enum MECPDecode { public static func decode(_ input: String) -> MECPMessage? { @@ -46,7 +48,7 @@ public enum MECPDecode { removal.append(pax.range) } - if let match = firstTimestamp(in: freetext), isAcceptedClock(match.digits) { + if let match = firstTimestamp(in: freetext) { extraction.timestampHHMM = match.digits removal.append(match.range) } @@ -63,9 +65,6 @@ public enum MECPDecode { } private static let etaCode = "R03" - private static let maxHour = 23 - private static let maxMinute = 59 - private static let clockDigits = 4 private static func isASCIIDigit(_ character: Character) -> Bool { character.isASCII && character.isNumber @@ -107,23 +106,29 @@ public enum MECPDecode { return before && after } - private static func firstTimestamp(in text: String) -> (digits: String, range: Range)? { - var search = text.startIndex - while search < text.endIndex, let at = text[search...].firstIndex(of: "@") { - var cursor = text.index(after: at) - var count = 0 - while cursor < text.endIndex, count < clockDigits, isASCIIDigit(text[cursor]) { - cursor = text.index(after: cursor) - count += 1 - } - let atBoundary = cursor == text.endIndex || text[cursor] == " " - if count == clockDigits, atBoundary { - let digitsStart = text.index(after: at) - return (String(text[digitsStart.. (digits: String, range: Range)? { + guard let match = text.matches(of: timestampPattern).first(where: { isBounded($0.range, in: text) }) else { + return nil + } + let hour = match.1.count == 1 ? "0\(match.1)" : String(match.1) + return (hour + match.2, match.range) } private static func leadingEta(in text: String) -> (minutes: Int, range: Range)? { @@ -137,15 +142,6 @@ public enum MECPDecode { return (minutes, start.. Bool { - guard digits.count == 4, - let hour = Int(digits.prefix(2)), - let minute = Int(digits.suffix(2)) else { - return false - } - return hour <= maxHour && minute <= maxMinute - } - private static func removing(_ ranges: [Range], from text: String) -> String? { var result = text for range in ranges.sorted(by: { $0.lowerBound > $1.lowerBound }) { diff --git a/MC1Services/Tests/MC1ServicesTests/MECPDecodeTests.swift b/MC1Services/Tests/MC1ServicesTests/MECPDecodeTests.swift index cc342d33bf..5bf9398453 100644 --- a/MC1Services/Tests/MC1ServicesTests/MECPDecodeTests.swift +++ b/MC1Services/Tests/MC1ServicesTests/MECPDecodeTests.swift @@ -167,22 +167,49 @@ struct MECPDecodeTests { 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.")) - #expect(punctuated.timestampHHMM == nil) - #expect(punctuated.displayTail == "arrived @1430.") + #expect(punctuated.timestampHHMM == "1430") + #expect(punctuated.displayTail == "arrived .") let invalid = try #require(MECPDecode.decode("MECP/3/R01 @2400 @1430")) - #expect(invalid.timestampHHMM == nil) - #expect(invalid.displayTail == "@2400 @1430") + #expect(invalid.timestampHHMM == "1430") + #expect(invalid.displayTail == "@2400") let accepted = try #require(MECPDecode.decode("MECP/0/M01 @1430 @en")) #expect(accepted.timestampHHMM == "1430") #expect(accepted.displayTail == "@en") } + @Test + func `a timestamp is removed from the tail`() throws { + let message = try #require(MECPDecode.decode("MECP/0/M01 @1430 @en")) + #expect(message.timestampHHMM == "1430") + #expect(message.displayTail == "@en") + } + + @Test + func `a timestamp may have a one-digit hour and a colon`() { + #expect(MECPDecode.decode("MECP/3/L01 @123")?.timestampHHMM == "0123") + #expect(MECPDecode.decode("MECP/2/T01 @6:30")?.timestampHHMM == "0630") + // The one-digit hour `@230` is tried first and fails the boundary after it. + #expect(MECPDecode.decode("MECP/3/L01 @2300")?.timestampHHMM == "2300") + } + + @Test + func `a timestamp needs a boundary on each side`() { + #expect(MECPDecode.decode("MECP/2/T05 @1430,")?.timestampHHMM == "1430") + #expect(MECPDecode.decode("MECP/2/T05 meet@1430")?.timestampHHMM == nil) + #expect(MECPDecode.decode("MECP/3/L06 @14300")?.timestampHHMM == nil) + } + + @Test + func `an out-of-range timestamp is skipped for the next one`() { + #expect(MECPDecode.decode("MECP/3/L06 @2460 @1400 @1530")?.timestampHHMM == "1400") + } + @Test func `decode keeps a string longer than the sender byte budget`() throws { #expect(MECPMessage.maxUTF8Bytes == 200) From 2291d21820363dde69736bcd2ab7e5f97fa5d4a1 Mon Sep 17 00:00:00 2001 From: Blake Latchford Date: Fri, 20 Nov 2026 16:59:00 -0500 Subject: [PATCH 2/4] Fix whitespace for linter --- MC1Services/Tests/MC1ServicesTests/MECPDecodeTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MC1Services/Tests/MC1ServicesTests/MECPDecodeTests.swift b/MC1Services/Tests/MC1ServicesTests/MECPDecodeTests.swift index 5bf9398453..51e32e6684 100644 --- a/MC1Services/Tests/MC1ServicesTests/MECPDecodeTests.swift +++ b/MC1Services/Tests/MC1ServicesTests/MECPDecodeTests.swift @@ -167,7 +167,7 @@ struct MECPDecodeTests { 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 322b445c5c37811361a8e48f017a2ff9dd24ba47 Mon Sep 17 00:00:00 2001 From: Blake Latchford Date: Sat, 21 Nov 2026 20:49:45 -0500 Subject: [PATCH 3/4] test: Cover MECP matches that overlap an earlier claim Co-Authored-By: Claude Opus 5.5 --- .../MC1ServicesTests/MECPDecodeTests.swift | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/MC1Services/Tests/MC1ServicesTests/MECPDecodeTests.swift b/MC1Services/Tests/MC1ServicesTests/MECPDecodeTests.swift index 1cd86f9eee..b90a3079bf 100644 --- a/MC1Services/Tests/MC1ServicesTests/MECPDecodeTests.swift +++ b/MC1Services/Tests/MC1ServicesTests/MECPDecodeTests.swift @@ -218,6 +218,23 @@ struct MECPDecodeTests { #expect(nonTimestamp.displayTail == "id:") } + /// MECP §6.1 rule 5: a later convention fails where it overlaps text an earlier one claimed. + @Test + func `a match overlapping an earlier claim is not decoded`() throws { + let durationInTimestamp = try #require(MECPDecode.decode("MECP/2/R03 @14:30 min")) + #expect(durationInTimestamp.timestampHHMM == "1430") + #expect(durationInTimestamp.eta == nil) + #expect(durationInTimestamp.displayTail == "min") + + let paxInCoordinates = try #require(MECPDecode.decode("MECP/0/S01 47.3769,8.5417 pax")) + #expect(paxInCoordinates.personCount == nil) + #expect(paxInCoordinates.displayTail == "47.3769,8.5417 pax") + + let etaInDesignator = try #require(MECPDecode.decode("MECP/2/U04 R03 eta 45")) + #expect(etaInDesignator.eta == nil) + #expect(etaInDesignator.displayTail == "eta 45") + } + @Test func `whitespace left after a removal collapses, and a following word stays`() throws { let onlyWhitespace = try #require(MECPDecode.decode("MECP/1/S01 4pax\t@1430")) From 88a4868b0d29943acd5e92391e5ae02b164064a0 Mon Sep 17 00:00:00 2001 From: Blake Latchford Date: Sun, 22 Nov 2026 04:42:04 -0500 Subject: [PATCH 4/4] test: Reject out-of-range MECP timestamp minutes and colon hours Co-Authored-By: Claude Opus 5.5 --- MC1Services/Tests/MC1ServicesTests/MECPDecodeTests.swift | 3 +++ 1 file changed, 3 insertions(+) diff --git a/MC1Services/Tests/MC1ServicesTests/MECPDecodeTests.swift b/MC1Services/Tests/MC1ServicesTests/MECPDecodeTests.swift index b90a3079bf..d5ae28b9a2 100644 --- a/MC1Services/Tests/MC1ServicesTests/MECPDecodeTests.swift +++ b/MC1Services/Tests/MC1ServicesTests/MECPDecodeTests.swift @@ -286,6 +286,9 @@ struct MECPDecodeTests { @Test func `an out-of-range timestamp is skipped for the next one`() { #expect(MECPDecode.decode("MECP/3/L06 @2460 @1400 @1530")?.timestampHHMM == "1400") + for tail in ["@1460", "@9:60", "@24:00"] { + #expect(MECPDecode.decode("MECP/3/L06 \(tail)")?.timestampHHMM == nil, "\(tail)") + } } @Test