11package me.grison.aoc
22
3+ import java.util.*
4+
35/* * Returns the occurrences of `c`. */
46fun String.occurrences (c : Char ) = count { it == c }
57
@@ -73,6 +75,7 @@ fun String.afterLast(s: String) = split(s).last()
7375fun String.allInts (includeNegative : Boolean = true): List <Int > =
7476 (if (includeNegative) " (-?\\ d+)" else " (\\ d+)" ).regex().findAll(this ).map { it.value.toInt() }.toList()
7577
78+ fun String.allPositiveInts (): List <Int > = this .allInts(false )
7679/* * Returns all long found in this string. */
7780fun String.allLongs (includeNegative : Boolean = true): List <Long > =
7881 (if (includeNegative) " (-?\\ d+)" else " (\\ d+)" ).regex().findAll(this ).map { it.value.toLong() }.toList()
@@ -101,6 +104,9 @@ fun String.except(c: Char) = replace(c + "", "")
101104/* * Returns the string before `str`. */
102105fun String.before (str : String ) = split(str)[0 ]
103106
107+ fun String.intRange () = this .normalSplit(" -" ).let { IntRange (it[0 ].toInt(), it[1 ].toInt()) }
108+ fun String.longRange () = this .normalSplit(" -" ).let { LongRange (it[0 ].toLong(), it[1 ].toLong()) }
109+
104110/* * Returns the string after `str`. */
105111fun String.after (str : String ) = split(str)[1 ]
106112
@@ -114,8 +120,8 @@ fun String.words() = normalSplit(" ")
114120fun String.set () = stringList().toSet()
115121
116122fun String .`is` (s : String ) = this == s
117- fun String.upper () = toUpperCase( )
118- fun String.lower () = toLowerCase( )
123+ fun String.upper () = uppercase( Locale .getDefault() )
124+ fun String.lower () = lowercase( Locale .getDefault() )
119125
120126fun String.isLower () = all { it.isLowerCase() }
121127fun String.isUpper () = all { it.isUpperCase() }
@@ -142,5 +148,5 @@ fun String.split(at: Int) = listOf(this.take(at), this.drop(at))
142148fun String.inTwo () = this .split(this .length / 2 )
143149
144150fun foo () {
145- " " .capitalize()
151+ " " .replaceFirstChar { if (it.isLowerCase()) it.titlecase( Locale .getDefault()) else it.toString() }
146152}
0 commit comments