Skip to content

Commit 4ac5ee8

Browse files
committed
PortMatcher code review feedback
1 parent 02a8979 commit 4ac5ee8

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

src/main/kotlin/com/coder/jetbrains/matcher/PortMatcher.kt

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,7 @@ class PortMatcher(private val rule: String) {
77
data class RegexPort(val pattern: Regex) : MatchRule()
88
}
99

10-
private val parsedRule: MatchRule
11-
12-
init {
13-
parsedRule = parseRule(rule)
14-
}
10+
private val parsedRule: MatchRule = parseRule(rule)
1511

1612
fun matches(port: Int): Boolean {
1713
return when (parsedRule) {
@@ -33,12 +29,10 @@ class PortMatcher(private val rule: String) {
3329
MatchRule.SinglePort(port)
3430
}
3531
// Try parsing as port range (e.g., "40000-55000")
36-
portPart.matches("^\\d+-\\d+$".toRegex()) -> {
32+
portPart.matches("^\\d+\\s*-\\s*\\d+$".toRegex()) -> {
3733
val (start, end) = portPart.split('-')
3834
.map { it.trim().toInt() }
39-
validatePort(start)
40-
validatePort(end)
41-
require(start <= end) { "Invalid port range: start must be less than or equal to end" }
35+
validatePortRange(start, end)
4236
MatchRule.PortRange(start, end)
4337
}
4438
// If not a single port or range, treat as regex
@@ -56,7 +50,9 @@ class PortMatcher(private val rule: String) {
5650
require(port in 0..65535) { "Port number must be between 0 and 65535, got: $port" }
5751
}
5852

59-
companion object {
60-
const val MAX_PORT = 65535
53+
private fun validatePortRange(start: Int, end: Int) {
54+
validatePort(start)
55+
validatePort(end)
56+
require(start <= end) { "Invalid port range: start must be less than or equal to end" }
6157
}
6258
}

src/test/kotlin/com/coder/jetbrains/matcher/PortMatcherTest.kt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ class PortMatcherTest {
3232
assertFalse(matcher.matches(55001))
3333
}
3434

35+
@Test
36+
fun `test port range with whitespace`() {
37+
val matcher = PortMatcher("20021 - 20024")
38+
assertFalse(matcher.matches(20000))
39+
assertTrue(matcher.matches(20022))
40+
}
41+
3542
@Test
3643
fun `test regex`() {
3744
val matcher = PortMatcher("800[1-9]")

0 commit comments

Comments
 (0)