[Jython-checkins] jython: (Trivial) coding standard adjustments and copyright notice in struct.java.
jeff.allen
jython-checkins at python.org
Tue Apr 9 01:15:01 EDT 2019
https://hg.python.org/jython/rev/ead57cad60e6
changeset: 8236:ead57cad60e6
user: Jeff Allen <ja.py at farowl.co.uk>
date: Mon Apr 08 23:20:18 2019 +0100
summary:
(Trivial) coding standard adjustments and copyright notice in struct.java.
files:
src/org/python/modules/struct.java | 508 +++++++++-------
1 files changed, 283 insertions(+), 225 deletions(-)
diff --git a/src/org/python/modules/struct.java b/src/org/python/modules/struct.java
--- a/src/org/python/modules/struct.java
+++ b/src/org/python/modules/struct.java
@@ -1,14 +1,15 @@
-/*
- * Copyright 1999 Finn Bock.
- *
- * This program contains material copyrighted by:
- * Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
- * The Netherlands.
- */
+// (c)2019 Jython Developers. Licensed to PSF under a Contributor Agreement.
+//
+// First Java version copyright 1999 Finn Bock also contains material Copyright 1991-1995 by
+// Stichting Mathematisch Centrum, Amsterdam, The Netherlands.
package org.python.modules;
+import org.python.core.ClassDictInit;
import org.python.core.Py;
+import org.python.core.Py2kBuffer;
+import org.python.core.PyArray;
+import org.python.core.PyByteArray;
import org.python.core.PyException;
import org.python.core.PyFloat;
import org.python.core.PyList;
@@ -19,11 +20,9 @@
import org.python.core.PyTuple;
import java.math.BigInteger;
-import org.python.core.ClassDictInit;
-import org.python.core.PyArray;
-import org.python.core.PyByteArray;
-import org.python.core.Py2kBuffer;
+
+//@formatter:off
/**
* This module performs conversions between Python values and C
* structs represented as Python strings. It uses <i>format strings</i>
@@ -255,14 +254,13 @@
* @author Finn Bock, bckfnn at pipmail.dknet.dk
* @version struct.java,v 1.6 1999/04/17 12:04:34 fb Exp
*/
+//@formatter:on
public class struct implements ClassDictInit {
- /**
- * Exception raised on various occasions; argument is a
- * string describing what is wrong.
- */
+ /** Exception raised on various occasions; argument is a string describing what is wrong. */
public static final PyObject error = Py.makeClass("error", Py.Exception, exceptionNamespace());
+ //@formatter:off
public static String __doc__ =
"Functions to convert between Python values and C structs.\n" +
"Python strings are used to hold the data representing the C\n" +
@@ -287,9 +285,10 @@
"Whitespace between formats is ignored.\n" +
"\n" +
"The variable struct.error is an exception raised on errors.";
-
+ //@formatter:on
static class FormatDef {
+
char name;
int size;
int alignment;
@@ -301,28 +300,30 @@
return this;
}
- void pack(ByteStream buf, PyObject value) {}
+ void pack(ByteStream buf, PyObject value) {}
Object unpack(ByteStream buf) {
return null;
}
int doPack(ByteStream buf, int count, int pos, PyObject[] args) {
- if (pos + count > args.length)
+ if (pos + count > args.length) {
throw StructError("insufficient arguments to pack");
+ }
int cnt = count;
- while (count-- > 0)
+ while (count-- > 0) {
pack(buf, args[pos++]);
+ }
return cnt;
}
void doUnpack(ByteStream buf, int count, PyList list) {
- while (count-- > 0)
+ while (count-- > 0) {
list.append(Py.java2py(unpack(buf)));
+ }
}
-
int get_int(PyObject value) {
try {
return value.asInt();
@@ -332,42 +333,43 @@
}
long get_long(PyObject value) {
- if (value instanceof PyLong){
+ if (value instanceof PyLong) {
Object v = value.__tojava__(Long.TYPE);
if (v == Py.NoConversion) {
throw StructError("long int too long to convert");
}
return ((Long) v).longValue();
- } else
+ } else {
return get_int(value);
+ }
}
BigInteger get_ulong(PyObject value) {
- if (value instanceof PyLong){
- BigInteger v = (BigInteger)value.__tojava__(BigInteger.class);
- if (v.compareTo(PyLong.MAX_ULONG) > 0){
+ if (value instanceof PyLong) {
+ BigInteger v = (BigInteger) value.__tojava__(BigInteger.class);
+ if (v.compareTo(PyLong.MAX_ULONG) > 0) {
throw StructError("unsigned long int too long to convert");
}
return v;
- } else
+ } else {
return BigInteger.valueOf(get_int(value));
+ }
}
double get_float(PyObject value) {
return value.asDouble();
}
-
void BEwriteInt(ByteStream buf, int v) {
buf.writeByte((v >>> 24) & 0xFF);
buf.writeByte((v >>> 16) & 0xFF);
- buf.writeByte((v >>> 8) & 0xFF);
- buf.writeByte((v >>> 0) & 0xFF);
+ buf.writeByte((v >>> 8) & 0xFF);
+ buf.writeByte((v >>> 0) & 0xFF);
}
void LEwriteInt(ByteStream buf, int v) {
- buf.writeByte((v >>> 0) & 0xFF);
- buf.writeByte((v >>> 8) & 0xFF);
+ buf.writeByte((v >>> 0) & 0xFF);
+ buf.writeByte((v >>> 8) & 0xFF);
buf.writeByte((v >>> 16) & 0xFF);
buf.writeByte((v >>> 24) & 0xFF);
}
@@ -389,8 +391,8 @@
}
}
+ static class ByteStream {
- static class ByteStream {
char[] data;
int len;
int pos;
@@ -404,15 +406,16 @@
ByteStream(String s) {
this(s, 0);
}
-
+
ByteStream(String s, int offset) {
int size = s.length() - offset;
data = new char[size];
s.getChars(offset, s.length(), data, 0);
len = size;
pos = 0;
-
-// System.out.println("s.length()=" + s.length() + ",offset=" + offset + ",size=" + size + ",data=" + Arrays.toString(data));
+
+// System.out.println("s.length()=" + s.length() + ",offset=" + offset + ",size=" + size + ",data="
+// + Arrays.toString(data));
}
int readByte() {
@@ -424,14 +427,12 @@
this.pos += len;
}
-
String readString(int l) {
char[] data = new char[l];
read(data, 0, l);
return new String(data);
}
-
private void ensureCapacity(int l) {
if (pos + l >= data.length) {
char[] b = new char[(pos + l) * 2];
@@ -440,13 +441,11 @@
}
}
-
void writeByte(int b) {
ensureCapacity(1);
- data[pos++] = (char)(b & 0xFF);
+ data[pos++] = (char) (b & 0xFF);
}
-
void write(char[] buf, int pos, int len) {
ensureCapacity(len);
System.arraycopy(buf, pos, data, this.pos, len);
@@ -459,7 +458,6 @@
write(data, 0, len);
}
-
int skip(int l) {
pos += l;
return pos;
@@ -469,317 +467,370 @@
return pos;
}
+ @Override
public String toString() {
return new String(data, 0, pos);
}
}
+ static class PadFormatDef extends FormatDef {
- static class PadFormatDef extends FormatDef {
+ @Override
int doPack(ByteStream buf, int count, int pos, PyObject[] args) {
- while (count-- > 0)
+ while (count-- > 0) {
buf.writeByte(0);
+ }
return 0;
}
+ @Override
void doUnpack(ByteStream buf, int count, PyList list) {
- while (count-- > 0)
+ while (count-- > 0) {
buf.readByte();
+ }
}
}
+ static class StringFormatDef extends FormatDef {
- static class StringFormatDef extends FormatDef {
+ @Override
int doPack(ByteStream buf, int count, int pos, PyObject[] args) {
PyObject value = args[pos];
- if (!(value instanceof PyString))
+ if (!(value instanceof PyString)) {
throw StructError("argument for 's' must be a string");
+ }
String s = value.toString();
int len = s.length();
buf.writeString(s, 0, Math.min(count, len));
if (len < count) {
count -= len;
- for (int i = 0; i < count; i++)
+ for (int i = 0; i < count; i++) {
buf.writeByte(0);
+ }
}
return 1;
}
+ @Override
void doUnpack(ByteStream buf, int count, PyList list) {
list.append(Py.newString(buf.readString(count)));
}
}
+ static class PascalStringFormatDef extends StringFormatDef {
- static class PascalStringFormatDef extends StringFormatDef {
+ @Override
int doPack(ByteStream buf, int count, int pos, PyObject[] args) {
PyObject value = args[pos];
- if (!(value instanceof PyString))
+ if (!(value instanceof PyString)) {
throw StructError("argument for 'p' must be a string");
+ }
- buf.writeByte(Math.min(0xFF, Math.min(value.toString().length(), count-1)));
- return super.doPack(buf, count-1, pos, args);
+ buf.writeByte(Math.min(0xFF, Math.min(value.toString().length(), count - 1)));
+ return super.doPack(buf, count - 1, pos, args);
}
+ @Override
void doUnpack(ByteStream buf, int count, PyList list) {
int n = buf.readByte();
- if (n >= count)
- n = count-1;
+ if (n >= count) {
+ n = count - 1;
+ }
super.doUnpack(buf, n, list);
- buf.skip(Math.max(count-n-1, 0));
+ buf.skip(Math.max(count - n - 1, 0));
}
}
+ static class CharFormatDef extends FormatDef {
- static class CharFormatDef extends FormatDef {
+ @Override
void pack(ByteStream buf, PyObject value) {
- if (!(value instanceof PyString) || value.__len__() != 1)
+ if (!(value instanceof PyString) || value.__len__() != 1) {
throw StructError("char format require string of length 1");
+ }
buf.writeByte(value.toString().charAt(0));
}
+ @Override
Object unpack(ByteStream buf) {
- return Py.newString((char)buf.readByte());
+ return Py.newString((char) buf.readByte());
}
}
+ static class ByteFormatDef extends FormatDef {
- static class ByteFormatDef extends FormatDef {
+ @Override
void pack(ByteStream buf, PyObject value) {
buf.writeByte(get_int(value));
}
+ @Override
Object unpack(ByteStream buf) {
int b = buf.readByte();
- if (b > Byte.MAX_VALUE)
+ if (b > Byte.MAX_VALUE) {
b -= 0x100;
+ }
return Py.newInteger(b);
}
}
static class UnsignedByteFormatDef extends ByteFormatDef {
+
+ @Override
Object unpack(ByteStream buf) {
return Py.newInteger(buf.readByte());
}
}
-
+
static class PointerFormatDef extends FormatDef {
+
FormatDef init(char name) {
String dataModel = System.getProperty("sun.arch.data.model");
- if (dataModel == null)
+ if (dataModel == null) {
throw Py.NotImplementedError("Can't determine if JVM is 32- or 64-bit");
+ }
int length = dataModel.equals("64") ? 8 : 4;
super.init(name, length, length);
return this;
}
-
+
+ @Override
void pack(ByteStream buf, PyObject value) {
throw Py.NotImplementedError("Pointer packing/unpacking not implemented in Jython");
}
+ @Override
Object unpack(ByteStream buf) {
throw Py.NotImplementedError("Pointer packing/unpacking not implemented in Jython");
}
}
-
+
static class LEShortFormatDef extends FormatDef {
+
+ @Override
void pack(ByteStream buf, PyObject value) {
int v = get_int(value);
buf.writeByte(v & 0xFF);
buf.writeByte((v >> 8) & 0xFF);
}
+ @Override
Object unpack(ByteStream buf) {
- int v = buf.readByte() |
- (buf.readByte() << 8);
- if (v > Short.MAX_VALUE)
- v -= 0x10000 ;
+ int v = buf.readByte() | (buf.readByte() << 8);
+ if (v > Short.MAX_VALUE) {
+ v -= 0x10000;
+ }
return Py.newInteger(v);
}
}
static class LEUnsignedShortFormatDef extends LEShortFormatDef {
+
+ @Override
Object unpack(ByteStream buf) {
- int v = buf.readByte() |
- (buf.readByte() << 8);
+ int v = buf.readByte() | (buf.readByte() << 8);
return Py.newInteger(v);
}
}
+ static class BEShortFormatDef extends FormatDef {
- static class BEShortFormatDef extends FormatDef {
+ @Override
void pack(ByteStream buf, PyObject value) {
int v = get_int(value);
buf.writeByte((v >> 8) & 0xFF);
buf.writeByte(v & 0xFF);
}
+ @Override
Object unpack(ByteStream buf) {
- int v = (buf.readByte() << 8) |
- buf.readByte();
- if (v > Short.MAX_VALUE)
+ int v = (buf.readByte() << 8) | buf.readByte();
+ if (v > Short.MAX_VALUE) {
v -= 0x10000;
+ }
return Py.newInteger(v);
}
}
+ static class BEUnsignedShortFormatDef extends BEShortFormatDef {
- static class BEUnsignedShortFormatDef extends BEShortFormatDef {
+ @Override
Object unpack(ByteStream buf) {
- int v = (buf.readByte() << 8) |
- buf.readByte();
+ int v = (buf.readByte() << 8) | buf.readByte();
return Py.newInteger(v);
}
}
+ static class LEIntFormatDef extends FormatDef {
- static class LEIntFormatDef extends FormatDef {
+ @Override
void pack(ByteStream buf, PyObject value) {
LEwriteInt(buf, get_int(value));
}
+ @Override
Object unpack(ByteStream buf) {
int v = LEreadInt(buf);
return Py.newInteger(v);
}
}
+ static class LEUnsignedIntFormatDef extends FormatDef {
- static class LEUnsignedIntFormatDef extends FormatDef {
+ @Override
void pack(ByteStream buf, PyObject value) {
- LEwriteInt(buf, (int)(get_long(value) & 0xFFFFFFFF));
+ LEwriteInt(buf, (int) (get_long(value) & 0xFFFFFFFF));
}
+ @Override
Object unpack(ByteStream buf) {
long v = LEreadInt(buf);
- if (v < 0)
+ if (v < 0) {
v += 0x100000000L;
+ }
return new PyLong(v);
}
}
+ static class BEIntFormatDef extends FormatDef {
- static class BEIntFormatDef extends FormatDef {
+ @Override
void pack(ByteStream buf, PyObject value) {
BEwriteInt(buf, get_int(value));
}
+ @Override
Object unpack(ByteStream buf) {
return Py.newInteger(BEreadInt(buf));
}
}
+ static class BEUnsignedIntFormatDef extends FormatDef {
- static class BEUnsignedIntFormatDef extends FormatDef {
+ @Override
void pack(ByteStream buf, PyObject value) {
- BEwriteInt(buf, (int)(get_long(value) & 0xFFFFFFFF));
+ BEwriteInt(buf, (int) (get_long(value) & 0xFFFFFFFF));
}
+
+ @Override
Object unpack(ByteStream buf) {
long v = BEreadInt(buf);
- if (v < 0)
+ if (v < 0) {
v += 0x100000000L;
+ }
return new PyLong(v);
}
}
static class LEUnsignedLongFormatDef extends FormatDef {
+
+ @Override
void pack(ByteStream buf, PyObject value) {
BigInteger bi = get_ulong(value);
if (bi.compareTo(BigInteger.valueOf(0)) < 0) {
throw StructError("can't convert negative long to unsigned");
}
long lvalue = bi.longValue(); // underflow is OK -- the bits are correct
- int high = (int) ( (lvalue & 0xFFFFFFFF00000000L)>>32 );
- int low = (int) ( lvalue & 0x00000000FFFFFFFFL );
- LEwriteInt( buf, low );
- LEwriteInt( buf, high );
+ int high = (int) ((lvalue & 0xFFFFFFFF00000000L) >> 32);
+ int low = (int) (lvalue & 0x00000000FFFFFFFFL);
+ LEwriteInt(buf, low);
+ LEwriteInt(buf, high);
}
+ @Override
Object unpack(ByteStream buf) {
- long low = ( LEreadInt( buf ) & 0X00000000FFFFFFFFL );
- long high = ( LEreadInt( buf ) & 0X00000000FFFFFFFFL );
- java.math.BigInteger result=java.math.BigInteger.valueOf(high);
- result=result.multiply(java.math.BigInteger.valueOf(0x100000000L));
- result=result.add(java.math.BigInteger.valueOf(low));
+ long low = (LEreadInt(buf) & 0X00000000FFFFFFFFL);
+ long high = (LEreadInt(buf) & 0X00000000FFFFFFFFL);
+ java.math.BigInteger result = java.math.BigInteger.valueOf(high);
+ result = result.multiply(java.math.BigInteger.valueOf(0x100000000L));
+ result = result.add(java.math.BigInteger.valueOf(low));
return new PyLong(result);
}
}
+ static class BEUnsignedLongFormatDef extends FormatDef {
- static class BEUnsignedLongFormatDef extends FormatDef {
+ @Override
void pack(ByteStream buf, PyObject value) {
BigInteger bi = get_ulong(value);
if (bi.compareTo(BigInteger.valueOf(0)) < 0) {
throw StructError("can't convert negative long to unsigned");
}
long lvalue = bi.longValue(); // underflow is OK -- the bits are correct
- int high = (int) ( (lvalue & 0xFFFFFFFF00000000L)>>32 );
- int low = (int) ( lvalue & 0x00000000FFFFFFFFL );
- BEwriteInt( buf, high );
- BEwriteInt( buf, low );
+ int high = (int) ((lvalue & 0xFFFFFFFF00000000L) >> 32);
+ int low = (int) (lvalue & 0x00000000FFFFFFFFL);
+ BEwriteInt(buf, high);
+ BEwriteInt(buf, low);
}
+ @Override
Object unpack(ByteStream buf) {
- long high = ( BEreadInt( buf ) & 0X00000000FFFFFFFFL );
- long low = ( BEreadInt( buf ) & 0X00000000FFFFFFFFL );
- java.math.BigInteger result=java.math.BigInteger.valueOf(high);
- result=result.multiply(java.math.BigInteger.valueOf(0x100000000L));
- result=result.add(java.math.BigInteger.valueOf(low));
+ long high = (BEreadInt(buf) & 0X00000000FFFFFFFFL);
+ long low = (BEreadInt(buf) & 0X00000000FFFFFFFFL);
+ java.math.BigInteger result = java.math.BigInteger.valueOf(high);
+ result = result.multiply(java.math.BigInteger.valueOf(0x100000000L));
+ result = result.add(java.math.BigInteger.valueOf(low));
return new PyLong(result);
}
}
+ static class LELongFormatDef extends FormatDef {
- static class LELongFormatDef extends FormatDef {
+ @Override
void pack(ByteStream buf, PyObject value) {
- long lvalue = get_long( value );
- int high = (int) ( (lvalue & 0xFFFFFFFF00000000L)>>32 );
- int low = (int) ( lvalue & 0x00000000FFFFFFFFL );
- LEwriteInt( buf, low );
- LEwriteInt( buf, high );
+ long lvalue = get_long(value);
+ int high = (int) ((lvalue & 0xFFFFFFFF00000000L) >> 32);
+ int low = (int) (lvalue & 0x00000000FFFFFFFFL);
+ LEwriteInt(buf, low);
+ LEwriteInt(buf, high);
}
+ @Override
Object unpack(ByteStream buf) {
long low = LEreadInt(buf) & 0x00000000FFFFFFFFL;
- long high = ((long)(LEreadInt(buf))<<32) & 0xFFFFFFFF00000000L;
- long result=(high|low);
+ long high = ((long) (LEreadInt(buf)) << 32) & 0xFFFFFFFF00000000L;
+ long result = (high | low);
return new PyLong(result);
}
}
+ static class BELongFormatDef extends FormatDef {
- static class BELongFormatDef extends FormatDef {
+ @Override
void pack(ByteStream buf, PyObject value) {
- long lvalue = get_long( value );
- int high = (int) ( (lvalue & 0xFFFFFFFF00000000L)>>32 );
- int low = (int) ( lvalue & 0x00000000FFFFFFFFL );
- BEwriteInt( buf, high );
- BEwriteInt( buf, low );
+ long lvalue = get_long(value);
+ int high = (int) ((lvalue & 0xFFFFFFFF00000000L) >> 32);
+ int low = (int) (lvalue & 0x00000000FFFFFFFFL);
+ BEwriteInt(buf, high);
+ BEwriteInt(buf, low);
}
+ @Override
Object unpack(ByteStream buf) {
- long high = ((long)(BEreadInt(buf))<<32) & 0xFFFFFFFF00000000L;
+ long high = ((long) (BEreadInt(buf)) << 32) & 0xFFFFFFFF00000000L;
long low = BEreadInt(buf) & 0x00000000FFFFFFFFL;
- long result=(high|low);
+ long result = (high | low);
return new PyLong(result);
}
}
+ static class LEFloatFormatDef extends FormatDef {
- static class LEFloatFormatDef extends FormatDef {
+ @Override
void pack(ByteStream buf, PyObject value) {
- int bits = Float.floatToIntBits((float)get_float(value));
+ int bits = Float.floatToIntBits((float) get_float(value));
LEwriteInt(buf, bits);
}
+ @Override
Object unpack(ByteStream buf) {
int bits = LEreadInt(buf);
float v = Float.intBitsToFloat(bits);
- if (PyFloat.float_format == PyFloat.Format.UNKNOWN && (
- Float.isInfinite(v) || Float.isNaN(v))) {
+ if (PyFloat.float_format == PyFloat.Format.UNKNOWN
+ && (Float.isInfinite(v) || Float.isNaN(v))) {
throw Py.ValueError("can't unpack IEEE 754 special value on non-IEEE platform");
}
return Py.newFloat(v);
@@ -787,36 +838,40 @@
}
static class LEDoubleFormatDef extends FormatDef {
+
+ @Override
void pack(ByteStream buf, PyObject value) {
long bits = Double.doubleToLongBits(get_float(value));
- LEwriteInt(buf, (int)(bits & 0xFFFFFFFF));
- LEwriteInt(buf, (int)(bits >>> 32));
+ LEwriteInt(buf, (int) (bits & 0xFFFFFFFF));
+ LEwriteInt(buf, (int) (bits >>> 32));
}
+ @Override
Object unpack(ByteStream buf) {
- long bits = (LEreadInt(buf) & 0xFFFFFFFFL) +
- (((long)LEreadInt(buf)) << 32);
+ long bits = (LEreadInt(buf) & 0xFFFFFFFFL) + (((long) LEreadInt(buf)) << 32);
double v = Double.longBitsToDouble(bits);
- if (PyFloat.double_format == PyFloat.Format.UNKNOWN &&
- (Double.isInfinite(v) || Double.isNaN(v))) {
+ if (PyFloat.double_format == PyFloat.Format.UNKNOWN
+ && (Double.isInfinite(v) || Double.isNaN(v))) {
throw Py.ValueError("can't unpack IEEE 754 special value on non-IEEE platform");
}
return Py.newFloat(v);
}
}
+ static class BEFloatFormatDef extends FormatDef {
- static class BEFloatFormatDef extends FormatDef {
+ @Override
void pack(ByteStream buf, PyObject value) {
- int bits = Float.floatToIntBits((float)get_float(value));
+ int bits = Float.floatToIntBits((float) get_float(value));
BEwriteInt(buf, bits);
}
+ @Override
Object unpack(ByteStream buf) {
int bits = BEreadInt(buf);
float v = Float.intBitsToFloat(bits);
- if (PyFloat.float_format == PyFloat.Format.UNKNOWN && (
- Float.isInfinite(v) || Float.isNaN(v))) {
+ if (PyFloat.float_format == PyFloat.Format.UNKNOWN
+ && (Float.isInfinite(v) || Float.isNaN(v))) {
throw Py.ValueError("can't unpack IEEE 754 special value on non-IEEE platform");
}
return Py.newFloat(v);
@@ -824,25 +879,27 @@
}
static class BEDoubleFormatDef extends FormatDef {
+
+ @Override
void pack(ByteStream buf, PyObject value) {
long bits = Double.doubleToLongBits(get_float(value));
- BEwriteInt(buf, (int)(bits >>> 32));
- BEwriteInt(buf, (int)(bits & 0xFFFFFFFF));
+ BEwriteInt(buf, (int) (bits >>> 32));
+ BEwriteInt(buf, (int) (bits & 0xFFFFFFFF));
}
+ @Override
Object unpack(ByteStream buf) {
- long bits = (((long) BEreadInt(buf)) << 32) +
- (BEreadInt(buf) & 0xFFFFFFFFL);
+ long bits = (((long) BEreadInt(buf)) << 32) + (BEreadInt(buf) & 0xFFFFFFFFL);
double v = Double.longBitsToDouble(bits);
- if (PyFloat.double_format == PyFloat.Format.UNKNOWN &&
- (Double.isInfinite(v) || Double.isNaN(v))) {
+ if (PyFloat.double_format == PyFloat.Format.UNKNOWN
+ && (Double.isInfinite(v) || Double.isNaN(v))) {
throw Py.ValueError("can't unpack IEEE 754 special value on non-IEEE platform");
}
return Py.newFloat(v);
}
}
-
+ //@formatter:off
private static FormatDef[] lilendian_table = {
new PadFormatDef() .init('x', 1, 0),
new ByteFormatDef() .init('b', 1, 0),
@@ -900,70 +957,66 @@
new BEDoubleFormatDef() .init('d', 8, 8),
new PointerFormatDef() .init('P')
};
-
-
+ //@formatter:on
static FormatDef[] whichtable(String pfmt) {
char c = pfmt.charAt(0);
switch (c) {
- case '<' :
- return lilendian_table;
- case '>':
- case '!':
- // Network byte order is big-endian
- return bigendian_table;
- case '=':
- return bigendian_table;
- case '@':
- default:
- return native_table;
+ case '<':
+ return lilendian_table;
+ case '>':
+ case '!':
+ // Network byte order is big-endian
+ return bigendian_table;
+ case '=':
+ return bigendian_table;
+ case '@':
+ default:
+ return native_table;
}
}
-
private static FormatDef getentry(char c, FormatDef[] f) {
for (int i = 0; i < f.length; i++) {
- if (f[i].name == c)
+ if (f[i].name == c) {
return f[i];
+ }
}
throw StructError("bad char in struct format");
}
-
-
private static int align(int size, FormatDef e) {
if (e.alignment != 0) {
- size = ((size + e.alignment - 1)
- / e.alignment)
- * e.alignment;
+ size = ((size + e.alignment - 1) / e.alignment) * e.alignment;
}
return size;
}
-
-
static int calcsize(String format, FormatDef[] f) {
int size = 0;
int len = format.length();
for (int j = 0; j < len; j++) {
char c = format.charAt(j);
- if (j == 0 && (c=='@' || c=='<' || c=='>' || c=='=' || c=='!'))
+ if (j == 0 && (c == '@' || c == '<' || c == '>' || c == '=' || c == '!')) {
continue;
- if (Character.isWhitespace(c))
+ }
+ if (Character.isWhitespace(c)) {
continue;
+ }
int num = 1;
if (Character.isDigit(c)) {
num = Character.digit(c, 10);
- while (++j < len &&
- Character.isDigit((c = format.charAt(j)))) {
- int x = num*10 + Character.digit(c, 10);
- if (x/10 != num)
+ while (++j < len && Character.isDigit((c = format.charAt(j)))) {
+ int x = num * 10 + Character.digit(c, 10);
+ if (x / 10 != num) {
throw StructError("overflow in item count");
+ }
num = x;
}
- if (j >= len)
+ if (j >= len) {
break;
+ }
}
FormatDef e = getentry(c, f);
@@ -972,69 +1025,70 @@
size = align(size, e);
int x = num * itemsize;
size += x;
- if (x/itemsize != num || size < 0)
+ if (x / itemsize != num || size < 0) {
throw StructError("total struct size too long");
+ }
}
return size;
}
-
/**
- * Return the size of the struct (and hence of the string)
- * corresponding to the given format.
+ * Return the size of the struct (and hence of the string) corresponding to the given format.
*/
static public int calcsize(String format) {
FormatDef[] f = whichtable(format);
return calcsize(format, f);
}
-
/**
- * Return a string containing the values v1, v2, ... packed according
- * to the given format. The arguments must match the
- * values required by the format exactly.
+ * Return a string containing the values v1, v2, ... packed according to the given format. The
+ * arguments must match the values required by the format exactly.
*/
static public PyString pack(PyObject[] args) {
- if (args.length < 1)
+ if (args.length < 1) {
Py.TypeError("illegal argument type for built-in operation");
+ }
String format = args[0].toString();
FormatDef[] f = whichtable(format);
int size = calcsize(format, f);
-
+
return new PyString(pack(format, f, size, 1, args).toString());
}
-
+
// xxx - may need to consider doing a generic arg parser here
static public void pack_into(PyObject[] args) {
- if (args.length < 3)
+ if (args.length < 3) {
Py.TypeError("illegal argument type for built-in operation");
+ }
String format = args[0].toString();
FormatDef[] f = whichtable(format);
int size = calcsize(format, f);
pack_into(format, f, size, 1, args);
}
-
+
static void pack_into(String format, FormatDef[] f, int size, int argstart, PyObject[] args) {
- if (args.length - argstart < 2)
+ if (args.length - argstart < 2) {
Py.TypeError("illegal argument type for built-in operation");
+ }
if (!(args[argstart] instanceof PyArray)) {
throw Py.TypeError("pack_into takes an array arg"); // as well as a buffer, what else?
}
- PyArray buffer = (PyArray)args[argstart];
+ PyArray buffer = (PyArray) args[argstart];
int offset = args[argstart + 1].asInt();
ByteStream res = pack(format, f, size, argstart + 2, args);
if (res.pos > buffer.__len__()) {
- throw StructError("pack_into requires a buffer of at least " + res.pos + " bytes, got " + buffer.__len__());
+ throw StructError("pack_into requires a buffer of at least " + res.pos + " bytes, got "
+ + buffer.__len__());
}
for (int i = 0; i < res.pos; i++, offset++) {
char val = res.data[i];
buffer.set(offset, val);
}
}
-
+
static ByteStream pack(String format, FormatDef[] f, int size, int start, PyObject[] args) {
ByteStream res = new ByteStream();
@@ -1042,64 +1096,68 @@
int len = format.length();
for (int j = 0; j < len; j++) {
char c = format.charAt(j);
- if (j == 0 && (c=='@' || c=='<' || c=='>' || c=='=' || c=='!'))
+ if (j == 0 && (c == '@' || c == '<' || c == '>' || c == '=' || c == '!')) {
continue;
- if (Character.isWhitespace(c))
+ }
+ if (Character.isWhitespace(c)) {
continue;
+ }
int num = 1;
if (Character.isDigit(c)) {
num = Character.digit(c, 10);
- while (++j < len && Character.isDigit((c = format.charAt(j))))
- num = num*10 + Character.digit(c, 10);
- if (j >= len)
+ while (++j < len && Character.isDigit((c = format.charAt(j)))) {
+ num = num * 10 + Character.digit(c, 10);
+ }
+ if (j >= len) {
break;
+ }
}
FormatDef e = getentry(c, f);
// Fill pad bytes with zeros
int nres = align(res.size(), e) - res.size();
- while (nres-- > 0)
+ while (nres-- > 0) {
res.writeByte(0);
+ }
i += e.doPack(res, num, i, args);
}
- if (i < args.length)
+ if (i < args.length) {
throw StructError("too many arguments for pack format");
+ }
return res;
}
-
+ /**
+ * Unpack the string (presumably packed by pack(fmt, ...)) according to the given format. The
+ * result is a tuple even if it contains exactly one item. The string must contain exactly the
+ * amount of data required by the format (i.e. len(string) must equal calcsize(fmt)).
+ */
- /**
- * Unpack the string (presumably packed by pack(fmt, ...)) according
- * to the given format. The result is a tuple even if it contains
- * exactly one item.
- * The string must contain exactly the amount of data required by
- * the format (i.e. len(string) must equal calcsize(fmt)).
- */
-
public static PyTuple unpack(String format, String string) {
FormatDef[] f = whichtable(format);
int size = calcsize(format, f);
int len = string.length();
- if (size != len)
+ if (size != len) {
throw StructError("unpack str size does not match format");
- return unpack(f, size, format, new ByteStream(string));
+ }
+ return unpack(f, size, format, new ByteStream(string));
}
-
+
public static PyTuple unpack(String format, PyArray buffer) {
String string = buffer.tostring();
FormatDef[] f = whichtable(format);
int size = calcsize(format, f);
int len = string.length();
- if (size != len)
+ if (size != len) {
throw StructError("unpack str size does not match format");
- return unpack(f, size, format, new ByteStream(string));
+ }
+ return unpack(f, size, format, new ByteStream(string));
}
-
- public static PyTuple unpack(String format, Py2kBuffer buffer){
+
+ public static PyTuple unpack(String format, Py2kBuffer buffer) {
return unpack(format, buffer.toString());
}
@@ -1125,47 +1183,48 @@
}
public static PyTuple unpack_from(String format, String string) {
- return unpack_from(format, string, 0);
+ return unpack_from(format, string, 0);
}
-
+
public static PyTuple unpack_from(String format, String string, int offset) {
FormatDef[] f = whichtable(format);
int size = calcsize(format, f);
int len = string.length();
- if (size >= (len - offset + 1))
+ if (size >= (len - offset + 1)) {
throw StructError("unpack_from str size does not match format");
+ }
return unpack(f, size, format, new ByteStream(string, offset));
}
-
+
static PyTuple unpack(FormatDef[] f, int size, String format, ByteStream str) {
PyList res = new PyList();
int flen = format.length();
for (int j = 0; j < flen; j++) {
char c = format.charAt(j);
- if (j == 0 && (c=='@' || c=='<' || c=='>' || c=='=' || c=='!'))
+ if (j == 0 && (c == '@' || c == '<' || c == '>' || c == '=' || c == '!')) {
continue;
- if (Character.isWhitespace(c))
+ }
+ if (Character.isWhitespace(c)) {
continue;
+ }
int num = 1;
if (Character.isDigit(c)) {
num = Character.digit(c, 10);
- while (++j < flen &&
- Character.isDigit((c = format.charAt(j))))
- num = num*10 + Character.digit(c, 10);
- if (j > flen)
+ while (++j < flen && Character.isDigit((c = format.charAt(j)))) {
+ num = num * 10 + Character.digit(c, 10);
+ }
+ if (j > flen) {
break;
+ }
}
FormatDef e = getentry(c, f);
-
str.skip(align(str.size(), e) - str.size());
-
e.doUnpack(str, num, res);
}
return PyTuple.fromIterable(res);
}
-
static PyException StructError(String explanation) {
return new PyException(error, explanation);
}
@@ -1175,10 +1234,9 @@
dict.__setitem__("__module__", new PyString("struct"));
return dict;
}
-
+
public static void classDictInit(PyObject dict) {
dict.__setitem__("Struct", PyStruct.TYPE);
}
}
-
--
Repository URL: https://hg.python.org/jython
More information about the Jython-checkins
mailing list