Fix range error (doc and impl of re_search disagreed!)

Use getargs() function.
Drop re_string from re object.
This commit is contained in:
Guido van Rossum 1992-01-27 16:46:19 +00:00
parent fc58e58cb7
commit d577c0ca90
1 changed files with 32 additions and 29 deletions

View File

@ -41,7 +41,6 @@ static object *RegexError; /* Exception */
typedef struct { typedef struct {
OB_HEAD OB_HEAD
object *re_string; /* The string (for printing) */
struct re_pattern_buffer re_patbuf; /* The compiled expression */ struct re_pattern_buffer re_patbuf; /* The compiled expression */
struct re_registers re_regs; /* The registers from the last match */ struct re_registers re_regs; /* The registers from the last match */
int re_regs_valid; /* Nonzero if the registers are valid */ int re_regs_valid; /* Nonzero if the registers are valid */
@ -54,7 +53,6 @@ static void
reg_dealloc(re) reg_dealloc(re)
regexobject *re; regexobject *re;
{ {
XDECREF(re->re_string);
XDEL(re->re_patbuf.buffer); XDEL(re->re_patbuf.buffer);
XDEL(re->re_patbuf.translate); XDEL(re->re_patbuf.translate);
DEL(re); DEL(re);
@ -91,18 +89,19 @@ reg_match(re, args)
{ {
object *v; object *v;
char *buffer; char *buffer;
int size;
int offset; int offset;
int result; int result;
if (args != NULL && is_stringobject(args)) { if (getargs(args, "s#", &buffer, &size)) {
v = args;
offset = 0; offset = 0;
} }
else if (!getstrintarg(args, &v, &offset)) else {
return NULL; err_clear();
buffer = getstringvalue(v); if (!getargs(args, "(s#i)", &buffer, &size, &offset))
return NULL;
}
re->re_regs_valid = 0; re->re_regs_valid = 0;
result = re_match(&re->re_patbuf, buffer, getstringsize(v), result = re_match(&re->re_patbuf, buffer, size, offset, &re->re_regs);
offset, &re->re_regs);
if (result < -1) { if (result < -1) {
/* Failure like stack overflow */ /* Failure like stack overflow */
err_setstr(RegexError, "match failure"); err_setstr(RegexError, "match failure");
@ -111,6 +110,7 @@ reg_match(re, args)
re->re_regs_valid = result >= 0; re->re_regs_valid = result >= 0;
return newintobject((long)result); /* Length of the match or -1 */ return newintobject((long)result); /* Length of the match or -1 */
} }
static object * static object *
reg_search(re, args) reg_search(re, args)
regexobject *re; regexobject *re;
@ -122,18 +122,23 @@ reg_search(re, args)
int offset; int offset;
int range; int range;
int result; int result;
if (args != NULL && is_stringobject(args)) {
v = args; if (getargs(args, "s#", &buffer, &size)) {
offset = 0; offset = 0;
} }
else if (!getstrintarg(args, &v, &offset)) else {
return NULL; err_clear();
buffer = getstringvalue(v); if (!getargs(args, "(s#i)", &buffer, &size, &offset))
size = getstringsize(v); return NULL;
}
if (offset < 0 || offset > size) { if (offset < 0 || offset > size) {
err_setstr(RegexError, "search offset out of range"); err_setstr(RegexError, "search offset out of range");
return NULL; return NULL;
} }
/* NB: In Emacs 18.57, the documentation for re_search[_2] and
the implementation don't match: the documentation states that
|range| positions are tried, while the code tries |range|+1
positions. It seems more productive to believe the code! */
range = size - offset; range = size - offset;
re->re_regs_valid = 0; re->re_regs_valid = 0;
result = re_search(&re->re_patbuf, buffer, size, offset, range, result = re_search(&re->re_patbuf, buffer, size, offset, range,
@ -184,31 +189,28 @@ static typeobject Regextype = {
0, /*tp_repr*/ 0, /*tp_repr*/
}; };
static regexobject * static object *
newregexobject(string) newregexobject(pat, size)
object *string; char *pat;
int size;
{ {
regexobject *re; regexobject *re;
re = NEWOBJ(regexobject, &Regextype); re = NEWOBJ(regexobject, &Regextype);
if (re != NULL) { if (re != NULL) {
char *error; char *error;
INCREF(string);
re->re_string = string;
re->re_patbuf.buffer = NULL; re->re_patbuf.buffer = NULL;
re->re_patbuf.allocated = 0; re->re_patbuf.allocated = 0;
re->re_patbuf.fastmap = re->re_fastmap; re->re_patbuf.fastmap = re->re_fastmap;
re->re_patbuf.translate = NULL; re->re_patbuf.translate = NULL;
re->re_regs_valid = 0; re->re_regs_valid = 0;
error = re_compile_pattern(getstringvalue(string), error = re_compile_pattern(pat, size, &re->re_patbuf);
getstringsize(string),
&re->re_patbuf);
if (error != NULL) { if (error != NULL) {
err_setstr(RegexError, error); err_setstr(RegexError, error);
DECREF(re); DECREF(re);
re = NULL; re = NULL;
} }
} }
return re; return (object *)re;
} }
static object * static object *
@ -216,10 +218,11 @@ regex_compile(self, args)
object *self; object *self;
object *args; object *args;
{ {
object *string; char *pat;
if (!getstrarg(args, &string)) int size;
if (!getargs(args, "s#", &pat, &size))
return NULL; return NULL;
return (object *)newregexobject(string); return newregexobject(pat, size);
} }
static object *cache_pat; static object *cache_pat;
@ -248,7 +251,7 @@ regex_match(self, args)
object *args; object *args;
{ {
object *pat, *string; object *pat, *string;
if (!getstrstrarg(args, &pat, &string)) if (!getStrStrarg(args, &pat, &string))
return NULL; return NULL;
if (update_cache(pat) < 0) if (update_cache(pat) < 0)
return NULL; return NULL;
@ -261,7 +264,7 @@ regex_search(self, args)
object *args; object *args;
{ {
object *pat, *string; object *pat, *string;
if (!getstrstrarg(args, &pat, &string)) if (!getStrStrarg(args, &pat, &string))
return NULL; return NULL;
if (update_cache(pat) < 0) if (update_cache(pat) < 0)
return NULL; return NULL;