Skip to content

Commit 3b75e1e

Browse files
committed
refactor: moved the Regex to a companion object constant so it's compiled once instead of on every keystroke
1 parent 4017c5c commit 3b75e1e

1 file changed

Lines changed: 10 additions & 8 deletions

File tree

app/src/main/java/to/bitkit/ui/settings/advanced/RgsServerViewModel.kt

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,20 +111,22 @@ class RgsServerViewModel @Inject constructor(
111111
}
112112

113113
private fun isValidURL(data: String): Boolean {
114-
val pattern = Regex(
114+
// Allow localhost in development mode
115+
if (Env.isDebug && data.contains("localhost")) {
116+
return true
117+
}
118+
119+
return URL_PATTERN.matches(data)
120+
}
121+
122+
companion object {
123+
private val URL_PATTERN = Regex(
115124
"^(https?://)?" + // protocol
116125
"((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|" + // domain name
117126
"((\\d{1,3}\\.){3}\\d{1,3}))" + // IP (v4) address
118127
"(:\\d+)?(/[-a-z\\d%_.~+]*)*", // port and path
119128
RegexOption.IGNORE_CASE
120129
)
121-
122-
// Allow localhost in development mode
123-
if (Env.isDebug && data.contains("localhost")) {
124-
return true
125-
}
126-
127-
return pattern.matches(data)
128130
}
129131
}
130132

0 commit comments

Comments
 (0)