diff --git a/src/base/path.lua b/src/base/path.lua index 713c13af..91fb00d3 100644 --- a/src/base/path.lua +++ b/src/base/path.lua @@ -237,30 +237,3 @@ return p:match("^(.*)"..ext.."$")..newext end - - --- --- Converts from a simple wildcard syntax, where * is "match any" --- and ** is "match recursive", to the corresponding Lua pattern. --- --- @param pattern --- The wildcard pattern to convert. --- @returns --- The corresponding Lua pattern. --- - - function path.wildcards(pattern) - -- Escape characters that have special meanings in Lua patterns - pattern = pattern:gsub("([%+%.%-%^%$%(%)%%])", "%%%1") - - -- Replace wildcard patterns with special placeholders so I don't - -- have competing star replacements to worry about - pattern = pattern:gsub("%*%*", "\001") - pattern = pattern:gsub("%*", "\002") - - -- Replace the placeholders with their Lua patterns - pattern = pattern:gsub("\001", ".*") - pattern = pattern:gsub("\002", "[^/]*") - - return pattern - end diff --git a/src/host/path_wildcards.c b/src/host/path_wildcards.c new file mode 100644 index 00000000..c9c64c3f --- /dev/null +++ b/src/host/path_wildcards.c @@ -0,0 +1,81 @@ +/** + * \file path_wildcards.c + * \brief Converts from a simple wildcard syntax to the corresponding Lua pattern. + * \author Copyright (c) 2015 Tom van Dijck, Jason Perkins and the Premake project + */ + +#include "premake.h" +#include +#include + +/* +--Converts from a simple wildcard syntax, where * is "match any" +-- and ** is "match recursive", to the corresponding Lua pattern. +-- +-- @param pattern +-- The wildcard pattern to convert. +-- @returns +-- The corresponding Lua pattern. +*/ +int path_wildcards(lua_State* L) +{ + size_t length, i; + const char* input; + char buffer[0x4000]; + char* output; + + input = luaL_checklstring(L, 1, &length); + output = buffer; + + for (i = 0; i < length; ++i) + { + char c = input[i]; + switch (c) + { + case '+': + case '.': + case '-': + case '^': + case '$': + case '(': + case ')': + case '%': + *(output++) = '%'; + *(output++) = c; + break; + + case '*': + if ((i + 1) < length && input[i + 1] == '*') + { + i++; // skip the next character. + *(output++) = '.'; + *(output++) = '*'; + } + else + { + *(output++) = '['; + *(output++) = '^'; + *(output++) = '/'; + *(output++) = ']'; + *(output++) = '*'; + } + break; + + default: + *(output++) = c; + break; + } + + if (output >= buffer + sizeof(buffer)) + { + lua_pushstring(L, "Wildcards expansion too big."); + lua_error(L); + return 0; + } + } + + *(output++) = '\0'; + + lua_pushstring(L, buffer); + return 1; +} diff --git a/src/host/premake.c b/src/host/premake.c index 2be283cd..88c74d23 100644 --- a/src/host/premake.c +++ b/src/host/premake.c @@ -49,6 +49,7 @@ static const luaL_Reg path_functions[] = { { "join", path_join }, { "normalize", path_normalize }, { "translate", path_translate }, + { "wildcards", path_wildcards }, { NULL, NULL } }; diff --git a/src/host/premake.h b/src/host/premake.h index edbd9600..8b48f021 100644 --- a/src/host/premake.h +++ b/src/host/premake.h @@ -95,6 +95,7 @@ int path_isabsolute(lua_State* L); int path_join(lua_State* L); int path_normalize(lua_State* L); int path_translate(lua_State* L); +int path_wildcards(lua_State* L); int os_chdir(lua_State* L); int os_chmod(lua_State* L); int os_copyfile(lua_State* L);