正規表現フィルタに空文字列にマッチする正規表現を指定したらエラーを表示する

データが空文字列だった場合は正規表現フィルタの内容に関わらず非マッチとする
This commit is contained in:
tateisu 2018-06-30 20:18:17 +09:00
parent 771ab49089
commit 48b7c327be
5 changed files with 33 additions and 29 deletions

View File

@ -1121,7 +1121,7 @@ class Column(
try {
val re = Pattern.compile(regex_text)
column_regex_filter =
{ text : CharSequence? -> if(text == null) false else re.matcher(text).find() }
{ text : CharSequence? -> if(text?.isEmpty() != false ) false else re.matcher(text).find() }
} catch(ex : Throwable) {
log.trace(ex)
}

View File

@ -29,8 +29,6 @@ import android.support.v7.widget.ListRecyclerView
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.text.SpannableStringBuilder
import android.text.Spanned
import jp.juggler.subwaytooter.span.EmojiImageSpan
import jp.juggler.subwaytooter.util.*
import jp.juggler.subwaytooter.view.ListDivider
import java.io.Closeable
@ -127,30 +125,34 @@ class ColumnViewHolder(
private var last_image_bitmap : Bitmap? = null
private var last_image_task : AsyncTask<Void, Void, Bitmap?>? = null
private val isRegexValid : Boolean
get() {
val s = etRegexFilter.text.toString()
if(s.isEmpty()) {
tvRegexFilterError.text = ""
return true
private fun checkRegexFilterError(src : String) : String? {
try {
if(src.isEmpty()) {
return null
}
try {
val m = Pattern.compile(s).matcher("")
if(m.find()) {
// XXX: 空文字列にマッチする正規表現はエラー扱いにする? しなくてもよい?
// tvRegexFilterError.text = activity.getString(R.string.)
}
} catch(ex : Throwable) {
val message = ex.message
tvRegexFilterError.text = if(message != null && message.isNotEmpty()) {
message
} else {
ex.withCaption(activity.resources, R.string.regex_error)
}
return false
val m = Pattern.compile(src).matcher("")
if(m.find()) {
// 空文字列にマッチする正規表現はエラー扱いにする
// そうしないとCWの警告テキストにマッチしてしまう
return activity.getString(R.string.regex_filter_matches_empty_string)
}
return null
} catch(ex : Throwable) {
val message = ex.message
return if(message != null && message.isNotEmpty()) {
message
} else {
ex.withCaption(activity.resources, R.string.regex_error)
}
return true
}
}
private fun isRegexValid() : Boolean {
val s = etRegexFilter.text.toString()
val error = checkRegexFilterError(s)
tvRegexFilterError.text = error ?: ""
return error == null
}
val isColumnSettingShown : Boolean
get() = llColumnSetting.visibility == View.VISIBLE
@ -292,8 +294,8 @@ class ColumnViewHolder(
override fun afterTextChanged(s : Editable) {
if(loading_busy) return
activity.handler.removeCallbacks(proc_start_filter)
if(isRegexValid) {
activity.handler.postDelayed(proc_start_filter, 1500L)
if(isRegexValid()) {
activity.handler.postDelayed(proc_start_filter, 666L)
}
}
})
@ -312,8 +314,7 @@ class ColumnViewHolder(
}
private val proc_start_filter = Runnable {
if(isPageDestroyed) return@Runnable
if(isRegexValid) {
if(! isPageDestroyed && isRegexValid() ) {
val column = this.column ?: return@Runnable
column.regex_text = etRegexFilter.text.toString()
activity.app_state.saveColumnList()
@ -479,7 +480,7 @@ class ColumnViewHolder(
// tvRegexFilterErrorの表示を更新
if(bAllowFilter) {
isRegexValid
isRegexValid()
}
when(column.column_type) {

View File

@ -688,6 +688,7 @@
<string name="notification_tl_font_size">Notification TL font size (unit:sp. leave empty to default. app restart required)</string>
<string name="notification_tl_icon_size">Notification TL icon size (unit:dp. default:32. app restart required)</string>
<string name="post_button_tapped_repeatly">Post button was tapped repeatedly</string>
<string name="regex_filter_matches_empty_string">regex filter matches to empty string.</string>
<!--<string name="abc_action_bar_home_description">Revenir à l\'accueil</string>-->
<!--<string name="abc_action_bar_home_description_format">%1$s, %2$s</string>-->

View File

@ -966,5 +966,6 @@
<string name="notification_tl_font_size">通知TLのフォントサイズ (単位:sp。空欄でデフォルト。アプリ再起動が必要)</string>
<string name="notification_tl_icon_size">通知TLのアイコンサイズ (単位:dp。デフォルト:32。アプリ再起動が必要)</string>
<string name="post_button_tapped_repeatly">投稿ボタンの連打を検出</string>
<string name="regex_filter_matches_empty_string">正規表現フィルタは空文字列にマッチしてしまいます</string>
</resources>

View File

@ -673,4 +673,5 @@
<string name="notification_tl_font_size">Notification TL font size (unit:sp. leave empty to default. app restart required)</string>
<string name="notification_tl_icon_size">Notification TL icon size (unit:dp. default:32. app restart required)</string>
<string name="post_button_tapped_repeatly">Post button was tapped repeatedly</string>
<string name="regex_filter_matches_empty_string">regex filter matches to empty string.</string>
</resources>