diff --git a/MC1Services/Sources/MC1Services/MECP/MECPDecode.swift b/MC1Services/Sources/MC1Services/MECP/MECPDecode.swift index 815496cbbd..7fccc980f1 100644 --- a/MC1Services/Sources/MC1Services/MECP/MECPDecode.swift +++ b/MC1Services/Sources/MC1Services/MECP/MECPDecode.swift @@ -70,7 +70,7 @@ public enum MECPDecode { var extraction = Extraction() // Claim timestamp, then person count, then duration. A later match skips characters an earlier one claimed. - if let match = firstTimestamp(in: freetext, excluding: claimed), isAcceptedClock(match.digits) { + if let match = firstTimestamp(in: freetext, excluding: claimed) { extraction.timestampHHMM = match.digits claimed.insert(contentsOf: match.range) removed.insert(contentsOf: match.range) @@ -95,8 +95,6 @@ public enum MECPDecode { static let etaCode = "R03" private static let wirePrefix = "MECP/" private static let maxHour = 23 - private static let maxMinute = 59 - private static let clockDigits = 4 private static let clockPartDigits = 2 private static let asciiZero: UInt32 = 0x30 private static let asciiFive: UInt32 = 0x35 @@ -107,8 +105,10 @@ public enum MECPDecode { #/0*([1-9][0-9]*) ?[Pp][Aa][Xx]/#.matchingSemantics(.unicodeScalar) } - private static var timestampPattern: Regex<(Substring, Substring)> { - #/@([0-9]{4})(?= |$)/#.matchingSemantics(.unicodeScalar) + /// MECP 1.4.0 §6.1 timestamp decoding pattern, without the boundaries `isBounded` checks. The two-digit + /// hours come first so `@2300` matches whole rather than as `@230` followed by `0`. + private static var timestampPattern: Regex<(Substring, Substring, Substring)> { + #/@(2[0-3]|[01][0-9]|[0-9]):?([0-5][0-9])/#.matchingSemantics(.unicodeScalar) } private static var leadingEtaPattern: Regex<(Substring, Substring)> { @@ -224,14 +224,18 @@ public enum MECPDecode { return before && after } + /// The first bounded timestamp that misses `claimed`, as four digits. A rejected match has only one `@`, at + /// its start, so resuming after it does not skip a later timestamp. An out-of-range value such as `@2460` + /// does not match. private static func firstTimestamp( in text: String, excluding claimed: RangeSet ) -> (digits: String, range: Range)? { var cursor = text.startIndex while cursor < text.endIndex, let match = text[cursor...].firstMatch(of: timestampPattern) { - if !overlaps(claimed, match.range) { - return (String(match.1), match.range) + if isBounded(match.range, in: text), !overlaps(claimed, match.range) { + let hour = match.1.count == 1 ? "0\(match.1)" : String(match.1) + return (hour + match.2, match.range) } cursor = match.range.upperBound } @@ -250,15 +254,6 @@ public enum MECPDecode { return (minutes, match.range) } - private static func isAcceptedClock(_ digits: String) -> Bool { - guard digits.count == clockDigits, - let hour = Int(digits.prefix(clockPartDigits)), - let minute = Int(digits.suffix(clockPartDigits)) else { - return false - } - return hour <= maxHour && minute <= maxMinute - } - private static func removing(_ subranges: RangeSet, from text: String) -> String? { guard !subranges.isEmpty else { return text.allSatisfy(\.isWhitespace) ? nil : text diff --git a/MC1Services/Tests/MC1ServicesTests/MECPDecodeTests.swift b/MC1Services/Tests/MC1ServicesTests/MECPDecodeTests.swift index 3d2a0ffcb7..d5ae28b9a2 100644 --- a/MC1Services/Tests/MC1ServicesTests/MECPDecodeTests.swift +++ b/MC1Services/Tests/MC1ServicesTests/MECPDecodeTests.swift @@ -189,7 +189,7 @@ struct MECPDecodeTests { @Test func `colon clock minutes are not person counts`() throws { - for tail in ["@6:30 pax", "14:30pax"] { + for tail in ["@6:30pax", "14:30pax"] { let message = try #require(MECPDecode.decode("MECP/1/S01 \(tail)"), "\(tail)") #expect(message.personCount == nil, "\(tail)") #expect(message.displayTail == tail, "\(tail)") @@ -199,9 +199,15 @@ struct MECPDecodeTests { #expect(longerHour.personCount == 30) #expect(longerHour.displayTail == "006:") + let claimed = try #require(MECPDecode.decode("MECP/1/S01 @6:30 pax")) + #expect(claimed.timestampHHMM == "0630") + #expect(claimed.personCount == nil) + #expect(claimed.displayTail == "pax") + let resumed = try #require(MECPDecode.decode("MECP/1/S01 @6:30 pax 2 pax")) + #expect(resumed.timestampHHMM == "0630") #expect(resumed.personCount == 2) - #expect(resumed.displayTail == "@6:30 pax") + #expect(resumed.displayTail == "pax") let invalidHour = try #require(MECPDecode.decode("MECP/1/S01 @24:30pax")) #expect(invalidHour.personCount == 30) @@ -212,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")) @@ -227,18 +250,47 @@ struct MECPDecodeTests { @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") + #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") + for tail in ["@1460", "@9:60", "@24:00"] { + #expect(MECPDecode.decode("MECP/3/L06 \(tail)")?.timestampHHMM == nil, "\(tail)") + } + } + @Test func `decode keeps a string longer than the sender byte budget`() throws { #expect(MECPMessage.maxUTF8Bytes == 200)