diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1995-01-06 18:31:10 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1995-01-06 18:31:10 -0200 |
| commit | 53c0a0f43c5ced8e5f7435aa50f1bfb1e5371992 (patch) | |
| tree | 99b03cd5091c198982e48df0436b2822cbac540e /strlib.c | |
| parent | ad97e9ccbcfff5cd6e8d9399a49fb4de8b87561e (diff) | |
| download | lua-53c0a0f43c5ced8e5f7435aa50f1bfb1e5371992.tar.gz lua-53c0a0f43c5ced8e5f7435aa50f1bfb1e5371992.tar.bz2 lua-53c0a0f43c5ced8e5f7435aa50f1bfb1e5371992.zip | |
function 'strfind' now has two optional parameters, to specify where
to start and stop the search.
Diffstat (limited to 'strlib.c')
| -rw-r--r-- | strlib.c | 30 |
1 files changed, 26 insertions, 4 deletions
| @@ -3,7 +3,7 @@ | |||
| 3 | ** String library to LUA | 3 | ** String library to LUA |
| 4 | */ | 4 | */ |
| 5 | 5 | ||
| 6 | char *rcs_strlib="$Id: strlib.c,v 1.6 1994/12/13 15:54:21 roberto Exp roberto $"; | 6 | char *rcs_strlib="$Id: strlib.c,v 1.7 1994/12/16 15:53:57 roberto Exp roberto $"; |
| 7 | 7 | ||
| 8 | #include <string.h> | 8 | #include <string.h> |
| 9 | #include <ctype.h> | 9 | #include <ctype.h> |
| @@ -15,20 +15,42 @@ char *rcs_strlib="$Id: strlib.c,v 1.6 1994/12/13 15:54:21 roberto Exp roberto $" | |||
| 15 | /* | 15 | /* |
| 16 | ** Return the position of the first caracter of a substring into a string | 16 | ** Return the position of the first caracter of a substring into a string |
| 17 | ** LUA interface: | 17 | ** LUA interface: |
| 18 | ** n = strfind (string, substring) | 18 | ** n = strfind (string, substring, init, end) |
| 19 | */ | 19 | */ |
| 20 | static void str_find (void) | 20 | static void str_find (void) |
| 21 | { | 21 | { |
| 22 | char *s1, *s2, *f; | 22 | char *s1, *s2, *f; |
| 23 | int init; | ||
| 23 | lua_Object o1 = lua_getparam (1); | 24 | lua_Object o1 = lua_getparam (1); |
| 24 | lua_Object o2 = lua_getparam (2); | 25 | lua_Object o2 = lua_getparam (2); |
| 26 | lua_Object o3 = lua_getparam (3); | ||
| 27 | lua_Object o4 = lua_getparam (4); | ||
| 25 | if (!lua_isstring(o1) || !lua_isstring(o2)) | 28 | if (!lua_isstring(o1) || !lua_isstring(o2)) |
| 26 | lua_error ("incorrect arguments to function `strfind'"); | 29 | lua_error ("incorrect arguments to function `strfind'"); |
| 30 | if (o3 == LUA_NOOBJECT) | ||
| 31 | init = 0; | ||
| 32 | else if (lua_isnumber(o3)) | ||
| 33 | init = lua_getnumber(o3)-1; | ||
| 34 | else | ||
| 35 | { | ||
| 36 | lua_error ("incorrect arguments to function `strfind'"); | ||
| 37 | return; /* to avoid warnings */ | ||
| 38 | } | ||
| 27 | s1 = lua_getstring(o1); | 39 | s1 = lua_getstring(o1); |
| 28 | s2 = lua_getstring(o2); | 40 | s2 = lua_getstring(o2); |
| 29 | f = strstr(s1,s2); | 41 | f = strstr(s1+init,s2); |
| 30 | if (f != NULL) | 42 | if (f != NULL) |
| 31 | lua_pushnumber (f-s1+1); | 43 | { |
| 44 | int pos = f-s1+1; | ||
| 45 | if (o4 == LUA_NOOBJECT) | ||
| 46 | lua_pushnumber (pos); | ||
| 47 | else if (!lua_isnumber(o4)) | ||
| 48 | lua_error ("incorrect arguments to function `strfind'"); | ||
| 49 | else if ((int)lua_getnumber(o4) >= pos+strlen(s2)-1) | ||
| 50 | lua_pushnumber (pos); | ||
| 51 | else | ||
| 52 | lua_pushnil(); | ||
| 53 | } | ||
| 32 | else | 54 | else |
| 33 | lua_pushnil(); | 55 | lua_pushnil(); |
| 34 | } | 56 | } |
