strlib.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /*
  2. ** strlib.c
  3. ** String library to LUA
  4. */
  5. char *rcs_strlib="$Id: strlib.c,v 1.2 1994/03/28 15:14:02 celes Exp celes $";
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <ctype.h>
  9. #include "mm.h"
  10. #include "lua.h"
  11. #include "lualib.h"
  12. /*
  13. ** Return the position of the first caracter of a substring into a string
  14. ** LUA interface:
  15. ** n = strfind (string, substring)
  16. */
  17. static void str_find (void)
  18. {
  19. char *s1, *s2, *f;
  20. lua_Object o1 = lua_getparam (1);
  21. lua_Object o2 = lua_getparam (2);
  22. if (!lua_isstring(o1) || !lua_isstring(o2))
  23. { lua_error ("incorrect arguments to function `strfind'"); return; }
  24. s1 = lua_getstring(o1);
  25. s2 = lua_getstring(o2);
  26. f = strstr(s1,s2);
  27. if (f != NULL)
  28. lua_pushnumber (f-s1+1);
  29. else
  30. lua_pushnil();
  31. }
  32. /*
  33. ** Return the string length
  34. ** LUA interface:
  35. ** n = strlen (string)
  36. */
  37. static void str_len (void)
  38. {
  39. lua_Object o = lua_getparam (1);
  40. if (!lua_isstring(o))
  41. { lua_error ("incorrect arguments to function `strlen'"); return; }
  42. lua_pushnumber(strlen(lua_getstring(o)));
  43. }
  44. /*
  45. ** Return the substring of a string, from start to end
  46. ** LUA interface:
  47. ** substring = strsub (string, start, end)
  48. */
  49. static void str_sub (void)
  50. {
  51. int start, end;
  52. char *s;
  53. lua_Object o1 = lua_getparam (1);
  54. lua_Object o2 = lua_getparam (2);
  55. lua_Object o3 = lua_getparam (3);
  56. if (!lua_isstring(o1) || !lua_isnumber(o2))
  57. { lua_error ("incorrect arguments to function `strsub'"); return; }
  58. if (o3 != NULL && !lua_isnumber(o3))
  59. { lua_error ("incorrect third argument to function `strsub'"); return; }
  60. s = lua_copystring(o1);
  61. start = lua_getnumber (o2);
  62. end = o3 == NULL ? strlen(s) : lua_getnumber (o3);
  63. if (end < start || start < 1 || end > strlen(s))
  64. lua_pushstring("");
  65. else
  66. {
  67. s[end] = 0;
  68. lua_pushstring (&s[start-1]);
  69. }
  70. free (s);
  71. }
  72. /*
  73. ** Convert a string to lower case.
  74. ** LUA interface:
  75. ** lowercase = strlower (string)
  76. */
  77. static void str_lower (void)
  78. {
  79. char *s, *c;
  80. lua_Object o = lua_getparam (1);
  81. if (!lua_isstring(o))
  82. { lua_error ("incorrect arguments to function `strlower'"); return; }
  83. c = s = strdup(lua_getstring(o));
  84. while (*c != 0)
  85. {
  86. *c = tolower(*c);
  87. c++;
  88. }
  89. lua_pushstring(s);
  90. free(s);
  91. }
  92. /*
  93. ** Convert a string to upper case.
  94. ** LUA interface:
  95. ** uppercase = strupper (string)
  96. */
  97. static void str_upper (void)
  98. {
  99. char *s, *c;
  100. lua_Object o = lua_getparam (1);
  101. if (!lua_isstring(o))
  102. { lua_error ("incorrect arguments to function `strlower'"); return; }
  103. c = s = strdup(lua_getstring(o));
  104. while (*c != 0)
  105. {
  106. *c = toupper(*c);
  107. c++;
  108. }
  109. lua_pushstring(s);
  110. free(s);
  111. }
  112. /*
  113. ** Open string library
  114. */
  115. void strlib_open (void)
  116. {
  117. lua_register ("strfind", str_find);
  118. lua_register ("strlen", str_len);
  119. lua_register ("strsub", str_sub);
  120. lua_register ("strlower", str_lower);
  121. lua_register ("strupper", str_upper);
  122. }