diff options
| author | The Lua team <lua@tecgraf.puc-rio.br> | 1993-07-28 10:18:00 -0300 |
|---|---|---|
| committer | The Lua team <lua@tecgraf.puc-rio.br> | 1993-07-28 10:18:00 -0300 |
| commit | cd05d9c5cb69020c069f037ba7f243f705d0a48a (patch) | |
| tree | cb7f08c0684c10970a528984741047fb3babadd3 /strlib.c | |
| download | lua-cd05d9c5cb69020c069f037ba7f243f705d0a48a.tar.gz lua-cd05d9c5cb69020c069f037ba7f243f705d0a48a.tar.bz2 lua-cd05d9c5cb69020c069f037ba7f243f705d0a48a.zip | |
oldest known commit
Diffstat (limited to 'strlib.c')
| -rw-r--r-- | strlib.c | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/strlib.c b/strlib.c new file mode 100644 index 00000000..efd01e9b --- /dev/null +++ b/strlib.c | |||
| @@ -0,0 +1,131 @@ | |||
| 1 | /* | ||
| 2 | ** strlib.c | ||
| 3 | ** String library to LUA | ||
| 4 | ** | ||
| 5 | ** Waldemar Celes Filho | ||
| 6 | ** TeCGraf - PUC-Rio | ||
| 7 | ** 19 May 93 | ||
| 8 | */ | ||
| 9 | |||
| 10 | #include <stdlib.h> | ||
| 11 | #include <string.h> | ||
| 12 | #include <ctype.h> | ||
| 13 | |||
| 14 | |||
| 15 | #include "lua.h" | ||
| 16 | |||
| 17 | /* | ||
| 18 | ** Return the position of the first caracter of a substring into a string | ||
| 19 | ** LUA interface: | ||
| 20 | ** n = strfind (string, substring) | ||
| 21 | */ | ||
| 22 | static void str_find (void) | ||
| 23 | { | ||
| 24 | int n; | ||
| 25 | char *s1, *s2; | ||
| 26 | lua_Object o1 = lua_getparam (1); | ||
| 27 | lua_Object o2 = lua_getparam (2); | ||
| 28 | if (!lua_isstring(o1) || !lua_isstring(o2)) | ||
| 29 | { lua_error ("incorrect arguments to function `strfind'"); return; } | ||
| 30 | s1 = lua_getstring(o1); | ||
| 31 | s2 = lua_getstring(o2); | ||
| 32 | n = strstr(s1,s2) - s1 + 1; | ||
| 33 | lua_pushnumber (n); | ||
| 34 | } | ||
| 35 | |||
| 36 | /* | ||
| 37 | ** Return the string length | ||
| 38 | ** LUA interface: | ||
| 39 | ** n = strlen (string) | ||
| 40 | */ | ||
| 41 | static void str_len (void) | ||
| 42 | { | ||
| 43 | lua_Object o = lua_getparam (1); | ||
| 44 | if (!lua_isstring(o)) | ||
| 45 | { lua_error ("incorrect arguments to function `strlen'"); return; } | ||
| 46 | lua_pushnumber(strlen(lua_getstring(o))); | ||
| 47 | } | ||
| 48 | |||
| 49 | |||
| 50 | /* | ||
| 51 | ** Return the substring of a string, from start to end | ||
| 52 | ** LUA interface: | ||
| 53 | ** substring = strsub (string, start, end) | ||
| 54 | */ | ||
| 55 | static void str_sub (void) | ||
| 56 | { | ||
| 57 | int start, end; | ||
| 58 | char *s; | ||
| 59 | lua_Object o1 = lua_getparam (1); | ||
| 60 | lua_Object o2 = lua_getparam (2); | ||
| 61 | lua_Object o3 = lua_getparam (3); | ||
| 62 | if (!lua_isstring(o1) || !lua_isnumber(o2) || !lua_isnumber(o3)) | ||
| 63 | { lua_error ("incorrect arguments to function `strsub'"); return; } | ||
| 64 | s = strdup (lua_getstring(o1)); | ||
| 65 | start = lua_getnumber (o2); | ||
| 66 | end = lua_getnumber (o3); | ||
| 67 | if (end < start || start < 1 || end > strlen(s)) | ||
| 68 | lua_pushstring (""); | ||
| 69 | else | ||
| 70 | { | ||
| 71 | s[end] = 0; | ||
| 72 | lua_pushstring (&s[start-1]); | ||
| 73 | } | ||
| 74 | free (s); | ||
| 75 | } | ||
| 76 | |||
| 77 | /* | ||
| 78 | ** Convert a string to lower case. | ||
| 79 | ** LUA interface: | ||
| 80 | ** lowercase = strlower (string) | ||
| 81 | */ | ||
| 82 | static void str_lower (void) | ||
| 83 | { | ||
| 84 | char *s, *c; | ||
| 85 | lua_Object o = lua_getparam (1); | ||
| 86 | if (!lua_isstring(o)) | ||
| 87 | { lua_error ("incorrect arguments to function `strlower'"); return; } | ||
| 88 | c = s = strdup(lua_getstring(o)); | ||
| 89 | while (*c != 0) | ||
| 90 | { | ||
| 91 | *c = tolower(*c); | ||
| 92 | c++; | ||
| 93 | } | ||
| 94 | lua_pushstring(s); | ||
| 95 | free(s); | ||
| 96 | } | ||
| 97 | |||
| 98 | |||
| 99 | /* | ||
| 100 | ** Convert a string to upper case. | ||
| 101 | ** LUA interface: | ||
| 102 | ** uppercase = strupper (string) | ||
| 103 | */ | ||
| 104 | static void str_upper (void) | ||
| 105 | { | ||
| 106 | char *s, *c; | ||
| 107 | lua_Object o = lua_getparam (1); | ||
| 108 | if (!lua_isstring(o)) | ||
| 109 | { lua_error ("incorrect arguments to function `strlower'"); return; } | ||
| 110 | c = s = strdup(lua_getstring(o)); | ||
| 111 | while (*c != 0) | ||
| 112 | { | ||
| 113 | *c = toupper(*c); | ||
| 114 | c++; | ||
| 115 | } | ||
| 116 | lua_pushstring(s); | ||
| 117 | free(s); | ||
| 118 | } | ||
| 119 | |||
| 120 | |||
| 121 | /* | ||
| 122 | ** Open string library | ||
| 123 | */ | ||
| 124 | void strlib_open (void) | ||
| 125 | { | ||
| 126 | lua_register ("strfind", str_find); | ||
| 127 | lua_register ("strlen", str_len); | ||
| 128 | lua_register ("strsub", str_sub); | ||
| 129 | lua_register ("strlower", str_lower); | ||
| 130 | lua_register ("strupper", str_upper); | ||
| 131 | } | ||
