Skip to content

Commit a12ca3e

Browse files
Q7DF12dust
andauthored
Feature/regex and protocol search (#5364)
* add windows compile options * add regex search and search from protocol function(#5354) * Delete compile-hevtun.bat * Delete fix_headers.bat * Extract matchesPattern to extension --------- Co-authored-by: 2dust <31833384+2dust@users.noreply.github.com>
1 parent 4e9df84 commit a12ca3e

File tree

2 files changed

+29
-7
lines changed

2 files changed

+29
-7
lines changed

V2rayNG/app/src/main/java/com/v2ray/ang/extension/_Ext.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,4 +189,15 @@ fun String.concatUrl(vararg paths: String): String {
189189
}
190190

191191
return builder.toString()
192+
}
193+
194+
/**
195+
* Helper function to match text either by Regex or literal string.
196+
*/
197+
fun String.matchesPattern(regex: Regex?, keyword: String?, ignoreCase: Boolean = true): Boolean {
198+
if (keyword.isNullOrEmpty()) {
199+
return true
200+
}
201+
return regex?.containsMatchIn(this)
202+
?: this.contains(keyword, ignoreCase = ignoreCase)
192203
}

V2rayNG/app/src/main/java/com/v2ray/ang/viewmodel/MainViewModel.kt

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import com.v2ray.ang.dto.ServersCache
1919
import com.v2ray.ang.dto.SubscriptionCache
2020
import com.v2ray.ang.dto.SubscriptionUpdateResult
2121
import com.v2ray.ang.dto.TestServiceMessage
22+
import com.v2ray.ang.extension.matchesPattern
2223
import com.v2ray.ang.extension.serializable
2324
import com.v2ray.ang.extension.toastError
2425
import com.v2ray.ang.extension.toastSuccess
@@ -35,6 +36,7 @@ import kotlinx.coroutines.cancelChildren
3536
import kotlinx.coroutines.launch
3637
import kotlinx.coroutines.withContext
3738
import java.util.Collections
39+
import java.util.regex.PatternSyntaxException
3840

3941
class MainViewModel(application: Application) : AndroidViewModel(application) {
4042
private var serverList = mutableListOf<String>() // MmkvManager.decodeServerList()
@@ -117,19 +119,28 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
117119
@Synchronized
118120
fun updateCache() {
119121
serversCache.clear()
120-
val kw = keywordFilter.trim().lowercase()
122+
val kw = keywordFilter.trim()
123+
val searchRegex = try {
124+
if (kw.isNotEmpty()) Regex(kw, setOf(RegexOption.IGNORE_CASE)) else null
125+
} catch (e: PatternSyntaxException) {
126+
null // Fallback to literal search if regex is invalid
127+
}
121128
for (guid in serverList) {
122129
val profile = MmkvManager.decodeServerConfig(guid) ?: continue
123130
if (kw.isEmpty()) {
124131
serversCache.add(ServersCache(guid, profile))
125132
continue
126133
}
127134

128-
val remarks = profile.remarks.lowercase()
129-
val description = profile.description.orEmpty().lowercase()
130-
val server = profile.server.orEmpty().lowercase()
131-
132-
if (remarks.contains(kw) || description.contains(kw) || server.contains(kw)) {
135+
val remarks = profile.remarks
136+
val description = profile.description.orEmpty()
137+
val server = profile.server.orEmpty()
138+
val protocol = profile.configType.name
139+
if (remarks.matchesPattern(searchRegex, kw)
140+
|| description.matchesPattern(searchRegex, kw)
141+
|| server.matchesPattern(searchRegex, kw)
142+
|| protocol.matchesPattern(searchRegex, kw)
143+
) {
133144
serversCache.add(ServersCache(guid, profile))
134145
}
135146
}
@@ -479,4 +490,4 @@ class MainViewModel(application: Application) : AndroidViewModel(application) {
479490
}
480491
}
481492
}
482-
}
493+
}

0 commit comments

Comments
 (0)