From: <cg...@us...> - 2006-08-24 03:56:20
|
Revision: 2906 Author: cgroves Date: 2006-08-23 20:56:12 -0700 (Wed, 23 Aug 2006) ViewCVS: http://svn.sourceforge.net/jython/?rev=2906&view=rev Log Message: ----------- merge in r2862 from 2.3 branch. return unicode if unicode is passed in, str if str is passed in Modified Paths: -------------- trunk/jython/src/org/python/core/PyString.java trunk/jython/src/org/python/core/PyUnicode.java trunk/jython/src/org/python/modules/sre/MatchObject.java trunk/jython/src/org/python/modules/sre/PatternObject.java trunk/jython/src/org/python/modules/sre/ScannerObject.java Modified: trunk/jython/src/org/python/core/PyString.java =================================================================== --- trunk/jython/src/org/python/core/PyString.java 2006-08-24 03:31:03 UTC (rev 2905) +++ trunk/jython/src/org/python/core/PyString.java 2006-08-24 03:56:12 UTC (rev 2906) @@ -3275,7 +3275,7 @@ if (step > 0 && stop < start) stop = start; if (step == 1) - return new PyString(string.substring(start, stop)); + return fromSubstring(start, stop); else { int n = sliceLength(start, stop, step); char new_chars[] = new char[n]; @@ -3283,10 +3283,14 @@ for (int i=start; j<n; i+=step) new_chars[j++] = string.charAt(i); - return new PyString(new String(new_chars)); + return createInstance(new String(new_chars)); } } + public PyString createInstance(String str) { + return new PyString(str); + } + public boolean __contains__(PyObject o) { return str___contains__(o); } @@ -3306,7 +3310,7 @@ for (int i=0; i<count; i++) { string.getChars(0, s, new_chars, i*s); } - return new PyString(new String(new_chars)); + return createInstance(new String(new_chars)); } final PyObject str___mul__(PyObject o) { @@ -3331,11 +3335,10 @@ if (generic_other instanceof PyString) { PyString other = (PyString)generic_other; String result = string.concat(other.string); - if (this instanceof PyUnicode || generic_other instanceof PyUnicode) { + if (generic_other instanceof PyUnicode) { return new PyUnicode(result); - } else { - return new PyString(result); } + return createInstance(result); } else return null; } @@ -3388,10 +3391,9 @@ ); } - //XXX: PyUnicode? public PyObject __mod__(PyObject other) { StringFormatter fmt = new StringFormatter(string); - return new PyString(fmt.format(other)); + return createInstance(fmt.format(other)); } public PyObject __int__() { @@ -3823,7 +3825,7 @@ } protected PyString fromSubstring(int begin, int end) { - return new PyString(string.substring(begin, end)); + return createInstance(string.substring(begin, end)); } public int index(String sub) { @@ -4317,7 +4319,7 @@ } final String str_replace(String oldPiece, String newPiece, int maxsplit) { - PyString newstr = new PyString(newPiece); + PyString newstr = createInstance(newPiece); return newstr.join(split(oldPiece, maxsplit)); } Modified: trunk/jython/src/org/python/core/PyUnicode.java =================================================================== --- trunk/jython/src/org/python/core/PyUnicode.java 2006-08-24 03:31:03 UTC (rev 2905) +++ trunk/jython/src/org/python/core/PyUnicode.java 2006-08-24 03:56:12 UTC (rev 2906) @@ -2751,6 +2751,10 @@ public String safeRepr() throws PyIgnoreMethodTag { return "'unicode' object"; } + + public PyString createInstance(String str){ + return new PyUnicode(str); + } final PyUnicode unicode___unicode__() { return str___unicode__(); Modified: trunk/jython/src/org/python/modules/sre/MatchObject.java =================================================================== --- trunk/jython/src/org/python/modules/sre/MatchObject.java 2006-08-24 03:31:03 UTC (rev 2905) +++ trunk/jython/src/org/python/modules/sre/MatchObject.java 2006-08-24 03:56:12 UTC (rev 2906) @@ -15,11 +15,18 @@ package org.python.modules.sre; -import org.python.core.*; +import org.python.core.ArgParser; +import org.python.core.Py; +import org.python.core.PyDictionary; +import org.python.core.PyInteger; +import org.python.core.PyObject; +import org.python.core.PyString; +import org.python.core.PyTuple; +import org.python.core.imp; public class MatchObject extends PyObject { - public String string; /* link to the target string */ + public PyString string; /* link to the target string */ public PyObject regs; /* cached list of matching spans */ PatternObject pattern; /* link to the regex (pattern) object */ int pos, endpos; /* current target slice */ @@ -173,8 +180,8 @@ if (string == null || start < 0) return def; + return string.__getslice__(Py.newInteger(start), Py.newInteger(end)); - return new PyString(string.substring(start, end)); } public PyObject __findattr__(String key) { Modified: trunk/jython/src/org/python/modules/sre/PatternObject.java =================================================================== --- trunk/jython/src/org/python/modules/sre/PatternObject.java 2006-08-24 03:31:03 UTC (rev 2905) +++ trunk/jython/src/org/python/modules/sre/PatternObject.java 2006-08-24 03:56:12 UTC (rev 2906) @@ -21,7 +21,7 @@ public class PatternObject extends PyObject { char[] code; /* link to the code string object */ - public String pattern; /* link to the pattern source (or None) */ + public PyString pattern; /* link to the pattern source (or None) */ public int groups; public org.python.core.PyObject groupindex; public int flags; @@ -33,7 +33,7 @@ int groups, PyObject groupindex, PyObject indexgroup) { if (pattern != null) - this.pattern = pattern.toString(); + this.pattern = pattern; this.flags = flags; this.code = code; this.codesize = code.length; @@ -42,34 +42,28 @@ this.indexgroup = indexgroup; } - public MatchObject match(String string) { - return match(string, 0, Integer.MAX_VALUE); - } + public MatchObject match(PyObject[] args, String[] kws) { + ArgParser ap = new ArgParser("search", args, kws, + "pattern", "pos", "endpos"); + PyString string = extractPyString(ap, 0); + int start = ap.getInt(1, 0); + int end = ap.getInt(2, string.__len__()); + SRE_STATE state = new SRE_STATE(string.toString(), start, end, flags); - public MatchObject match(String string, int start) { - return match(string, start, Integer.MAX_VALUE); - } - - public MatchObject match(String string, int start, int end) { - SRE_STATE state = new SRE_STATE(string, start, end, flags); - state.ptr = state.start; int status = state.SRE_MATCH(code, 0, 1); return _pattern_new_match(state, string, status); } - - - - + public MatchObject search(PyObject[] args, String[] kws) { ArgParser ap = new ArgParser("search", args, kws, "pattern", "pos", "endpos"); - String string = ap.getString(0); + PyString string = extractPyString(ap, 0); int start = ap.getInt(1, 0); - int end = ap.getInt(2, string.length()); + int end = ap.getInt(2, string.__len__()); - SRE_STATE state = new SRE_STATE(string, start, end, flags); + SRE_STATE state = new SRE_STATE(string.toString(), start, end, flags); int status = state.SRE_SEARCH(code, 0); @@ -81,10 +75,9 @@ ArgParser ap = new ArgParser("sub", args, kws, "repl", "string", "count"); PyObject template = ap.getPyObject(0); - String string = ap.getString(1); int count = ap.getInt(2, 0); - return subx(template, string, count, false); + return subx(template, extractPyString(ap, 1), count, false); } @@ -93,16 +86,16 @@ ArgParser ap = new ArgParser("subn", args, kws, "repl", "string", "count"); PyObject template = ap.getPyObject(0); - String string = ap.getString(1); int count = ap.getInt(2, 0); - return subx(template, string, count, true); + return subx(template, extractPyString(ap, 1), count, true); } - private PyObject subx(PyObject template, String string, int count, + private PyObject subx(PyObject template, PyString instring, int count, boolean subn) { + String string = instring.toString(); PyObject filter = null; boolean filter_is_callable = false; if (template.isCallable()) { @@ -150,7 +143,7 @@ PyObject item; if (filter_is_callable) { /* pass match object through filter */ - MatchObject match = _pattern_new_match(state, string, 1); + MatchObject match = _pattern_new_match(state, instring, 1); item = filter.__call__(match); } else { item = filter; @@ -175,20 +168,20 @@ if (subn) return new PyTuple(new PyObject[] { - Py.newString(buf.toString()), Py.newInteger(n) + instring.createInstance(buf.toString()), Py.newInteger(n) }); else - return Py.newString(buf.toString()); + return instring.createInstance(buf.toString()); } public PyObject split(PyObject[] args, String[] kws) { ArgParser ap = new ArgParser("split", args, kws, "source", "maxsplit"); - String string = ap.getString(0); + PyString string = extractPyString(ap, 0); int maxsplit = ap.getInt(1, 0); - SRE_STATE state = new SRE_STATE(string, 0, Integer.MAX_VALUE, flags); + SRE_STATE state = new SRE_STATE(string.toString(), 0, Integer.MAX_VALUE, flags); PyList list = new PyList(); @@ -212,13 +205,13 @@ } /* get segment before this match */ - PyObject item = Py.newString(string.substring(last, state.start)); + PyObject item = string.__getslice__(Py.newInteger(last), Py.newInteger(state.start)); list.append(item); for (int i = 0; i < groups; i++) { - String s = state.getslice(i+1, string, false); + String s = state.getslice(i+1, string.toString(), false); if (s != null) - list.append(Py.newString(s)); + list.append(string.createInstance(s)); else list.append(Py.None); } @@ -226,8 +219,7 @@ last = state.start = state.ptr; } - PyObject item = Py.newString(string.substring(last, state.endpos)); - list.append(item); + list.append(string.__getslice__(Py.newInteger(last), Py.newInteger(state.endpos))); return list; } @@ -242,11 +234,11 @@ public PyObject findall(PyObject[] args, String[] kws) { ArgParser ap = new ArgParser("findall", args, kws, "source", "pos", "endpos"); - String string = ap.getString(0); + PyString string = extractPyString(ap, 0); int start = ap.getInt(1, 0); int end = ap.getInt(2, Integer.MAX_VALUE); - SRE_STATE state = new SRE_STATE(string, start, end, flags); + SRE_STATE state = new SRE_STATE(string.toString(), start, end, flags); Vector list = new Vector(); @@ -260,16 +252,15 @@ /* don't bother to build a match object */ switch (groups) { case 0: - item = Py.newString( - string.substring(state.start, state.ptr)); + item = string.__getslice__(Py.newInteger(state.start), Py.newInteger(state.ptr)); break; case 1: - item = Py.newString(state.getslice(1, string, true)); + item = string.createInstance(state.getslice(1, string.toString(), 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)); + t[i] = string.createInstance(state.getslice(i+1, string.toString(), true)); item = new PyTuple(t); break; } @@ -291,38 +282,29 @@ return new PyList(list); } - - 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); + public PyObject finditer(PyObject[] args, String[] kws) { + ScannerObject scanner = scanner(args, kws); PyObject search = scanner.__findattr__("search"); return new PyCallIter(search, Py.None); } + + public ScannerObject scanner(PyObject[] args, String[] kws) { + ArgParser ap = new ArgParser("scanner", args, kws, + "pattern", "pos", "endpos"); + PyString string = extractPyString(ap, 0); - public ScannerObject scanner(String string) { - return scanner(string, 0, Integer.MAX_VALUE); - } - - public ScannerObject scanner(String string, int start) { - return scanner(string, start, Integer.MAX_VALUE); - } - - public ScannerObject scanner(String string, int start, int end) { ScannerObject self = new ScannerObject(); - self.state = new SRE_STATE(string, start, end, flags); + self.state = new SRE_STATE(string.toString(), + ap.getInt(1, 0), + ap.getInt(2, Integer.MAX_VALUE), + flags); self.pattern = this; self.string = string; return self; } + private void _error(int status) { if (status == SRE_STATE.SRE_ERROR_RECURSION_LIMIT) throw Py.RuntimeError("maximum recursion limit exceeded"); @@ -331,7 +313,7 @@ } - MatchObject _pattern_new_match(SRE_STATE state, String string, + MatchObject _pattern_new_match(SRE_STATE state, PyString string, int status) { /* create match object (from state object) */ @@ -374,6 +356,14 @@ _error(status); return null; } + + private static PyString extractPyString(ArgParser ap, int pos){ + PyObject obj = ap.getPyObject(pos); + if(!(obj instanceof PyString)){ + throw Py.TypeError("expected str or unicode but got " + obj.getType()); + } + return (PyString)ap.getPyObject(pos); + } } Modified: trunk/jython/src/org/python/modules/sre/ScannerObject.java =================================================================== --- trunk/jython/src/org/python/modules/sre/ScannerObject.java 2006-08-24 03:31:03 UTC (rev 2905) +++ trunk/jython/src/org/python/modules/sre/ScannerObject.java 2006-08-24 03:56:12 UTC (rev 2906) @@ -19,7 +19,7 @@ public class ScannerObject extends PyObject { public PatternObject pattern; - String string; + PyString string; SRE_STATE state; public MatchObject match() { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |