aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/util/StringUtil.kt
blob: 50c53673fea96203bb5f43749b9f3dec61e5a8c7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package moe.nea.firmament.util

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

	fun String.camelWords(): Sequence<String> {
		return splitToSequence(camelWordStart)
	}

	private val camelWordStart = Regex("((?<=[a-z])(?=[A-Z]))| ")

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

	fun String.title() = replaceFirstChar { it.titlecase() }

	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()
	}

}