Skip to content

Commit

Permalink
fix: Not Closed brackets and curcly braces
Browse files Browse the repository at this point in the history
Signed-off-by: Sam Smallman <srsmallman@mac.com>
  • Loading branch information
sammysmallman committed Oct 1, 2023
1 parent 1dd0f2f commit 8a03c09
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
21 changes: 13 additions & 8 deletions Sources/CoreOSC/OSCMatch.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -118,7 +118,6 @@ public enum OSCMatch {
patternCharactersMatched: 0,
addressCharactersMatched: 0)
}

return OSCPatternMatch(match: matching,
patternCharactersMatched:
addressPattern.distance(from: addressPattern.startIndex,
Expand Down Expand Up @@ -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] {
Expand Down Expand Up @@ -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)
Expand Down
40 changes: 37 additions & 3 deletions Tests/CoreOSCTests/OSCMatchTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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() {
Expand Down

0 comments on commit 8a03c09

Please sign in to comment.