mirror of
https://github.com/python/cpython.git
synced 2025-09-27 02:39:58 +00:00
Extensive changes to regex module (group(), casefold, etc.)
This commit is contained in:
parent
337b20e23e
commit
ccd5bad471
1 changed files with 46 additions and 12 deletions
|
@ -168,7 +168,7 @@ reg_search(re, args)
|
||||||
}
|
}
|
||||||
|
|
||||||
static object *
|
static object *
|
||||||
reg_substring(re, args)
|
reg_group(re, args)
|
||||||
regexobject *re;
|
regexobject *re;
|
||||||
object *args;
|
object *args;
|
||||||
{
|
{
|
||||||
|
@ -179,7 +179,7 @@ reg_substring(re, args)
|
||||||
if (res == NULL)
|
if (res == NULL)
|
||||||
return NULL;
|
return NULL;
|
||||||
for (i = 0; i < n; i++) {
|
for (i = 0; i < n; i++) {
|
||||||
object *v = reg_substring(re, gettupleitem(args, i));
|
object *v = reg_group(re, gettupleitem(args, i));
|
||||||
if (v == NULL) {
|
if (v == NULL) {
|
||||||
DECREF(res);
|
DECREF(res);
|
||||||
return NULL;
|
return NULL;
|
||||||
|
@ -191,12 +191,12 @@ reg_substring(re, args)
|
||||||
if (!getargs(args, "i", &i))
|
if (!getargs(args, "i", &i))
|
||||||
return NULL;
|
return NULL;
|
||||||
if (i < 0 || i >= RE_NREGS) {
|
if (i < 0 || i >= RE_NREGS) {
|
||||||
err_setstr(RegexError, "substring() index out of range");
|
err_setstr(RegexError, "group() index out of range");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
if (re->re_lastok == NULL) {
|
if (re->re_lastok == NULL) {
|
||||||
err_setstr(RegexError,
|
err_setstr(RegexError,
|
||||||
"substring() only valid after successful match/search");
|
"group() only valid after successful match/search");
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
a = re->re_regs.start[i];
|
a = re->re_regs.start[i];
|
||||||
|
@ -211,7 +211,7 @@ reg_substring(re, args)
|
||||||
static struct methodlist reg_methods[] = {
|
static struct methodlist reg_methods[] = {
|
||||||
{"match", reg_match},
|
{"match", reg_match},
|
||||||
{"search", reg_search},
|
{"search", reg_search},
|
||||||
{"substring", reg_substring},
|
{"group", reg_group},
|
||||||
{NULL, NULL} /* sentinel */
|
{NULL, NULL} /* sentinel */
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -222,21 +222,40 @@ reg_getattr(re, name)
|
||||||
{
|
{
|
||||||
if (strcmp(name, "regs") == 0) {
|
if (strcmp(name, "regs") == 0) {
|
||||||
if (re->re_lastok == NULL) {
|
if (re->re_lastok == NULL) {
|
||||||
err_setstr(RegexError,
|
INCREF(None);
|
||||||
"regs only valid after successful match/search");
|
return None;
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
return makeresult(&re->re_regs);
|
return makeresult(&re->re_regs);
|
||||||
}
|
}
|
||||||
if (strcmp(name, "last") == 0) {
|
if (strcmp(name, "last") == 0) {
|
||||||
if (re->re_lastok == NULL) {
|
if (re->re_lastok == NULL) {
|
||||||
err_setstr(RegexError,
|
INCREF(None);
|
||||||
"last only valid after successful match/search");
|
return None;
|
||||||
return NULL;
|
|
||||||
}
|
}
|
||||||
INCREF(re->re_lastok);
|
INCREF(re->re_lastok);
|
||||||
return re->re_lastok;
|
return re->re_lastok;
|
||||||
}
|
}
|
||||||
|
if (strcmp(name, "translate") == 0) {
|
||||||
|
if (re->re_translate == NULL) {
|
||||||
|
INCREF(None);
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
INCREF(re->re_translate);
|
||||||
|
return re->re_translate;
|
||||||
|
}
|
||||||
|
if (strcmp(name, "__members__") == 0) {
|
||||||
|
object *list = newlistobject(3);
|
||||||
|
if (list) {
|
||||||
|
setlistitem(list, 0, newstringobject("last"));
|
||||||
|
setlistitem(list, 1, newstringobject("regs"));
|
||||||
|
setlistitem(list, 2, newstringobject("translate"));
|
||||||
|
if (err_occurred()) {
|
||||||
|
DECREF(list);
|
||||||
|
list = NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return list;
|
||||||
|
}
|
||||||
return findmethod(reg_methods, (object *)re, name);
|
return findmethod(reg_methods, (object *)re, name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -373,7 +392,7 @@ static struct methodlist regex_global_methods[] = {
|
||||||
|
|
||||||
initregex()
|
initregex()
|
||||||
{
|
{
|
||||||
object *m, *d;
|
object *m, *d, *v;
|
||||||
|
|
||||||
m = initmodule("regex", regex_global_methods);
|
m = initmodule("regex", regex_global_methods);
|
||||||
d = getmoduledict(m);
|
d = getmoduledict(m);
|
||||||
|
@ -382,4 +401,19 @@ initregex()
|
||||||
RegexError = newstringobject("regex.error");
|
RegexError = newstringobject("regex.error");
|
||||||
if (RegexError == NULL || dictinsert(d, "error", RegexError) != 0)
|
if (RegexError == NULL || dictinsert(d, "error", RegexError) != 0)
|
||||||
fatal("can't define regex.error");
|
fatal("can't define regex.error");
|
||||||
|
|
||||||
|
/* Initialize regex.casefold constant */
|
||||||
|
v = newsizedstringobject((char *)NULL, 256);
|
||||||
|
if (v != NULL) {
|
||||||
|
int i;
|
||||||
|
char *s = getstringvalue(v);
|
||||||
|
for (i = 0; i < 256; i++) {
|
||||||
|
if (isupper(i))
|
||||||
|
s[i] = tolower(i);
|
||||||
|
else
|
||||||
|
s[i] = i;
|
||||||
|
}
|
||||||
|
dictinsert(d, "casefold", v);
|
||||||
|
DECREF(v);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue