strlib.c 2.7 KB

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