Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 39 additions & 20 deletions MC1Services/Sources/MC1Services/MECP/MECPDecode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,20 @@ public enum MECPDecode {
var removal: [Range<String.Index>] = []
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) {
extraction.timestampHHMM = match.digits
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)
}
Expand All @@ -63,29 +66,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]/#
Comment thread
Avi0n marked this conversation as resolved.
Resolved
.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<String.Index>)? {
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..<index]) else { return nil }
let end = text.index(index, offsetBy: paxSuffix.count)
return (count, start..<end)
guard let match = text.matches(of: paxPattern).first(where: { isBounded($0.range, in: text) }) else {
return nil
}
return nil
// The pattern does not limit the digits; a count too large for Int is clamped, not dropped.
return (Int(match.1) ?? Int.max, match.range)
}

/// MECP 1.4.0 §6.1 rule 4: the boundary class B.
private static let boundary: Set<Unicode.Scalar> = {
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<UInt32>] = [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<String.Index>, 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<String.Index>)? {
Expand Down
67 changes: 67 additions & 0 deletions MC1Services/Tests/MC1ServicesTests/MECPDecodeTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,73 @@ 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 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)
}

@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."))
Expand Down
Loading