string_endswith.c 583 B

12345678910111213141516171819202122232425262728
  1. /**
  2. * \file string_endswith.c
  3. * \brief Determines if a string ends with the given sequence.
  4. * \author Copyright (c) 2002-2009 Jason Perkins and the Premake project
  5. */
  6. #include "premake.h"
  7. #include <string.h>
  8. int string_endswith(lua_State* L)
  9. {
  10. const char* haystack = luaL_optstring(L, 1, NULL);
  11. const char* needle = luaL_optstring(L, 2, NULL);
  12. if (haystack && needle)
  13. {
  14. int hlen = (int)strlen(haystack);
  15. int nlen = (int)strlen(needle);
  16. if (hlen >= nlen)
  17. {
  18. lua_pushboolean(L, strcmp(haystack + hlen - nlen, needle) == 0);
  19. return 1;
  20. }
  21. }
  22. return 0;
  23. }