mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-04-22 18:53:16 +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 class FileSystem {
|
||||
|
||||
// Collapse the string into its component parts, removing ..'s
|
||||
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()) {
|
||||
// . is redundant
|
||||
// ... and more are treated as .
|
||||
@ -450,7 +452,7 @@ public class FileSystem {
|
||||
}
|
||||
} else if (part.length() >= 255) {
|
||||
// If part length > 255 and it is the last part
|
||||
outputParts.addLast(part.substring(0, 255));
|
||||
outputParts.addLast(part.substring(0, 255).strip());
|
||||
} else {
|
||||
// Anything else we add to the stack
|
||||
outputParts.addLast(part);
|
||||
|
@ -12,6 +12,8 @@ import dan200.computercraft.core.TestFiles;
|
||||
import dan200.computercraft.core.apis.ObjectWrapper;
|
||||
import dan200.computercraft.core.apis.handles.EncodedWritableHandle;
|
||||
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.IOException;
|
||||
@ -74,4 +76,21 @@ public class FileSystemTest {
|
||||
var err = assertThrows(LuaException.class, () -> wrapper.call("write", "Tiny line"));
|
||||
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…
x
Reference in New Issue
Block a user