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:
SquidDev 2020-11-28 11:41:03 +00:00
parent 04f9644ae7
commit d4199064ae
3 changed files with 31 additions and 15 deletions

View File

@ -5,6 +5,7 @@
*/
package dan200.computercraft.core.apis;
import dan200.computercraft.api.lua.IArguments;
import dan200.computercraft.api.lua.ILuaAPI;
import dan200.computercraft.api.lua.LuaException;
import dan200.computercraft.api.lua.LuaFunction;
@ -83,17 +84,28 @@ public final String[] list( String path ) throws LuaException
}
/**
* Combines two parts of a path into one full path, adding separators as
* 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.
*/
@LuaFunction
public final String combine( String pathA, String pathB )
public final String combine( IArguments arguments ) throws LuaException
{
return 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 );
}
/**
@ -385,8 +397,8 @@ public final Object[] getDrive( String path ) throws LuaException
*
* @param path The path to check the free space for.
* @return The amount of free space available, in bytes.
* @cc.treturn number|"unlimited" The amount of free space available, in bytes, or "unlimited".
* @throws LuaException If the path doesn't exist.
* @cc.treturn number|"unlimited" The amount of free space available, in bytes, or "unlimited".
*/
@LuaFunction
public final Object getFreeSpace( String path ) throws LuaException

View File

@ -98,7 +98,7 @@ public synchronized void unmount( String path )
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 );
@ -479,7 +479,7 @@ private static String sanitizePath( String path )
private static final Pattern threeDotsPattern = Pattern.compile( "^\\.{3,}$" );
private static String sanitizePath( String path, boolean allowWildcards )
public static String sanitizePath( String path, boolean allowWildcards )
{
// Allow windowsy slashes
path = path.replace( '\\', '/' );

View File

@ -39,15 +39,19 @@ describe("The fs library", function()
end)
end)
describe("fs.list", function()
it("fails on files", function()
expect.error(fs.list, "rom/startup.lua"):eq("/rom/startup.lua: Not a directory")
expect.error(fs.list, "startup.lua"):eq("/startup.lua: Not a directory")
describe("fs.combine", function()
it("removes . and ..", function()
expect(fs.combine("./a/b")):eq("a/b")
expect(fs.combine("a/b", "../c")):eq("a/c")
expect(fs.combine("a", "../c")):eq("c")
expect(fs.combine("a", "../../c")):eq("../c")
end)
it("fails on non-existent nodes", function()
expect.error(fs.list, "rom/x"):eq("/rom/x: Not a directory")
expect.error(fs.list, "x"):eq("/x: Not a directory")
it("combines empty paths", function()
expect(fs.combine("a")):eq("a")
expect(fs.combine("a", "")):eq("a")
expect(fs.combine("", "a")):eq("a")
expect(fs.combine("a", "", "b", "c")):eq("a/b/c")
end)
end)