From: Finn B. <bc...@us...> - 2002-01-06 15:53:27
|
Update of /cvsroot/jython/jython/org/python/modules/sre In directory usw-pr-cvs1:/tmp/cvs-serv21026/sre Modified Files: PatternObject.java SRE_STATE.java ScannerObject.java Log Message: CPython-2.2's sre changes to MAGIC==20010701. Index: PatternObject.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/modules/sre/PatternObject.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** PatternObject.java 2001/11/27 19:07:22 1.7 --- PatternObject.java 2002/01/06 15:53:25 1.8 *************** *** 27,30 **** --- 27,31 ---- public int flags; org.python.core.PyObject indexgroup; + public int codesize; *************** *** 32,38 **** int groups, PyObject groupindex, PyObject indexgroup) { ! this.pattern = pattern.toString(); this.flags = flags; this.code = code; this.groups = groups; this.groupindex = groupindex; --- 33,41 ---- int groups, PyObject groupindex, PyObject indexgroup) { ! if (pattern != null) ! this.pattern = pattern.toString(); this.flags = flags; this.code = code; + this.codesize = code.length; this.groups = groups; this.groupindex = groupindex; *************** *** 82,93 **** int count = ap.getInt(2, 0); ! return call("_sub", new PyObject[] { ! Py.java2py(this), ! template, ! Py.newString(string), ! Py.newInteger(count) }); } public PyObject subn(PyObject[] args, String[] kws) { ArgParser ap = new ArgParser("subn", args, kws, --- 85,93 ---- int count = ap.getInt(2, 0); ! return subx(template, string, count, false); } + public PyObject subn(PyObject[] args, String[] kws) { ArgParser ap = new ArgParser("subn", args, kws, *************** *** 97,105 **** int count = ap.getInt(2, 0); ! return call("_subn", new PyObject[] { ! Py.java2py(this), ! template, ! Py.newString(string), ! Py.newInteger(count) }); } --- 97,183 ---- int count = ap.getInt(2, 0); ! return subx(template, string, count, true); ! } ! ! ! private PyObject subx(PyObject template, String string, int count, ! boolean subn) ! { ! PyObject filter = null; ! boolean filter_is_callable = false; ! if (template.isCallable()) { ! filter = template; ! filter_is_callable = true; ! } else { ! boolean literal = false; ! if (template instanceof PyString) { ! literal = template.toString().indexOf('\\') < 0; ! } ! if (literal) { ! filter = template; ! filter_is_callable = false; ! } else { ! filter = call("sre", "_subx", new PyObject[] { ! this, template}); ! filter_is_callable = filter.isCallable(); ! } ! } ! ! SRE_STATE state = new SRE_STATE(string, 0, Integer.MAX_VALUE, flags); ! ! StringBuffer buf = new StringBuffer(); ! ! int n = 0; ! int i = 0; ! ! while (count == 0 || n < count) { ! state.state_reset(); ! state.ptr = state.start; ! int status = state.SRE_SEARCH(code, 0); ! if (status <= 0) { ! if (status == 0) ! break; ! _error(status); ! } ! int b = state.start; ! int e = state.ptr; ! ! if (i < b) { ! /* get segment before this match */ ! buf.append(string.substring(i, b)); ! } ! if (! (i == b && i == e && n > 0)) { ! PyObject item; ! if (filter_is_callable) { ! /* pass match object through filter */ ! MatchObject match = _pattern_new_match(state, string, 1); ! item = filter.__call__(match); ! } else { ! item = filter; ! } ! ! if (item != Py.None) { ! buf.append(item.toString()); ! } ! i = e; ! n++; ! } ! ! /* move on */ ! if (state.ptr == state.start) ! state.start = state.ptr + 1; ! else ! state.start = state.ptr; ! } ! if (i < state.endpos) { ! buf.append(string.substring(i, state.endpos)); ! } ! ! if (subn) ! return new PyTuple(new PyObject[] { ! Py.newString(buf.toString()), Py.newInteger(n) ! }); ! else ! return Py.newString(buf.toString()); } *************** *** 109,122 **** "source", "maxsplit"); String string = ap.getString(0); ! int count = ap.getInt(1, 0); ! return call("_split", new PyObject[] { ! Py.java2py(this), ! Py.newString(string), ! Py.newInteger(count) }); } ! private PyObject call(String function, PyObject[] args) { ! PyObject sre = imp.importName("sre", true); return sre.invoke(function, args); } --- 187,238 ---- "source", "maxsplit"); String string = ap.getString(0); ! int maxsplit = ap.getInt(1, 0); ! SRE_STATE state = new SRE_STATE(string, 0, Integer.MAX_VALUE, flags); ! ! PyList list = new PyList(); ! ! int n = 0; ! int last = state.start; ! while (maxsplit == 0 || n < maxsplit) { ! state.state_reset(); ! state.ptr = state.start; ! int status = state.SRE_SEARCH(code, 0); ! if (status <= 0) { ! if (status == 0) ! break; ! _error(status); ! } ! if (state.start == state.ptr) { ! if (last == state.end) ! break; ! /* skip one character */ ! state.start = state.ptr + 1; ! continue; ! } ! ! /* get segment before this match */ ! PyObject item = Py.newString(string.substring(last, state.start)); ! list.append(item); ! ! for (int i = 0; i < groups; i++) { ! String s = state.getslice(i+1, string, false); ! if (s != null) ! list.append(Py.newString(s)); ! else ! list.append(Py.None); ! } ! n += 1; ! last = state.start = state.ptr; ! } ! ! PyObject item = Py.newString(string.substring(last, state.endpos)); ! list.append(item); ! ! return list; } ! private PyObject call(String module, String function, PyObject[] args) { ! PyObject sre = imp.importName(module, true); return sre.invoke(function, args); } *************** *** 149,158 **** break; case 1: ! item = Py.newString(state.getslice(1, string)); break; default: PyObject[] t = new PyObject[groups]; for (int i = 0; i < groups; i++) ! t[i] = Py.newString(state.getslice(i+1, string)); item = new PyTuple(t); break; --- 265,274 ---- break; case 1: ! item = Py.newString(state.getslice(1, string, true)); break; default: PyObject[] t = new PyObject[groups]; for (int i = 0; i < groups; i++) ! t[i] = Py.newString(state.getslice(i+1, string, true)); item = new PyTuple(t); break; *************** *** 176,179 **** --- 292,311 ---- } + + /* Enable "finditer" when iter support is added. + public PyObject finditer(String string) { + return finditer(string, 0, Integer.MAX_VALUE); + } + + public PyObject finditer(String string, int start) { + return finditer(string, start, Integer.MAX_VALUE); + } + + public PyObject finditer(String string, int start, int end) { + ScannerObject scanner = scanner(string, start, end); + PyObject search = scanner.__findattr__("search"); + return new PyCallIterator(search, Py.None); + } + */ public ScannerObject scanner(String string) { Index: SRE_STATE.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/modules/sre/SRE_STATE.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** SRE_STATE.java 2001/11/27 19:07:22 1.7 --- SRE_STATE.java 2002/01/06 15:53:25 1.8 *************** *** 41,62 **** public static final int SRE_OP_CATEGORY = 9; public static final int SRE_OP_CHARSET = 10; ! public static final int SRE_OP_GROUPREF = 11; ! public static final int SRE_OP_GROUPREF_IGNORE = 12; ! public static final int SRE_OP_IN = 13; ! public static final int SRE_OP_IN_IGNORE = 14; ! public static final int SRE_OP_INFO = 15; ! public static final int SRE_OP_JUMP = 16; ! public static final int SRE_OP_LITERAL = 17; ! public static final int SRE_OP_LITERAL_IGNORE = 18; ! public static final int SRE_OP_MARK = 19; ! public static final int SRE_OP_MAX_UNTIL = 20; ! public static final int SRE_OP_MIN_UNTIL = 21; ! public static final int SRE_OP_NOT_LITERAL = 22; ! public static final int SRE_OP_NOT_LITERAL_IGNORE = 23; ! public static final int SRE_OP_NEGATE = 24; ! public static final int SRE_OP_RANGE = 25; ! public static final int SRE_OP_REPEAT = 26; ! public static final int SRE_OP_REPEAT_ONE = 27; ! public static final int SRE_OP_SUBPATTERN = 28; public static final int SRE_AT_BEGINNING = 0; --- 41,63 ---- public static final int SRE_OP_CATEGORY = 9; public static final int SRE_OP_CHARSET = 10; ! public static final int SRE_OP_BIGCHARSET = 11; ! public static final int SRE_OP_GROUPREF = 12; ! public static final int SRE_OP_GROUPREF_IGNORE = 13; ! public static final int SRE_OP_IN = 14; ! public static final int SRE_OP_IN_IGNORE = 15; ! public static final int SRE_OP_INFO = 16; ! public static final int SRE_OP_JUMP = 17; ! public static final int SRE_OP_LITERAL = 18; ! public static final int SRE_OP_LITERAL_IGNORE = 19; ! public static final int SRE_OP_MARK = 20; ! public static final int SRE_OP_MAX_UNTIL = 21; ! public static final int SRE_OP_MIN_UNTIL = 22; ! public static final int SRE_OP_NOT_LITERAL = 23; ! public static final int SRE_OP_NOT_LITERAL_IGNORE = 24; ! public static final int SRE_OP_NEGATE = 25; ! public static final int SRE_OP_RANGE = 26; ! public static final int SRE_OP_REPEAT = 27; ! public static final int SRE_OP_REPEAT_ONE = 28; ! public static final int SRE_OP_SUBPATTERN = 29; public static final int SRE_AT_BEGINNING = 0; *************** *** 329,332 **** --- 330,344 ---- break; + case SRE_OP_BIGCHARSET: + /* <BIGCHARSET> <blockcount> <256 blockindices> <blocks> */ + int count = set[setidx++]; + int block = set[ch >> 8]; + setidx += 128; + int idx = block*16 + ((ch & 255)>>4); + if ((set[setidx + idx] & (1 << (ch & 15))) != 0) + return ok; + setidx += count*16; + break; + case SRE_OP_CATEGORY: /* <CATEGORY> <code> */ *************** *** 816,819 **** --- 828,832 ---- return i; mark_restore(0, lastmark); + this.lastmark = lastmark; rp.count = count - 1; this.ptr = ptr; *************** *** 859,872 **** /* see if the tail matches */ this.repeat = rp.prev; ! if (pattern[rp.pidx + 2] == 65535) { ! /* unbounded repeat */ ! for (;;) { ! i = SRE_MATCH(pattern, pidx, level + 1); ! if (i != 0 || ptr >= end) ! break; ! this.ptr = ++ptr; ! } ! } else ! i = SRE_MATCH(pattern, pidx, level + 1); if (i != 0) return i; --- 872,876 ---- /* see if the tail matches */ this.repeat = rp.prev; ! i = SRE_MATCH(pattern, pidx, level + 1); if (i != 0) return i; *************** *** 991,994 **** --- 995,1000 ---- this.start = ptr; this.ptr = ++ptr; + if ((flags & SRE_INFO_LITERAL) != 0) + return 1; status = SRE_MATCH(pattern, pidx + 2, 1); if (status != 0) *************** *** 1028,1035 **** - - - - final boolean sre_category(char category, char ch) { switch (category) { --- 1034,1037 ---- *************** *** 1175,1179 **** ! String getslice(int index, String string) { int i, j; --- 1177,1181 ---- ! String getslice(int index, String string, boolean empty) { int i, j; *************** *** 1181,1185 **** if (string == null || mark[index] == -1 || mark[index+1] == -1) { ! i = j = 0; } else { i = mark[index]; --- 1183,1192 ---- if (string == null || mark[index] == -1 || mark[index+1] == -1) { ! if (empty) { ! /* want empty string */ ! i = j = 0; ! } else { ! return null; ! } } else { i = mark[index]; Index: ScannerObject.java =================================================================== RCS file: /cvsroot/jython/jython/org/python/modules/sre/ScannerObject.java,v retrieving revision 1.3 retrieving revision 1.4 diff -C2 -d -r1.3 -r1.4 *** ScannerObject.java 2001/11/27 13:51:37 1.3 --- ScannerObject.java 2002/01/06 15:53:25 1.4 *************** *** 16,20 **** package org.python.modules.sre; ! public class ScannerObject { PatternObject pattern; String string; --- 16,22 ---- package org.python.modules.sre; ! import org.python.core.*; ! ! public class ScannerObject extends PyObject { PatternObject pattern; String string; |