Skip to content

Instantly share code, notes, and snippets.

@L-Briand
Last active September 16, 2024 13:14
Show Gist options
  • Save L-Briand/0797a11b2f97a5111502bc3a385fc6b4 to your computer and use it in GitHub Desktop.
Save L-Briand/0797a11b2f97a5111502bc3a385fc6b4 to your computer and use it in GitHub Desktop.
/**
* Transform the given dotted string [str] to a sequence of string.
* Example: `"key.value"` -> `["key", "value"]`
*
* - All values are trimmed of whitespaces. `" a. b .c "` -> `["a", "b", "c"]`
* - An empty point `"."`, multiple points `".."` produces nothing. -> `[]`
* - Leading point(s) `".key"`, `"..key"` or trailing point(s) `"key."`, `"key.."` are omitted -> `["key"]`
* - Multipoint between elements `"key..value"` are like single point -> `["key", "value"]`
*
* @param str The dotted string to parse
* @return A sequence of trimmed strings separated by dots.
*/
@Suppress("NOTHING_TO_INLINE")
internal inline fun tokenizeDottedString(str: CharSequence): Sequence<String> = sequence {
if(str.isEmpty()) return@sequence
var index = 0
// dismiss empty accessor
while(index < str.length && (str[index] == '.' || str[index].isWhitespace())) index += 1
if(index == str.length) return@sequence
var wordStart: Int = -1
var wordEnd = 0
while (index < str.length) {
if(str[index] == '.') {
if(wordStart != -1) yield(str.substring(wordStart, wordEnd))
wordStart = -1
} else if(!str[index].isWhitespace()) {
if(wordStart == -1) wordStart = index
wordEnd = index + 1
}
index++
}
if(wordStart != -1) yield(str.substring(wordStart, wordEnd))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment