mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2024-06-26 07:03:22 +00:00
Trim spaces from filesystem paths
I kinda hate this, but not sure what else to do. It might be worth rewriting sanitizePath in the future to loop through the string once, but I think this is good enough for now.
This commit is contained in:
parent
ee2670d53b
commit
b8fce1eecc
|
@ -429,7 +429,9 @@ public static String sanitizePath(String path, boolean allowWildcards) {
|
||||||
|
|
||||||
// Collapse the string into its component parts, removing ..'s
|
// Collapse the string into its component parts, removing ..'s
|
||||||
var outputParts = new ArrayDeque<String>();
|
var outputParts = new ArrayDeque<String>();
|
||||||
for (var part : Splitter.on('/').split(path)) {
|
for (var fullPart : Splitter.on('/').split(path)) {
|
||||||
|
var part = fullPart.strip();
|
||||||
|
|
||||||
if (part.isEmpty() || part.equals(".") || threeDotsPattern.matcher(part).matches()) {
|
if (part.isEmpty() || part.equals(".") || threeDotsPattern.matcher(part).matches()) {
|
||||||
// . is redundant
|
// . is redundant
|
||||||
// ... and more are treated as .
|
// ... and more are treated as .
|
||||||
|
@ -450,7 +452,7 @@ public static String sanitizePath(String path, boolean allowWildcards) {
|
||||||
}
|
}
|
||||||
} else if (part.length() >= 255) {
|
} else if (part.length() >= 255) {
|
||||||
// If part length > 255 and it is the last part
|
// If part length > 255 and it is the last part
|
||||||
outputParts.addLast(part.substring(0, 255));
|
outputParts.addLast(part.substring(0, 255).strip());
|
||||||
} else {
|
} else {
|
||||||
// Anything else we add to the stack
|
// Anything else we add to the stack
|
||||||
outputParts.addLast(part);
|
outputParts.addLast(part);
|
||||||
|
|
|
@ -12,6 +12,8 @@
|
||||||
import dan200.computercraft.core.apis.ObjectWrapper;
|
import dan200.computercraft.core.apis.ObjectWrapper;
|
||||||
import dan200.computercraft.core.apis.handles.EncodedWritableHandle;
|
import dan200.computercraft.core.apis.handles.EncodedWritableHandle;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.junit.jupiter.params.ParameterizedTest;
|
||||||
|
import org.junit.jupiter.params.provider.MethodSource;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
@ -74,4 +76,21 @@ public void testUnmountCloses() throws FileSystemException {
|
||||||
var err = assertThrows(LuaException.class, () -> wrapper.call("write", "Tiny line"));
|
var err = assertThrows(LuaException.class, () -> wrapper.call("write", "Tiny line"));
|
||||||
assertEquals("attempt to use a closed file", err.getMessage());
|
assertEquals("attempt to use a closed file", err.getMessage());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ParameterizedTest(name = "{0}")
|
||||||
|
@MethodSource("sanitiseCases")
|
||||||
|
public void testSanitize(String input, String output) {
|
||||||
|
assertEquals(output, FileSystem.sanitizePath(input, false));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String[][] sanitiseCases() {
|
||||||
|
return new String[][]{
|
||||||
|
new String[]{ "a//b", "a/b" },
|
||||||
|
new String[]{ "a/./b", "a/b" },
|
||||||
|
new String[]{ "a/../b", "b" },
|
||||||
|
new String[]{ "a/.../b", "a/b" },
|
||||||
|
new String[]{ " a ", "a" },
|
||||||
|
new String[]{ "a b c", "a b c" },
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user