a `contains` on `List<String>` with `caseIns...
# stdlib
e
a
contains
on
List<String>
with
caseInsensitive: Boolean
?
c
list.any { it.lowerCase() == value }
e
I know, but this is the stdlib
c
wouldn’t expect a fairly limited use-case method such as “contains case insensitive” to be in stdlib. All kinds of permutations - contains matching regex, contains prefix, and so forth. All readily doable with minimal code today.
e
we should not be adding new case sensitivity APIs that don't take locale into consideration Locale is currently JVM-only thus, not a great fit for stdlib currently IMO
also if this is something you're repeating, it would be better to use
contains
on a (case-folded)
Set<String>
c
list.any { it.equals(value, ignoreCase = true) }
is probably faster / uses less memory
👍 2
k
Not only faster, but probably more correct (for certain non-English characters)
👍 1
e
equals(ignoreCase = true)
does not handle some non-English characters well (most notably Turkic languages)
for example,
Copy code
import java.text.Collator
import java.util.Locale

Collator.getInstance(Locale("tr")).apply { strength = Collator.SECONDARY }.compare("I", "i") // -1, because these are not case-insensitive equal in Turkish
Collator.getInstance(Locale("tr")).apply { strength = Collator.SECONDARY }.compare("İ", "i") // 0, because these are case-insensitive equal in Turkish
Collator.getInstance(Locale("de")).apply { strength = Collator.SECONDARY }.compare("SS", "ß") // 0, because these are case-insensitive equal in German
equals(ignoreCase = true)
has no way to get any of these cases right. you should really consider it as an ASCII-only API
👍 2
e
ok, got it
e
if you are doing it repeatedly, you can pre-compute
Collator.getCollationKey
, which will be more efficient than doing case-insensitive comparisons repeatedly. for example,
Copy code
val collationKeys = buildSet { listOfStrings.mapTo(this) { collator.getCollationKey(it) } }

collator.getCollationKey(string1) in collationKeys
collator.getCollationKey(string2) in collationKeys
// etc.
e
No, I just had to do it a couple of time in a short amount of time
229 Views