Skip to content

Commit

Permalink
Merge pull request #139 from Semper-Viventem/fix-regex-matching
Browse files Browse the repository at this point in the history
Fix regex matching
  • Loading branch information
Semper-Viventem authored May 6, 2024
2 parents 231ebdb + 2b14cc8 commit 9bbbae3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ android {
minSdk = 29
targetSdk = 34

versionCode = 1708536354
versionName = "0.25.2-beta"
versionCode = 1708536355
versionName = "0.25.3-beta"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package f.cking.software.domain.interactor.filterchecker

import f.cking.software.checkRegexSafe
import f.cking.software.data.helpers.PowerModeHelper
import f.cking.software.data.repo.DevicesRepository
import f.cking.software.domain.interactor.CheckDeviceIsFollowingInteractor
Expand All @@ -26,12 +27,12 @@ class FilterCheckerImpl(
device.firstDetectTimeMs in filter.from..filter.to
}
private val name = filterChecker<RadarProfile.Filter.Name>(useCache = true) { device, filter ->
val regexMatch = device.name?.contains(filter.name.toRegex()) ?: false
val regexMatch = device.name?.checkRegexSafe(filter.name) ?: false
val noCaseSubstringMatch = device.name?.contains(filter.name, filter.ignoreCase) ?: false
regexMatch || noCaseSubstringMatch
}
private val address = filterChecker<RadarProfile.Filter.Address>(useCache = true) { device, filter ->
device.address.contains(filter.address.toRegex())
device.address.checkRegexSafe(filter.address)
}
private val manufacturer = filterChecker<RadarProfile.Filter.Manufacturer>(useCache = true) { device, filter ->
device.manufacturerInfo?.id?.let { it == filter.manufacturerId } ?: false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import f.cking.software.BuildConfig
import f.cking.software.R
import f.cking.software.checkRegexSafe
import f.cking.software.data.helpers.IntentHelper
import f.cking.software.data.repo.DevicesRepository
import f.cking.software.data.repo.SettingsRepository
Expand Down Expand Up @@ -294,8 +295,8 @@ class DeviceListViewModel(
|| (device.customName?.contains(searchStr, true) ?: false)
|| (device.manufacturerInfo?.name?.contains(searchStr, true) ?: false)
|| device.address.contains(searchStr, true)
|| device.address.contains(query.toRegex())
|| (device.name?.contains(query.toRegex()) ?: false)
|| device.address.checkRegexSafe(query)
|| (device.name?.checkRegexSafe(query) ?: false)
} ?: true
}

Expand Down
13 changes: 13 additions & 0 deletions app/src/main/java/f/cking/software/utils/ext.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import android.view.Display
import android.view.WindowManager
import android.widget.Toast
import androidx.core.content.ContextCompat
import timber.log.Timber
import java.security.MessageDigest
import java.time.Instant
import java.time.LocalDate
import java.time.LocalDateTime
import java.time.LocalTime
import java.time.ZoneId
import java.time.format.DateTimeFormatter
import java.util.regex.PatternSyntaxException


fun Long.getTimePeriodStr(context: Context): String {
Expand Down Expand Up @@ -125,4 +127,15 @@ fun <T> List<T>.splitToBatches(batchSize: Int): List<List<T>> {
fun Context.isDarkModeOn(): Boolean {
val nightModeFlags = resources.configuration.uiMode and android.content.res.Configuration.UI_MODE_NIGHT_MASK
return nightModeFlags == android.content.res.Configuration.UI_MODE_NIGHT_YES
}

fun String.checkRegexSafe(pattern: String): Boolean {
return try {
this.contains(pattern.toRegex())
} catch (e: PatternSyntaxException) {
false
} catch (e: Throwable) {
Timber.e("Unexpected regex failure", e)
false
}
}

0 comments on commit 9bbbae3

Please sign in to comment.