diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-03-03 11:58:26 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2000-03-03 11:58:26 -0300 |
commit | 3c9d999424520c809e05bee11d81788b488434f6 (patch) | |
tree | 7556d9ea10bda42b226aec4dd956753467cc0864 /lstrlib.c | |
parent | f7840a3e0bc07813246b2bad6bf4579848187908 (diff) | |
download | lua-3c9d999424520c809e05bee11d81788b488434f6.tar.gz lua-3c9d999424520c809e05bee11d81788b488434f6.tar.bz2 lua-3c9d999424520c809e05bee11d81788b488434f6.zip |
many details (most by lhf).
Diffstat (limited to 'lstrlib.c')
-rw-r--r-- | lstrlib.c | 46 |
1 files changed, 23 insertions, 23 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstrlib.c,v 1.39 1999/12/27 17:33:22 roberto Exp roberto $ | 2 | ** $Id: lstrlib.c,v 1.40 2000/02/08 16:34:31 roberto Exp roberto $ |
3 | ** Standard library for string operations and pattern-matching | 3 | ** Standard library for string operations and pattern-matching |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -116,8 +116,8 @@ static void str_char (lua_State *L) { | |||
116 | ** ======================================================= | 116 | ** ======================================================= |
117 | */ | 117 | */ |
118 | 118 | ||
119 | #ifndef MAX_CAPT | 119 | #ifndef MAX_CAPTURES |
120 | #define MAX_CAPT 32 /* arbitrary limit */ | 120 | #define MAX_CAPTURES 32 /* arbitrary limit */ |
121 | #endif | 121 | #endif |
122 | 122 | ||
123 | 123 | ||
@@ -127,12 +127,12 @@ struct Capture { | |||
127 | struct { | 127 | struct { |
128 | const char *init; | 128 | const char *init; |
129 | int len; /* -1 signals unfinished capture */ | 129 | int len; /* -1 signals unfinished capture */ |
130 | } capture[MAX_CAPT]; | 130 | } capture[MAX_CAPTURES]; |
131 | }; | 131 | }; |
132 | 132 | ||
133 | 133 | ||
134 | #define ESC '%' | 134 | #define ESC '%' |
135 | #define SPECIALS "^$*+?.([%-" | 135 | #define SPECIALS "^$*+?.([%-" |
136 | 136 | ||
137 | 137 | ||
138 | static void push_captures (lua_State *L, struct Capture *cap) { | 138 | static void push_captures (lua_State *L, struct Capture *cap) { |
@@ -145,7 +145,7 @@ static void push_captures (lua_State *L, struct Capture *cap) { | |||
145 | } | 145 | } |
146 | 146 | ||
147 | 147 | ||
148 | static int check_cap (lua_State *L, int l, struct Capture *cap) { | 148 | static int check_capture (lua_State *L, int l, struct Capture *cap) { |
149 | l -= '1'; | 149 | l -= '1'; |
150 | if (!(0 <= l && l < cap->level && cap->capture[l].len != -1)) | 150 | if (!(0 <= l && l < cap->level && cap->capture[l].len != -1)) |
151 | lua_error(L, "invalid capture index"); | 151 | lua_error(L, "invalid capture index"); |
@@ -165,12 +165,12 @@ static int capture_to_close (lua_State *L, struct Capture *cap) { | |||
165 | const char *luaI_classend (lua_State *L, const char *p) { | 165 | const char *luaI_classend (lua_State *L, const char *p) { |
166 | switch (*p++) { | 166 | switch (*p++) { |
167 | case ESC: | 167 | case ESC: |
168 | if (*p == '\0') lua_error(L, "incorrect pattern (ends with `%')"); | 168 | if (*p == '\0') lua_error(L, "malformed pattern (ends with `%')"); |
169 | return p+1; | 169 | return p+1; |
170 | case '[': | 170 | case '[': |
171 | if (*p == '^') p++; | 171 | if (*p == '^') p++; |
172 | do { /* look for a ']' */ | 172 | do { /* look for a ']' */ |
173 | if (*p == '\0') lua_error(L, "incorrect pattern (missing `]')"); | 173 | if (*p == '\0') lua_error(L, "malformed pattern (missing `]')"); |
174 | if (*(p++) == ESC && *p != '\0') p++; /* skip escapes (e.g. '%]') */ | 174 | if (*(p++) == ESC && *p != '\0') p++; /* skip escapes (e.g. '%]') */ |
175 | } while (*p != ']'); | 175 | } while (*p != ']'); |
176 | return p+1; | 176 | return p+1; |
@@ -180,7 +180,7 @@ const char *luaI_classend (lua_State *L, const char *p) { | |||
180 | } | 180 | } |
181 | 181 | ||
182 | 182 | ||
183 | static int matchclass (int c, int cl) { | 183 | static int match_class (int c, int cl) { |
184 | int res; | 184 | int res; |
185 | switch (tolower(cl)) { | 185 | switch (tolower(cl)) { |
186 | case 'a' : res = isalpha(c); break; | 186 | case 'a' : res = isalpha(c); break; |
@@ -209,7 +209,7 @@ static int matchbracketclass (int c, const char *p, const char *endclass) { | |||
209 | while (++p < endclass) { | 209 | while (++p < endclass) { |
210 | if (*p == ESC) { | 210 | if (*p == ESC) { |
211 | p++; | 211 | p++; |
212 | if (matchclass(c, (unsigned char)*p)) | 212 | if (match_class(c, (unsigned char)*p)) |
213 | return sig; | 213 | return sig; |
214 | } | 214 | } |
215 | else if ((*(p+1) == '-') && (p+2 < endclass)) { | 215 | else if ((*(p+1) == '-') && (p+2 < endclass)) { |
@@ -217,7 +217,7 @@ static int matchbracketclass (int c, const char *p, const char *endclass) { | |||
217 | if ((int)(unsigned char)*(p-2) <= c && c <= (int)(unsigned char)*p) | 217 | if ((int)(unsigned char)*(p-2) <= c && c <= (int)(unsigned char)*p) |
218 | return sig; | 218 | return sig; |
219 | } | 219 | } |
220 | else if ((unsigned char)*p == c) return sig; | 220 | else if ((int)(unsigned char)*p == c) return sig; |
221 | } | 221 | } |
222 | return !sig; | 222 | return !sig; |
223 | } | 223 | } |
@@ -229,7 +229,7 @@ int luaI_singlematch (int c, const char *p, const char *ep) { | |||
229 | case '.': /* matches any char */ | 229 | case '.': /* matches any char */ |
230 | return 1; | 230 | return 1; |
231 | case ESC: | 231 | case ESC: |
232 | return matchclass(c, (unsigned char)*(p+1)); | 232 | return match_class(c, (unsigned char)*(p+1)); |
233 | case '[': | 233 | case '[': |
234 | return matchbracketclass(c, p, ep-1); | 234 | return matchbracketclass(c, p, ep-1); |
235 | default: | 235 | default: |
@@ -289,11 +289,11 @@ static const char *min_expand (lua_State *L, const char *s, const char *p, const | |||
289 | } | 289 | } |
290 | 290 | ||
291 | 291 | ||
292 | static const char *start_capt (lua_State *L, const char *s, const char *p, | 292 | static const char *start_capture (lua_State *L, const char *s, const char *p, |
293 | struct Capture *cap) { | 293 | struct Capture *cap) { |
294 | const char *res; | 294 | const char *res; |
295 | int level = cap->level; | 295 | int level = cap->level; |
296 | if (level >= MAX_CAPT) lua_error(L, "too many captures"); | 296 | if (level >= MAX_CAPTURES) lua_error(L, "too many captures"); |
297 | cap->capture[level].init = s; | 297 | cap->capture[level].init = s; |
298 | cap->capture[level].len = -1; | 298 | cap->capture[level].len = -1; |
299 | cap->level = level+1; | 299 | cap->level = level+1; |
@@ -303,8 +303,8 @@ static const char *start_capt (lua_State *L, const char *s, const char *p, | |||
303 | } | 303 | } |
304 | 304 | ||
305 | 305 | ||
306 | static const char *end_capt (lua_State *L, const char *s, const char *p, | 306 | static const char *end_capture (lua_State *L, const char *s, const char *p, |
307 | struct Capture *cap) { | 307 | struct Capture *cap) { |
308 | int l = capture_to_close(L, cap); | 308 | int l = capture_to_close(L, cap); |
309 | const char *res; | 309 | const char *res; |
310 | cap->capture[l].len = s - cap->capture[l].init; /* close capture */ | 310 | cap->capture[l].len = s - cap->capture[l].init; /* close capture */ |
@@ -316,7 +316,7 @@ static const char *end_capt (lua_State *L, const char *s, const char *p, | |||
316 | 316 | ||
317 | static const char *match_capture (lua_State *L, const char *s, int level, | 317 | static const char *match_capture (lua_State *L, const char *s, int level, |
318 | struct Capture *cap) { | 318 | struct Capture *cap) { |
319 | int l = check_cap(L, level, cap); | 319 | int l = check_capture(L, level, cap); |
320 | int len = cap->capture[l].len; | 320 | int len = cap->capture[l].len; |
321 | if (cap->src_end-s >= len && | 321 | if (cap->src_end-s >= len && |
322 | memcmp(cap->capture[l].init, s, len) == 0) | 322 | memcmp(cap->capture[l].init, s, len) == 0) |
@@ -329,9 +329,9 @@ static const char *match (lua_State *L, const char *s, const char *p, struct Cap | |||
329 | init: /* using goto's to optimize tail recursion */ | 329 | init: /* using goto's to optimize tail recursion */ |
330 | switch (*p) { | 330 | switch (*p) { |
331 | case '(': /* start capture */ | 331 | case '(': /* start capture */ |
332 | return start_capt(L, s, p, cap); | 332 | return start_capture(L, s, p, cap); |
333 | case ')': /* end capture */ | 333 | case ')': /* end capture */ |
334 | return end_capt(L, s, p, cap); | 334 | return end_capture(L, s, p, cap); |
335 | case ESC: /* may be %[0-9] or %b */ | 335 | case ESC: /* may be %[0-9] or %b */ |
336 | if (isdigit((unsigned char)(*(p+1)))) { /* capture? */ | 336 | if (isdigit((unsigned char)(*(p+1)))) { /* capture? */ |
337 | s = match_capture(L, s, *(p+1), cap); | 337 | s = match_capture(L, s, *(p+1), cap); |
@@ -444,7 +444,7 @@ static void add_s (lua_State *L, lua_Object newp, struct Capture *cap) { | |||
444 | if (!isdigit((unsigned char)news[i])) | 444 | if (!isdigit((unsigned char)news[i])) |
445 | luaL_addchar(L, news[i]); | 445 | luaL_addchar(L, news[i]); |
446 | else { | 446 | else { |
447 | int level = check_cap(L, news[i], cap); | 447 | int level = check_capture(L, news[i], cap); |
448 | addnchar(L, cap->capture[level].init, cap->capture[level].len); | 448 | addnchar(L, cap->capture[level].init, cap->capture[level].len); |
449 | } | 449 | } |
450 | } | 450 | } |
@@ -576,7 +576,7 @@ static void str_format (lua_State *L) { | |||
576 | long l; | 576 | long l; |
577 | const char *s = luaL_check_lstr(L, arg, &l); | 577 | const char *s = luaL_check_lstr(L, arg, &l); |
578 | if (cap.capture[1].len == 0 && l >= 100) { | 578 | if (cap.capture[1].len == 0 && l >= 100) { |
579 | /* no precision and string is too big to be formatted; | 579 | /* no precision and string is too long to be formatted; |
580 | keep original string */ | 580 | keep original string */ |
581 | addnchar(L, s, l); | 581 | addnchar(L, s, l); |
582 | continue; /* skip the "addsize" at the end */ | 582 | continue; /* skip the "addsize" at the end */ |