mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2024-12-12 19:20:29 +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:
parent
04f9644ae7
commit
d4199064ae
@ -5,6 +5,7 @@
|
|||||||
*/
|
*/
|
||||||
package dan200.computercraft.core.apis;
|
package dan200.computercraft.core.apis;
|
||||||
|
|
||||||
|
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;
|
||||||
@ -83,17 +84,28 @@ public class FSAPI implements ILuaAPI
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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.
|
* 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.
|
||||||
*/
|
*/
|
||||||
@LuaFunction
|
@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 class FSAPI implements ILuaAPI
|
|||||||
*
|
*
|
||||||
* @param path The path to check the free space for.
|
* @param path The path to check the free space for.
|
||||||
* @return The amount of free space available, in bytes.
|
* @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.
|
* @throws LuaException If the path doesn't exist.
|
||||||
|
* @cc.treturn number|"unlimited" The amount of free space available, in bytes, or "unlimited".
|
||||||
*/
|
*/
|
||||||
@LuaFunction
|
@LuaFunction
|
||||||
public final Object getFreeSpace( String path ) throws LuaException
|
public final Object getFreeSpace( String path ) throws LuaException
|
||||||
|
@ -98,7 +98,7 @@ public class FileSystem
|
|||||||
mounts.remove( sanitizePath( path ) );
|
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 );
|
||||||
@ -479,7 +479,7 @@ public class FileSystem
|
|||||||
|
|
||||||
private static final Pattern threeDotsPattern = Pattern.compile( "^\\.{3,}$" );
|
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
|
// Allow windowsy slashes
|
||||||
path = path.replace( '\\', '/' );
|
path = path.replace( '\\', '/' );
|
||||||
|
@ -39,15 +39,19 @@ describe("The fs library", function()
|
|||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
describe("fs.list", function()
|
describe("fs.combine", function()
|
||||||
it("fails on files", function()
|
it("removes . and ..", function()
|
||||||
expect.error(fs.list, "rom/startup.lua"):eq("/rom/startup.lua: Not a directory")
|
expect(fs.combine("./a/b")):eq("a/b")
|
||||||
expect.error(fs.list, "startup.lua"):eq("/startup.lua: Not a directory")
|
expect(fs.combine("a/b", "../c")):eq("a/c")
|
||||||
|
expect(fs.combine("a", "../c")):eq("c")
|
||||||
|
expect(fs.combine("a", "../../c")):eq("../c")
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it("fails on non-existent nodes", function()
|
it("combines empty paths", function()
|
||||||
expect.error(fs.list, "rom/x"):eq("/rom/x: Not a directory")
|
expect(fs.combine("a")):eq("a")
|
||||||
expect.error(fs.list, "x"):eq("/x: Not a directory")
|
expect(fs.combine("a", "")):eq("a")
|
||||||
|
expect(fs.combine("", "a")):eq("a")
|
||||||
|
expect(fs.combine("a", "", "b", "c")):eq("a/b/c")
|
||||||
end)
|
end)
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user