Open-source multiplayer game server compatible with the RuneScape client
https://www.openrs2.org/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.3 KiB
55 lines
1.3 KiB
package dev.openrs2.asm.filter
|
|
|
|
object Glob {
|
|
fun compile(pattern: String): Regex {
|
|
return compile(pattern, className = false)
|
|
}
|
|
|
|
fun compileClass(pattern: String): Regex {
|
|
return compile(pattern, className = true)
|
|
}
|
|
|
|
private fun compile(pattern: String, className: Boolean): Regex {
|
|
val regex = StringBuilder()
|
|
var star = false
|
|
var escape = false
|
|
|
|
for (ch in pattern) {
|
|
check(!star || !escape)
|
|
|
|
if (star) {
|
|
star = false
|
|
|
|
if (ch == '*') {
|
|
regex.append(".*")
|
|
continue
|
|
}
|
|
|
|
regex.append("[^/]*")
|
|
} else if (escape) {
|
|
regex.append(Regex.escape(ch.toString()))
|
|
continue
|
|
}
|
|
|
|
when (ch) {
|
|
'*' -> if (className) {
|
|
star = true
|
|
} else {
|
|
regex.append(".*")
|
|
}
|
|
'\\' -> escape = true
|
|
else -> regex.append(Regex.escape(ch.toString()))
|
|
}
|
|
}
|
|
|
|
if (star) {
|
|
regex.append(".*")
|
|
}
|
|
|
|
require(!escape) {
|
|
"Unterminated escape sequence"
|
|
}
|
|
|
|
return Regex(regex.toString())
|
|
}
|
|
}
|
|
|