diff --git a/patchwork.md b/patchwork.md index 910d99b2f..76fdf3edf 100644 --- a/patchwork.md +++ b/patchwork.md @@ -374,3 +374,17 @@ or numbers. Fixes #591 ``` + +``` +d4199064ae5ae8023c589f80f12d94e1c6bbc2b5 + +Make fs.combine accept multiple arguments + +Means we can now do fs.combine("a", "b", "c"). Of course, one may just +write "a/b/c" in this case, but it's definitely useful elsewhere. + +This is /technically/ a breaking change as fs.combine(a, b:gsub(...)) +will no longer function (as gsub returns multiple arguments). However, +I've done a quick search through GH and my Pastebin archives and can't +find any programs which would break. Fingers crossed. +``` diff --git a/src/main/java/dan200/computercraft/core/apis/FSAPI.java b/src/main/java/dan200/computercraft/core/apis/FSAPI.java index a0dd8241c..1dd4dd661 100644 --- a/src/main/java/dan200/computercraft/core/apis/FSAPI.java +++ b/src/main/java/dan200/computercraft/core/apis/FSAPI.java @@ -17,6 +17,8 @@ import java.util.Map; import java.util.OptionalLong; import java.util.function.Function; + +import dan200.computercraft.api.lua.IArguments; import dan200.computercraft.api.lua.ILuaAPI; import dan200.computercraft.api.lua.LuaException; import dan200.computercraft.api.lua.LuaFunction; @@ -75,15 +77,27 @@ public class FSAPI implements ILuaAPI { } /** - * Combines two parts of a path into one full path, adding separators as needed. + * Combines several parts of a path into one full path, adding separators as + * needed * - * @param pathA The first part of the path. For example, a parent directory path. - * @param pathB The second part of the path. For example, a file name. + * @param arguments The paths to combine. * @return The new path, with separators added between parts as needed. + * @cc.tparam string path The first part of the path. For example, a parent directory path. + * @cc.tparam string ... Additional parts of the path to combine. + * @throws LuaException On argument errors. */ @LuaFunction - public final String combine(String pathA, String pathB) { - return this.fileSystem.combine(pathA, pathB); + public final String combine( IArguments arguments ) throws LuaException { + StringBuilder result = new StringBuilder(); + result.append(FileSystem.sanitizePath(arguments.getString(0), true)); + + for (int i = 1, n = arguments.count(); i < n; i++) { + String part = FileSystem.sanitizePath(arguments.getString(i), true); + if (result.length() != 0 && !part.isEmpty()) result.append('/'); + result.append(part); + } + + return FileSystem.sanitizePath(result.toString(), true); } /** diff --git a/src/main/java/dan200/computercraft/core/filesystem/FileSystem.java b/src/main/java/dan200/computercraft/core/filesystem/FileSystem.java index 7cd57d661..ebb9390cb 100644 --- a/src/main/java/dan200/computercraft/core/filesystem/FileSystem.java +++ b/src/main/java/dan200/computercraft/core/filesystem/FileSystem.java @@ -76,7 +76,7 @@ public class FileSystem { this.mounts.put(location, wrapper); } - private static String sanitizePath(String path, boolean allowWildcards) { + public static String sanitizePath(String path, boolean allowWildcards) { // Allow windowsy slashes path = path.replace('\\', '/'); @@ -231,7 +231,7 @@ public class FileSystem { this.mounts.remove(sanitizePath(path)); } - public synchronized String combine(String path, String childPath) { + public String combine( String path, String childPath ) { path = sanitizePath(path, true); childPath = sanitizePath(childPath, true);