strlib.c 3.4 KB

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