1
0
mirror of https://github.com/SquidDev-CC/CC-Tweaked synced 2025-08-28 08:12:18 +00:00

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.
This commit is contained in:
Merith-TK 2021-05-07 11:39:14 -07:00
parent 717686d855
commit f1176af9d1
3 changed files with 35 additions and 7 deletions

View File

@ -374,3 +374,17 @@ or numbers.
Fixes #591 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.
```

View File

@ -17,6 +17,8 @@ import java.util.Map;
import java.util.OptionalLong; import java.util.OptionalLong;
import java.util.function.Function; import java.util.function.Function;
import dan200.computercraft.api.lua.IArguments;
import dan200.computercraft.api.lua.ILuaAPI; import dan200.computercraft.api.lua.ILuaAPI;
import dan200.computercraft.api.lua.LuaException; import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.LuaFunction; 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 arguments The paths to combine.
* @param pathB The second part of the path. For example, a file name.
* @return The new path, with separators added between parts as needed. * @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 @LuaFunction
public final String combine(String pathA, String pathB) { public final String combine( IArguments arguments ) throws LuaException {
return this.fileSystem.combine(pathA, pathB); 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);
} }
/** /**

View File

@ -76,7 +76,7 @@ public class FileSystem {
this.mounts.put(location, wrapper); this.mounts.put(location, wrapper);
} }
private static String sanitizePath(String path, boolean allowWildcards) { public static String sanitizePath(String path, boolean allowWildcards) {
// Allow windowsy slashes // Allow windowsy slashes
path = path.replace('\\', '/'); path = path.replace('\\', '/');
@ -231,7 +231,7 @@ public class FileSystem {
this.mounts.remove(sanitizePath(path)); this.mounts.remove(sanitizePath(path));
} }
public synchronized String combine(String path, String childPath) { public String combine( String path, String childPath ) {
path = sanitizePath(path, true); path = sanitizePath(path, true);
childPath = sanitizePath(childPath, true); childPath = sanitizePath(childPath, true);