[Jython-checkins] jython: Implement bytearray.join
frank.wierzbicki
jython-checkins at python.org
Tue Jun 5 06:12:33 CEST 2012
http://hg.python.org/jython/rev/376886ea89fc
changeset: 6679:376886ea89fc
user: Jeff Allen <ja...py at farowl.co.uk>
date: Mon May 28 21:15:06 2012 +0100
summary:
Implement bytearray.join
Now scoring 2 failures and 58 errors in test_bytes.py
files:
src/org/python/core/BaseBytes.java | 57 ++++++++++++++++
src/org/python/core/PyByteArray.java | 18 +++++
2 files changed, 75 insertions(+), 0 deletions(-)
diff --git a/src/org/python/core/BaseBytes.java b/src/org/python/core/BaseBytes.java
--- a/src/org/python/core/BaseBytes.java
+++ b/src/org/python/core/BaseBytes.java
@@ -2111,6 +2111,63 @@
}
/**
+ * Almost ready-to-expose implementation of Python <code>join(iterable)</code>. Return ...
+ *
+ * @param iter
+ * @return
+ */
+ final synchronized PyByteArray basebytes_join(Iterable<? extends PyObject> iter) {
+
+ List<View> iterList = new LinkedList<View>();
+ long mysize = this.size;
+ long totalSize = 0;
+ boolean first = true;
+
+ for (PyObject o : iter) {
+ // Scan the iterable into a list, checking type and accumulating size
+ View v = getView(o);
+ if (v == null) {
+ // Unsuitable object to be in this join
+ String fmt = "can only join an iterable of bytes (item %d has type '%.80s')";
+ throw Py.TypeError(String.format(fmt, iterList.size(), o.getType().fastGetName()));
+ }
+ iterList.add(v);
+ totalSize += v.size();
+
+ // Each element after the first is preceded by a copy of this
+ if (!first) {
+ totalSize += mysize;
+ } else {
+ first = false;
+ }
+
+ if (totalSize > Integer.MAX_VALUE) {
+ throw Py.OverflowError("join() result would be too long");
+ }
+ }
+
+ // Load the Views from the iterator into a new PyByteArray
+ PyByteArray result = new PyByteArray((int)totalSize);
+ int p = result.offset; // Copy-to pointer
+ first = true;
+
+ for (View v : iterList) {
+ // Each element after the first is preceded by a copy of this
+ if (!first) {
+ System.arraycopy(storage, offset, result.storage, p, size);
+ p += size;
+ } else {
+ first = false;
+ }
+ // Then the element from the iterable
+ v.copyTo(result.storage, p);
+ p += v.size();
+ }
+
+ return result;
+ }
+
+ /**
* Ready-to-expose implementation of Python <code>rfind( sub [, start [, end ]] )</code>. Return
* the highest index in the byte array where byte sequence <code>sub</code> is found, such that
* <code>sub</code> is contained in the slice <code>[start:end]</code>. Arguments
diff --git a/src/org/python/core/PyByteArray.java b/src/org/python/core/PyByteArray.java
--- a/src/org/python/core/PyByteArray.java
+++ b/src/org/python/core/PyByteArray.java
@@ -1057,6 +1057,24 @@
pyinsert(boundToSequence(index.asIndex()), value);
}
+
+ /**
+ * str.join(iterable)
+ Return a bytearray which is the concatenation of the strings in the iterable <code>iterable</code>. The separator between elements is the string providing this method.
+ *
+ * @param iterable of byte array objects, or objects viewable as such.
+.
+ */
+ public PyByteArray join(PyObject iterable) {
+ return bytearray_join(iterable);
+ }
+
+ @ExposedMethod(doc = BuiltinDocs.bytearray_join_doc)
+ final PyByteArray bytearray_join(PyObject iterable) {
+ return basebytes_join(iterable.asIterable());
+ }
+
+
@ExposedMethod(doc = BuiltinDocs.bytearray___len___doc)
final int bytearray___len__() {
return __len__();
--
Repository URL: http://hg.python.org/jython
More information about the Jython-checkins
mailing list