strlib.c 3.2 KB

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