Merge pull request #277 from Blizzard/path-wildcards-to-c

path.wildcards -> C
This commit is contained in:
Tom van Dijck 2015-10-05 09:37:49 -07:00
commit b5c4108129
4 changed files with 83 additions and 27 deletions

View File

@ -237,30 +237,3 @@
return p:match("^(.*)"..ext.."$")..newext return p:match("^(.*)"..ext.."$")..newext
end 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

81
src/host/path_wildcards.c Normal file
View File

@ -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 <string.h>
#include <malloc.h>
/*
--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;
}

View File

@ -49,6 +49,7 @@ static const luaL_Reg path_functions[] = {
{ "join", path_join }, { "join", path_join },
{ "normalize", path_normalize }, { "normalize", path_normalize },
{ "translate", path_translate }, { "translate", path_translate },
{ "wildcards", path_wildcards },
{ NULL, NULL } { NULL, NULL }
}; };

View File

@ -95,6 +95,7 @@ int path_isabsolute(lua_State* L);
int path_join(lua_State* L); int path_join(lua_State* L);
int path_normalize(lua_State* L); int path_normalize(lua_State* L);
int path_translate(lua_State* L); int path_translate(lua_State* L);
int path_wildcards(lua_State* L);
int os_chdir(lua_State* L); int os_chdir(lua_State* L);
int os_chmod(lua_State* L); int os_chmod(lua_State* L);
int os_copyfile(lua_State* L); int os_copyfile(lua_State* L);