Added length function in NameConditions

This commit is contained in:
2022-09-08 04:39:00 +08:00
parent e09d3282f0
commit 11e1beca3a
2 changed files with 44 additions and 0 deletions

View File

@@ -55,6 +55,12 @@ class NameConditions @PublishedApi internal constructor() {
/** 正则字符匹配条件 */
private var cdsMatches: Regex? = null
/** 字符长度匹配条件 */
private var cdsLength = -1
/** 字符长度范围匹配条件 */
private var cdsLengthRange = IntRange.EMPTY
/** 标识为匿名类的主类调用对象条件 */
private var isThisSynthetic0 = false
@@ -143,6 +149,22 @@ class NameConditions @PublishedApi internal constructor() {
cdsMatches = regex
}
/**
* 字符长度匹配
* @param num 预期的长度
*/
fun length(num: Int) {
cdsLength = num
}
/**
* 字符长度范围匹配
* @param numRange 预期的长度范围
*/
fun length(numRange: IntRange) {
cdsLengthRange = numRange
}
/**
* 标识为匿名类的主类调用对象
*
@@ -247,6 +269,8 @@ class NameConditions @PublishedApi internal constructor() {
if (cdsEndsWith != null) cdsEndsWith?.apply { conditions = conditions && it.endsWith(first, second) }
if (cdsContains != null) cdsContains?.apply { conditions = conditions && it.contains(first, second) }
if (cdsMatches != null) cdsMatches?.apply { conditions = conditions && it.matches(regex = this) }
if (cdsLength >= 0) conditions = conditions && it.length == cdsLength
if (cdsLengthRange != IntRange.EMPTY) conditions = conditions && it.length in cdsLengthRange
}
return conditions
}
@@ -265,6 +289,8 @@ class NameConditions @PublishedApi internal constructor() {
if (cdsEndsWith != null) cdsEndsWith?.apply { conditions += "<EndsWith:[suffix: $first, isIgnoreCase: $second]> " }
if (cdsContains != null) cdsContains?.apply { conditions += "<Contains:[other: $first, isIgnoreCase: $second]> " }
if (cdsMatches != null) cdsMatches?.apply { conditions += "<Matches:[regex: $this]> " }
if (cdsLength >= 0) conditions += "<Length:[num: $cdsLength]> "
if (cdsLengthRange != IntRange.EMPTY) conditions += "<LengthRange:[numRange: $cdsLengthRange]> "
return "[${conditions.trim()}]"
}
}