aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>1996-03-22 14:57:24 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>1996-03-22 14:57:24 -0300
commitdf1ee1fb1c5e0235c3182e2094dacb98de00937f (patch)
tree6b4019aefda2ab77823c419c4a576dbae191c380
parentf1d0276684ef85ee457fe6a25d85cb8d5b3df0b6 (diff)
downloadlua-df1ee1fb1c5e0235c3182e2094dacb98de00937f.tar.gz
lua-df1ee1fb1c5e0235c3182e2094dacb98de00937f.tar.bz2
lua-df1ee1fb1c5e0235c3182e2094dacb98de00937f.zip
small "abstraction"
-rw-r--r--strlib.c23
1 files changed, 12 insertions, 11 deletions
diff --git a/strlib.c b/strlib.c
index 33ff56c0..9d084b36 100644
--- a/strlib.c
+++ b/strlib.c
@@ -3,12 +3,13 @@
3** String library to LUA 3** String library to LUA
4*/ 4*/
5 5
6char *rcs_strlib="$Id: strlib.c,v 1.20 1996/03/19 22:28:37 roberto Exp roberto $"; 6char *rcs_strlib="$Id: strlib.c,v 1.21 1996/03/21 22:18:08 roberto Exp roberto $";
7 7
8#include <string.h> 8#include <string.h>
9#include <stdio.h> 9#include <stdio.h>
10#include <stdlib.h> 10#include <stdlib.h>
11#include <ctype.h> 11#include <ctype.h>
12#include <limits.h>
12 13
13#include "lua.h" 14#include "lua.h"
14#include "lualib.h" 15#include "lualib.h"
@@ -37,6 +38,12 @@ double lua_check_number (int numArg, char *funcname)
37 return lua_getnumber(o); 38 return lua_getnumber(o);
38} 39}
39 40
41static int lua_opt_number (int numArg, int def, char *funcname)
42{
43 return (lua_getparam(numArg) == LUA_NOOBJECT) ? def :
44 (int)lua_check_number(numArg, funcname);
45}
46
40char *luaI_addchar (int c) 47char *luaI_addchar (int c)
41{ 48{
42 static char *buff = NULL; 49 static char *buff = NULL;
@@ -73,15 +80,12 @@ static void str_find (void)
73{ 80{
74 char *s1 = lua_check_string(1, "strfind"); 81 char *s1 = lua_check_string(1, "strfind");
75 char *s2 = lua_check_string(2, "strfind"); 82 char *s2 = lua_check_string(2, "strfind");
76 int init = (lua_getparam(3) == LUA_NOOBJECT) ? 0 : 83 int init = lua_opt_number(3, 1, "strfind") - 1;
77 (int)lua_check_number(3, "strfind")-1;
78 char *f = (init>=0 && init<=strlen(s1)) ? strstr(s1+init,s2) : NULL; 84 char *f = (init>=0 && init<=strlen(s1)) ? strstr(s1+init,s2) : NULL;
79 if (f != NULL) 85 if (f != NULL)
80 { 86 {
81 int pos = f-s1+1; 87 int pos = f-s1+1;
82 if (lua_getparam (4) == LUA_NOOBJECT) 88 if (lua_opt_number(4, INT_MAX, "strfind") >= pos+strlen(s2)-1)
83 lua_pushnumber (pos);
84 else if ((int)lua_check_number(4, "strfind") >= pos+strlen(s2)-1)
85 lua_pushnumber (pos); 89 lua_pushnumber (pos);
86 else 90 else
87 lua_pushnil(); 91 lua_pushnil();
@@ -111,8 +115,7 @@ static void str_sub (void)
111{ 115{
112 char *s = lua_check_string(1, "strsub"); 116 char *s = lua_check_string(1, "strsub");
113 int start = (int)lua_check_number(2, "strsub"); 117 int start = (int)lua_check_number(2, "strsub");
114 int end = (lua_getparam(3) == LUA_NOOBJECT) ? strlen(s) : 118 int end = lua_opt_number(3, strlen(s), "strsub");
115 (int)lua_check_number(3, "strsub");
116 if (end < start || start < 1 || end > strlen(s)) 119 if (end < start || start < 1 || end > strlen(s))
117 lua_pushliteral(""); 120 lua_pushliteral("");
118 else 121 else
@@ -159,9 +162,7 @@ static void str_upper (void)
159static void str_ascii (void) 162static void str_ascii (void)
160{ 163{
161 char *s = lua_check_string(1, "ascii"); 164 char *s = lua_check_string(1, "ascii");
162 lua_Object o2 = lua_getparam(2); 165 int pos = lua_opt_number(2, 1, "ascii") - 1;
163 int pos;
164 pos = (o2 == LUA_NOOBJECT) ? 0 : (int)lua_check_number(2, "ascii")-1;
165 if (pos<0 || pos>=strlen(s)) 166 if (pos<0 || pos>=strlen(s))
166 lua_arg_error("ascii"); 167 lua_arg_error("ascii");
167 lua_pushnumber(s[pos]); 168 lua_pushnumber(s[pos]);