Port string.startswith() to native code

This commit is contained in:
Jason Perkins 2013-11-26 19:10:53 -05:00
parent 082dc318e4
commit 2774e7796a
4 changed files with 26 additions and 9 deletions

View File

@ -61,12 +61,3 @@
end
return n
end
--
-- Returns true if `haystack` starts with the sequence `needle`.
--
function string.startswith(haystack, needle)
return (haystack:find(needle, 1, true) == 1)
end

View File

@ -69,6 +69,7 @@ static const luaL_Reg os_functions[] = {
static const luaL_Reg string_functions[] = {
{ "endswith", string_endswith },
{ "hash", string_hash },
{ "startswith", string_startswith },
{ NULL, NULL }
};

View File

@ -85,6 +85,7 @@ int os_stat(lua_State* L);
int os_uuid(lua_State* L);
int string_endswith(lua_State* L);
int string_hash(lua_State* L);
int string_startswith(lua_State* L);
/* Engine interface */
int premake_init(lua_State* L);

View File

@ -0,0 +1,24 @@
/**
* \file string_startswith.c
* \brief Determines if a string starts with the given sequence.
* \author Copyright (c) 2013 Jason Perkins and the Premake project
*/
#include "premake.h"
#include <string.h>
int string_startswith(lua_State* L)
{
const char* haystack = luaL_optstring(L, 1, NULL);
const char* needle = luaL_optstring(L, 2, NULL);
if (haystack && needle)
{
int nlen = strlen(needle);
lua_pushboolean(L, strncmp(haystack, needle, nlen) == 0);
return 1;
}
return 0;
}