Skip to content

Commit 3fc9495

Browse files
committed
feat: notations for file system paths
1 parent 2bfbd1d commit 3fc9495

File tree

6 files changed

+101
-35
lines changed

6 files changed

+101
-35
lines changed

src/main/kotlin/BaseStringNotation.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ abstract class BaseStringNotation(private val splitAt: Regex): StringNotation {
1212
protected open fun transformPartAfterParse(index: Int, part: String) = part
1313

1414
override fun parse(sourceString: String): Word =
15-
Word(sourceString.split(splitAt).asSequence().filter(String::isNotBlank).mapIndexed(::transformPartAfterParse))
15+
Word(sourceString.split(splitAt).asSequence().mapIndexed(::transformPartAfterParse))
1616

1717
/**
1818
* Allows to transform a part before it is being printed. The default implementation does not modify the part in any way.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package de.joshuagleitze.stringnotation
2+
3+
/**
4+
* A notation for paths on a Unix file system. [Parsing][StringNotation.parse] will recognise all substrings that are
5+
* separated by `/` as a [part][Word.parts]. [Printing][StringNotation.print] will print the parts separated by `/`
6+
* after removing `\u0000` (ASCII: `NUL`) and `\u0057` (ASCII: slash) characters from them.
7+
*
8+
* [Printed][StringNotation.print] paths will not start with an additional `/`. To print an absolute path, include `""` (the empty string) as the first part in the printed word.
9+
*/
10+
object UnixPath : BaseStringNotation(Regex("/")) {
11+
private val invalidChars = Regex("[\u0000/]+")
12+
override fun transformPartToPrint(index: Int, part: String) = part.replace(invalidChars, "")
13+
override fun printBeforeInnerPart(index: Int, part: String) = "/"
14+
}
15+
16+
/**
17+
* A notation for paths on a Windows file system. [Parsing][StringNotation.parse] will recognise all substrings that are
18+
* separated by `\` as a [part][Word.parts]. [Printing][StringNotation.print] will print the parts separated by `\`
19+
* after removing the following characters from them:
20+
* * ASCII control characters
21+
* * `<`, `>`, `:`, `"`, `/`, `\`, `|`, `?`, `*`
22+
*
23+
* To allow printing paths that start with a drive letter, the notation will not strip a `:` from the first part if it’s the last character.
24+
*/
25+
object WindowsPath : BaseStringNotation(Regex("\\\\")) {
26+
private val invalidChars = Regex("[\\p{Cntrl}<>:\"/\\\\|?*]+")
27+
override fun transformPartToPrint(index: Int, part: String): String {
28+
val replaced = part.replace(invalidChars, "")
29+
return if (index == 0 && part.endsWith(":")) {
30+
"$replaced:"
31+
} else replaced
32+
}
33+
override fun printBeforeInnerPart(index: Int, part: String) = "\\"
34+
}

src/main/kotlin/JavaNotations.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import javax.lang.model.SourceVersion
1111
* Allowed characters are determined using [Character.isJavaIdentifierStart] and [Character.isJavaIdentifierPart]. Keywords are detected
1212
* using [SourceVersion.isKeyword].
1313
*/
14-
data object JavaTypeName : StringNotation by UpperCamelCase {
14+
object JavaTypeName : StringNotation by UpperCamelCase {
1515
override fun print(word: Word) = UpperCamelCase.print(
1616
Word(word.parts.mapIndexed { index, wordPart ->
1717
if (index == 0) wordPart.keepOnlyJavaIdentifierChars()

src/main/kotlin/Notations.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ object LowerCamelCase : BaseStringNotation(camelCaseSplitRegex) {
3030
/**
3131
* The `SCREAMING_SNAKE_CASE` notation.
3232
*/
33-
object ScreamingSnakeCase : BaseStringNotation(Regex("_")) {
33+
object ScreamingSnakeCase : BaseStringNotation(Regex("(?<!^)_")) {
3434
override fun transformPartAfterParse(index: Int, part: String) = part.lowercase(Locale.ROOT)
3535

3636
override fun printBeforeInnerPart(index: Int, part: String) = "_"
@@ -41,7 +41,7 @@ object ScreamingSnakeCase : BaseStringNotation(Regex("_")) {
4141
/**
4242
* The `snake_case` notation.
4343
*/
44-
object SnakeCase: BaseStringNotation(Regex("_")) {
44+
object SnakeCase: BaseStringNotation(Regex("(?<!^)_")) {
4545
override fun printBeforeInnerPart(index: Int, part: String) = "_"
4646
}
4747

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package de.joshuagleitze.stringnotation
2+
3+
class UnixPathTest : BaseNotationTest(
4+
notation = UnixPath,
5+
unchangedWords = listOf(
6+
"/home/user/some/file" to Word("", "home", "user", "some", "file"),
7+
"a/relative/path" to Word("a", "relative", "path")
8+
),
9+
printOnlyWords = listOf(
10+
Word("", "home", "null\u0000") to "/home/null",
11+
Word("", "home", "user/some/file") to "/home/usersomefile",
12+
)
13+
)
14+
15+
class WindowsPathTest : BaseNotationTest(
16+
notation = WindowsPath,
17+
unchangedWords = listOf(
18+
"C:\\Users\\user\\some\\file" to Word("C:", "Users", "user", "some", "file"),
19+
"a\\relative\\path" to Word("a", "relative", "path")
20+
),
21+
printOnlyWords = listOf(
22+
*('\u0000'..'\u001F').map { controlChar ->
23+
Word("C:", "bad${controlChar}File") to "C:\\badFile"
24+
}.toTypedArray(),
25+
Word("C:", "bad\u007FFile") to "C:\\badFile",
26+
Word("C:", "bad<File>") to "C:\\badFile",
27+
)
28+
)

src/test/kotlin/NotationsTest.kt

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,46 @@
11
package de.joshuagleitze.stringnotation
22

3-
class UpperCamelCaseTest: BaseNotationTest(
4-
notation = UpperCamelCase,
5-
unchangedWords = listOf("ImInUpperCamelCase" to Word("im", "in", "upper", "camel", "case")),
6-
printOnlyWords = listOf(
7-
Word("removes", "upperCase") to "RemovesUppercase"
8-
)
3+
class UpperCamelCaseTest : BaseNotationTest(
4+
notation = UpperCamelCase,
5+
unchangedWords = listOf("ImInUpperCamelCase" to Word("im", "in", "upper", "camel", "case")),
6+
printOnlyWords = listOf(
7+
Word("removes", "upperCase") to "RemovesUppercase"
8+
)
99
)
1010

11-
class LowerCamelCaseTest: BaseNotationTest(
12-
notation = LowerCamelCase,
13-
unchangedWords = listOf("imInLowerCamelCase" to Word("im", "in", "lower", "camel", "case")),
14-
printOnlyWords = listOf(
15-
Word("removes", "upperCase") to "removesUppercase"
16-
)
11+
class LowerCamelCaseTest : BaseNotationTest(
12+
notation = LowerCamelCase,
13+
unchangedWords = listOf("imInLowerCamelCase" to Word("im", "in", "lower", "camel", "case")),
14+
printOnlyWords = listOf(
15+
Word("removes", "upperCase") to "removesUppercase"
16+
)
1717
)
1818

19-
class ScreamingSnakeCaseTest: BaseNotationTest(
20-
notation = ScreamingSnakeCase,
21-
unchangedWords = listOf("IM_IN_SCREAMING_SNAKE_CASE" to Word("im", "in", "screaming", "snake", "case")),
22-
parseOnlyWords = listOf("im_iN_sNAKe_cASE_with_CAPItals" to Word("im", "in", "snake", "case", "with", "capitals")),
23-
printOnlyWords = listOf(Word("im", "iN", "sNAKe", "cASE", "with", "CAPItals") to "IM_IN_SNAKE_CASE_WITH_CAPITALS")
19+
class ScreamingSnakeCaseTest : BaseNotationTest(
20+
notation = ScreamingSnakeCase,
21+
unchangedWords = listOf(
22+
"IM_IN_SCREAMING_SNAKE_CASE" to Word("im", "in", "screaming", "snake", "case"),
23+
"_I_HAVE_A_PREFIX" to Word("_i", "have", "a", "prefix")
24+
),
25+
parseOnlyWords = listOf("im_iN_sNAKe_cASE_with_CAPItals" to Word("im", "in", "snake", "case", "with", "capitals")),
26+
printOnlyWords = listOf(Word("im", "iN", "sNAKe", "cASE", "with", "CAPItals") to "IM_IN_SNAKE_CASE_WITH_CAPITALS")
2427
)
2528

26-
class SnakeCaseTest: BaseNotationTest(
27-
notation = SnakeCase,
28-
unchangedWords = listOf(
29-
"im_in_snake_case" to Word("im", "in", "snake", "case"),
30-
"im_iN_sNAKe_cASE_with_CAPItals" to Word("im", "iN", "sNAKe", "cASE", "with", "CAPItals")
31-
)
29+
class SnakeCaseTest : BaseNotationTest(
30+
notation = SnakeCase,
31+
unchangedWords = listOf(
32+
"im_in_snake_case" to Word("im", "in", "snake", "case"),
33+
"im_iN_sNAKe_cASE_with_CAPItals" to Word("im", "iN", "sNAKe", "cASE", "with", "CAPItals"),
34+
"_i_have_a_prefix" to Word("_i", "have", "a", "prefix")
35+
)
3236
)
3337

34-
class NormalWordsTest: BaseNotationTest(
35-
notation = NormalWords,
36-
unchangedWords = listOf("I’m using normal words noTation!" to Word("I’m", "using", "normal", "words", "noTation!")),
37-
parseOnlyWords = listOf(
38-
"I’m using tabs\nand\r other fancy whitespace!" to Word(
39-
"I’m", "using", "tabs", "and", "other", "fancy", "whitespace!"
40-
)
41-
)
38+
class NormalWordsTest : BaseNotationTest(
39+
notation = NormalWords,
40+
unchangedWords = listOf("I’m using normal words noTation!" to Word("I’m", "using", "normal", "words", "noTation!")),
41+
parseOnlyWords = listOf(
42+
"I’m using tabs\nand\r other fancy whitespace!" to Word(
43+
"I’m", "using", "tabs", "and", "other", "fancy", "whitespace!"
44+
)
45+
)
4246
)

0 commit comments

Comments
 (0)