diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1995-02-06 17:37:51 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1995-02-06 17:37:51 -0200 |
commit | 2d053126e642bc1770ccc426ef0a95eb13f2085c (patch) | |
tree | 05f35d5b215a9cbd21433ad45a86434e56ec0937 | |
parent | 3203460c9e44f44e4586ecee6b1cb528db9150c7 (diff) | |
download | lua-2d053126e642bc1770ccc426ef0a95eb13f2085c.tar.gz lua-2d053126e642bc1770ccc426ef0a95eb13f2085c.tar.bz2 lua-2d053126e642bc1770ccc426ef0a95eb13f2085c.zip |
new function for copy strings (strdup is not ANSI)
-rw-r--r-- | strlib.c | 21 |
1 files changed, 16 insertions, 5 deletions
@@ -3,16 +3,27 @@ | |||
3 | ** String library to LUA | 3 | ** String library to LUA |
4 | */ | 4 | */ |
5 | 5 | ||
6 | char *rcs_strlib="$Id: strlib.c,v 1.10 1995/02/02 18:54:58 roberto Exp $"; | 6 | char *rcs_strlib="$Id: strlib.c,v 1.11 1995/02/02 20:05:37 roberto Exp roberto $"; |
7 | 7 | ||
8 | #include <string.h> | 8 | #include <string.h> |
9 | #include <strings.h> | ||
10 | #include <stdlib.h> | 9 | #include <stdlib.h> |
11 | #include <ctype.h> | 10 | #include <ctype.h> |
12 | 11 | ||
13 | #include "lua.h" | 12 | #include "lua.h" |
14 | #include "lualib.h" | 13 | #include "lualib.h" |
15 | 14 | ||
15 | |||
16 | static char *newstring (lua_Object o) | ||
17 | { | ||
18 | char *s = lua_getstring(o); | ||
19 | char *ns = (char *)malloc(strlen(s)+1); | ||
20 | if (ns == 0) | ||
21 | lua_error("not enough memory for new string"); | ||
22 | strcpy(ns, s); | ||
23 | return ns; | ||
24 | } | ||
25 | |||
26 | |||
16 | /* | 27 | /* |
17 | ** Return the position of the first caracter of a substring into a string | 28 | ** Return the position of the first caracter of a substring into a string |
18 | ** LUA interface: | 29 | ** LUA interface: |
@@ -86,7 +97,7 @@ static void str_sub (void) | |||
86 | lua_error ("incorrect arguments to function `strsub'"); | 97 | lua_error ("incorrect arguments to function `strsub'"); |
87 | if (o3 != LUA_NOOBJECT && !lua_isnumber(o3)) | 98 | if (o3 != LUA_NOOBJECT && !lua_isnumber(o3)) |
88 | lua_error ("incorrect third argument to function `strsub'"); | 99 | lua_error ("incorrect third argument to function `strsub'"); |
89 | s = lua_copystring(o1); | 100 | s = newstring(o1); |
90 | start = lua_getnumber (o2); | 101 | start = lua_getnumber (o2); |
91 | end = o3 == LUA_NOOBJECT ? strlen(s) : lua_getnumber (o3); | 102 | end = o3 == LUA_NOOBJECT ? strlen(s) : lua_getnumber (o3); |
92 | if (end < start || start < 1 || end > strlen(s)) | 103 | if (end < start || start < 1 || end > strlen(s)) |
@@ -110,7 +121,7 @@ static void str_lower (void) | |||
110 | lua_Object o = lua_getparam (1); | 121 | lua_Object o = lua_getparam (1); |
111 | if (!lua_isstring(o)) | 122 | if (!lua_isstring(o)) |
112 | lua_error ("incorrect arguments to function `strlower'"); | 123 | lua_error ("incorrect arguments to function `strlower'"); |
113 | c = s = lua_copystring(o); | 124 | c = s = newstring(o); |
114 | while (*c != 0) | 125 | while (*c != 0) |
115 | { | 126 | { |
116 | *c = tolower(*c); | 127 | *c = tolower(*c); |
@@ -132,7 +143,7 @@ static void str_upper (void) | |||
132 | lua_Object o = lua_getparam (1); | 143 | lua_Object o = lua_getparam (1); |
133 | if (!lua_isstring(o)) | 144 | if (!lua_isstring(o)) |
134 | lua_error ("incorrect arguments to function `strlower'"); | 145 | lua_error ("incorrect arguments to function `strlower'"); |
135 | c = s = lua_copystring(o); | 146 | c = s = newstring(o); |
136 | while (*c != 0) | 147 | while (*c != 0) |
137 | { | 148 | { |
138 | *c = toupper(*c); | 149 | *c = toupper(*c); |