Optimize regex
This commit is contained in:
@@ -2,7 +2,6 @@ package top.fatweb.api.util
|
|||||||
|
|
||||||
import jakarta.servlet.http.HttpServletRequest
|
import jakarta.servlet.http.HttpServletRequest
|
||||||
import org.springframework.web.servlet.mvc.condition.RequestCondition
|
import org.springframework.web.servlet.mvc.condition.RequestCondition
|
||||||
import java.util.regex.Pattern
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Api version condition
|
* Api version condition
|
||||||
@@ -11,15 +10,13 @@ import java.util.regex.Pattern
|
|||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
class ApiVersionCondition(private val apiVersion: Int) : RequestCondition<ApiVersionCondition> {
|
class ApiVersionCondition(private val apiVersion: Int) : RequestCondition<ApiVersionCondition> {
|
||||||
private val versionPrefixPattern: Pattern = Pattern.compile(".*v(\\d+).*")
|
private val versionPrefixRegex = Regex(".*v(\\d+).*")
|
||||||
|
|
||||||
override fun combine(other: ApiVersionCondition): ApiVersionCondition = ApiVersionCondition(other.apiVersion)
|
override fun combine(other: ApiVersionCondition): ApiVersionCondition = ApiVersionCondition(other.apiVersion)
|
||||||
|
|
||||||
override fun getMatchingCondition(request: HttpServletRequest): ApiVersionCondition? {
|
override fun getMatchingCondition(request: HttpServletRequest): ApiVersionCondition? {
|
||||||
val matcher = versionPrefixPattern.matcher(request.requestURI)
|
versionPrefixRegex.matchAt(request.requestURI, 0)?.let {
|
||||||
if (matcher.find()) {
|
if (it.groupValues[1].toInt() >= this.apiVersion) {
|
||||||
val version = matcher.group(1).toInt()
|
|
||||||
if (version >= this.apiVersion) {
|
|
||||||
return this
|
return this
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
package top.fatweb.api.util
|
package top.fatweb.api.util
|
||||||
|
|
||||||
import java.security.MessageDigest
|
import java.security.MessageDigest
|
||||||
import java.util.regex.Pattern
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* String util
|
* String util
|
||||||
@@ -10,7 +9,7 @@ import java.util.regex.Pattern
|
|||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
object StrUtil {
|
object StrUtil {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Convert CamelCase string to underscore-delimited string
|
* Convert CamelCase string to underscore-delimited string
|
||||||
*
|
*
|
||||||
@@ -22,16 +21,7 @@ object StrUtil {
|
|||||||
fun upperToUnderLetter(str: String?): String {
|
fun upperToUnderLetter(str: String?): String {
|
||||||
str ?: let { return "" }
|
str ?: let { return "" }
|
||||||
|
|
||||||
val matcher = Pattern.compile("([A-Z])").matcher(str)
|
return Regex("[A-Z]").replace(str) { matchResult -> "_${matchResult.value.lowercase()}" }
|
||||||
val stringBuilder = StringBuilder()
|
|
||||||
|
|
||||||
while (matcher.find()) {
|
|
||||||
matcher.appendReplacement(stringBuilder, "_" + matcher.group().lowercase())
|
|
||||||
}
|
|
||||||
|
|
||||||
matcher.appendTail(stringBuilder)
|
|
||||||
|
|
||||||
return stringBuilder.toString()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -45,16 +35,7 @@ object StrUtil {
|
|||||||
fun underToUpperLetter(str: String?): String {
|
fun underToUpperLetter(str: String?): String {
|
||||||
str ?: let { return "" }
|
str ?: let { return "" }
|
||||||
|
|
||||||
val matcher = Pattern.compile("(_)([a-z])").matcher(str)
|
return Regex("_[a-z]").replace(str) { matchResult -> matchResult.value.removePrefix("_").uppercase() }
|
||||||
val stringBuilder = StringBuilder()
|
|
||||||
|
|
||||||
while (matcher.find()) {
|
|
||||||
matcher.appendReplacement(stringBuilder, matcher.group().replace("_", "").uppercase())
|
|
||||||
}
|
|
||||||
|
|
||||||
matcher.appendTail(stringBuilder)
|
|
||||||
|
|
||||||
return stringBuilder.toString()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -65,7 +46,7 @@ object StrUtil {
|
|||||||
* @author FatttSnake, fatttsnake@gmail.com
|
* @author FatttSnake, fatttsnake@gmail.com
|
||||||
* @since 1.0.0
|
* @since 1.0.0
|
||||||
*/
|
*/
|
||||||
fun getRandomPassword(length: Int): String {
|
fun getRandomPassword(length: Int): String {
|
||||||
val characterSet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
val characterSet = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
val password = StringBuilder()
|
val password = StringBuilder()
|
||||||
|
|
||||||
@@ -92,7 +73,7 @@ object StrUtil {
|
|||||||
if (b < 0x10) {
|
if (b < 0x10) {
|
||||||
str = "0$str"
|
str = "0$str"
|
||||||
}
|
}
|
||||||
hex.append(str.substring(str.length -2))
|
hex.append(str.substring(str.length - 2))
|
||||||
}
|
}
|
||||||
return hex.toString()
|
return hex.toString()
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user