Skip to content

Instantly share code, notes, and snippets.

@DRSchlaubi
Created April 20, 2020 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save DRSchlaubi/c9531cb5ac82a00a3e29c0cc10a0e224 to your computer and use it in GitHub Desktop.
Save DRSchlaubi/c9531cb5ac82a00a3e29c0cc10a0e224 to your computer and use it in GitHub Desktop.
file fixer 3001
import java.nio.file.Files
import java.nio.file.Paths
Files.walk(Paths.get("lib/"), Integer.MAX_VALUE).forEach {
if (!Files.isDirectory(it)) {
val oldName = it.toFile().name
val newName = if (oldName.isPotentialPascalCase) {
oldName.pascalCase()
} else oldName
Files.move(it, it.parent.resolveSibling(newName.toString()))
}
}
fun CharSequence.pascalCase() = StringBuilder(this).pascalCase()
tailrec fun StringBuilder.pascalCase(): CharSequence {
// Find next upper char
val index = getNextUpperCharacter()
if (index >= 0) {
// Insert underscore if needed
val newIndex = if (index != 0) {
insert(index, "_")
index + 1
} else index
// lowercase camel cased char
setCharAt(newIndex, this[newIndex].toLowerCase())
// Check for others
return pascalCase()
}
// No work to do
return this
}
tailrec fun CharSequence.getNextUpperCharacter(start: Int = 0): Int {
// Out of bounds
if (start > lastIndex) return -1
// First is camelcased
if (this[start].isUpperCase()) return start
// Continue search
return getNextUpperCharacter(start + 1)
}
val CharSequence.isPotentialPascalCase: Boolean
get() = any(Char::isUpperCase)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment