Port string.startswith() to native code
This commit is contained in:
parent
082dc318e4
commit
2774e7796a
@ -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
|
||||
|
@ -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 }
|
||||
};
|
||||
|
||||
|
@ -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);
|
||||
|
24
src/host/string_startswith.c
Normal file
24
src/host/string_startswith.c
Normal 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;
|
||||
}
|
Loading…
Reference in New Issue
Block a user