Created
April 20, 2020 19:01
-
-
Save DRSchlaubi/c9531cb5ac82a00a3e29c0cc10a0e224 to your computer and use it in GitHub Desktop.
file fixer 3001
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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