From 8a03c09b80db1779f493e7a7fa12d06b7c6bc28f Mon Sep 17 00:00:00 2001 From: Sam Smallman Date: Sun, 1 Oct 2023 14:48:23 +0100 Subject: [PATCH] fix: Not Closed brackets and curcly braces Signed-off-by: Sam Smallman --- Sources/CoreOSC/OSCMatch.swift | 21 ++++++++------ Tests/CoreOSCTests/OSCMatchTests.swift | 40 ++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 11 deletions(-) diff --git a/Sources/CoreOSC/OSCMatch.swift b/Sources/CoreOSC/OSCMatch.swift index 82a7dd8..8650952 100644 --- a/Sources/CoreOSC/OSCMatch.swift +++ b/Sources/CoreOSC/OSCMatch.swift @@ -78,8 +78,8 @@ public enum OSCMatch { addressCharacterOffset: &addressCharacterOffset) if match == false { if patternCharacterOffset != addressPattern.endIndex && - addressPattern.index(after: patternCharacterOffset) == addressPattern.endIndex && - addressPattern[patternCharacterOffset] == "]" { + addressPattern.index(after: patternCharacterOffset) == addressPattern.endIndex && + addressPattern[patternCharacterOffset] == "]" { return OSCPatternMatch(match: .unmatched, patternCharactersMatched: addressPattern.distance(from: addressPattern.startIndex, to: addressPattern.endIndex), @@ -118,7 +118,6 @@ public enum OSCMatch { patternCharactersMatched: 0, addressCharactersMatched: 0) } - return OSCPatternMatch(match: matching, patternCharactersMatched: addressPattern.distance(from: addressPattern.startIndex, @@ -224,17 +223,19 @@ public enum OSCMatch { while patternCharacterOffset != pattern.endIndex && pattern[patternCharacterOffset] != "]" { if pattern[pattern.index(after: patternCharacterOffset)] == "-" { - if address[addressCharacterOffset].asciiValue! >= pattern[patternCharacterOffset].asciiValue! && - address[addressCharacterOffset].asciiValue! <= pattern[pattern.index(patternCharacterOffset, - offsetBy: 2)].asciiValue! { + if address[addressCharacterOffset].asciiValue! >= pattern[patternCharacterOffset].asciiValue!, + let index = pattern.index(patternCharacterOffset, offsetBy: 2, limitedBy: pattern.index(before: pattern.endIndex)), + address[addressCharacterOffset].asciiValue! <= pattern[index].asciiValue! { matched = val while patternCharacterOffset != pattern.endIndex && pattern[patternCharacterOffset] != "]" { patternCharacterOffset = pattern.index(after: patternCharacterOffset) } break + } else if let index = pattern.index(patternCharacterOffset, offsetBy: 3, limitedBy: pattern.index(before: pattern.endIndex)) { + patternCharacterOffset = index } else { - patternCharacterOffset = pattern.index(patternCharacterOffset, offsetBy: 3) + return false } } else { if pattern[patternCharacterOffset] == address[addressCharacterOffset] { @@ -283,8 +284,12 @@ public enum OSCMatch { pattern[offset] != "/" { offset = pattern.index(after: offset) } - patternCharacterOffset = offset + if pattern.endIndex == pattern.index(patternCharacterOffset, offsetBy: distance) || pattern[offset] != "}" { + patternCharacterOffset = startIndex + return false + } addressCharacterOffset = address.index(addressCharacterOffset, offsetBy: distance - 1) + patternCharacterOffset = offset return true } else { offset = pattern.index(after: offset) diff --git a/Tests/CoreOSCTests/OSCMatchTests.swift b/Tests/CoreOSCTests/OSCMatchTests.swift index 4b73f13..0aaa769 100644 --- a/Tests/CoreOSCTests/OSCMatchTests.swift +++ b/Tests/CoreOSCTests/OSCMatchTests.swift @@ -257,7 +257,21 @@ final class OSCMatchTests: XCTestCase { patternCharactersMatched: "/abc/[!d-f]".count, addressCharactersMatched: "/abc/".count)) } - + + func testInvalidSquareBracketsNotClosed() { + XCTAssertEqual(OSCMatch.match(addressPattern: "/abc/[d-", + address: "/abc/d"), + OSCPatternMatch(match: .unmatched, + patternCharactersMatched: "/abc/[".count, + addressCharactersMatched: "/abc/".count)) + + XCTAssertEqual(OSCMatch.match(addressPattern: "/abc/[!d-", + address: "/abc/d"), + OSCPatternMatch(match: .unmatched, + patternCharactersMatched: "/abc/[!".count, + addressCharactersMatched: "/abc/".count)) + } + // MARK: - Curly Braces Wildcard OSC Address Pattern Tests func testCurlyBracesFullMatch() { @@ -284,14 +298,34 @@ final class OSCMatchTests: XCTestCase { addressCharactersMatched: "/abc".count)) } - func testCurlyBraceUnmatched() { + func testCurlyBracesUnmatched() { XCTAssertEqual(OSCMatch.match(addressPattern: "/{abc,def}/{ghi,jkl}", address: "/abc/mno"), OSCPatternMatch(match: .unmatched, patternCharactersMatched: "/{abc,def}/".count, addressCharactersMatched: "/abc/".count)) } - + + func testInvalidCurlyBracesNotClosed() { + XCTAssertEqual(OSCMatch.match(addressPattern: "/abc/{d", + address: "/abc/d"), + OSCPatternMatch(match: .unmatched, + patternCharactersMatched: "/abc/".count, + addressCharactersMatched: "/abc/".count)) + + XCTAssertEqual(OSCMatch.match(addressPattern: "/abc/{d/ghi", + address: "/abc/d/g"), + OSCPatternMatch(match: .unmatched, + patternCharactersMatched: "/abc/".count, + addressCharactersMatched: "/abc/".count)) + + XCTAssertEqual(OSCMatch.match(addressPattern: "/{a}/{d/g", + address: "/a/d/g"), + OSCPatternMatch(match: .unmatched, + patternCharactersMatched: "/{a}/".count, + addressCharactersMatched: "/a/".count)) + } + // MARK: - All Wildcards OSC Address Pattern Tests func testAllWildcardsFullMatch() {