strlib.c 3.2 KB

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