strlib.c 2.7 KB

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