aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/util/StringUtil.kt
blob: 68e161a795b661fed0b13caddd5f7b41e913f554 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package moe.nea.firmament.util

object StringUtil {
	fun String.words(): Sequence<String> {
		return splitToSequence(" ") // TODO: better boundaries
	}

	fun parseIntWithComma(string: String): Int {
		return string.replace(",", "").toInt()
	}

	fun Iterable<String>.unwords() = joinToString(" ")
	fun nextLexicographicStringOfSameLength(string: String): String {
		val next = StringBuilder(string)
		while (next.lastOrNull() == Character.MAX_VALUE) next.setLength(next.length - 1)
		if (next.isEmpty()) return "" // There is no upper bound. Fall back to the empty string
		val lastIdx = next.indices.last
		next[lastIdx] = (next[lastIdx] + 1)
		return next.toString()
	}

}