Zyle Moore
05/06/2024, 12:21 AMinline fun Char.toRange(): CharRange =
this..this
@Test
fun `toRange creates a range with a single value`() {
val range = 'Z'.toRange()
assertEquals('Z', range.first)
assertEquals('Z', range.last)
}
Zyle Moore
05/06/2024, 12:37 AM'Z'..'Z'
is that I have to specify the value twice.
Making a val Char.range: CharRange
would get me almost as short as the above, but it feels more appropriate for it to be a functionSzymon Jeziorski
05/06/2024, 9:28 AMtoRange
extension is as good as you can get avoiding variable duplicationZyle Moore
05/06/2024, 9:05 PMZyle Moore
05/06/2024, 9:18 PMval asciiRanges = listOf(
'a'..'z',
'0'..'9',
'-'.range,
'_'.range,
)
val hostnameRange: List<Char> = asciiRanges.flatten()
inline fun String.inHostnameRange(): Boolean =
all(hostnameRange::contains)
Szymon Jeziorski
05/06/2024, 10:49 PM+
operator like so:
val hostnameChars: List<Char> = ('a'..'z') + ('0'..'9') + '_' + '-'
also, in your case Set
might be more appropriate then List
since you're only checking whether it contains certain elements.
To achieve the same but with set you could either use above and convert it to set, or you could use set builder:
val hostnameChars: Set<Char> = buildSet {
this += 'a'..'z'
this += '0'..'9'
this += '_'
this += '-'
}
Klitos Kyriacou
05/07/2024, 11:48 AMZyle Moore
05/08/2024, 7:35 PMZyle Moore
05/08/2024, 7:37 PM