Author: Antonio Cuni <[email protected]>
Branch: faster-rstruct-2
Changeset: r91232:4c598c7f0b3e
Date: 2017-05-10 18:45 +0200
http://bitbucket.org/pypy/pypy/changeset/4c598c7f0b3e/
Log: fix pack_pascal
diff --git a/rpython/rlib/rstruct/standardfmttable.py
b/rpython/rlib/rstruct/standardfmttable.py
--- a/rpython/rlib/rstruct/standardfmttable.py
+++ b/rpython/rlib/rstruct/standardfmttable.py
@@ -40,18 +40,22 @@
fmtiter.result.setitem(fmtiter.pos, c)
fmtiter.advance(1)
-def pack_string(fmtiter, count):
+def _pack_string(fmtiter, string, count):
pos = fmtiter.pos
- string = fmtiter.accept_str_arg()
if len(string) < count:
fmtiter.result.setslice(pos, string)
if fmtiter.needs_zeros:
- for i in range(pos + len(string), count):
- fmtiter.result.setitem(i, '\x00')
+ pos += len(string)
+ for i in range(count - len(string)):
+ fmtiter.result.setitem(pos+i, '\x00')
else:
fmtiter.result.setslice(pos, string[:count])
fmtiter.advance(count)
+def pack_string(fmtiter, count):
+ string = fmtiter.accept_str_arg()
+ _pack_string(fmtiter, string, count)
+
def pack_pascal(fmtiter, count):
string = fmtiter.accept_str_arg()
prefix = len(string)
@@ -60,12 +64,10 @@
if prefix < 0:
raise StructError("bad '0p' in struct format")
if prefix > 255:
- prefixchar = '\xff'
- else:
- prefixchar = chr(prefix)
- fmtiter.result.append(prefixchar)
- fmtiter.result.append_slice(string, 0, prefix)
- fmtiter.result.append_multiple_char('\x00', count - (1 + prefix))
+ prefix = 255
+ fmtiter.result.setitem(fmtiter.pos, chr(prefix))
+ fmtiter.advance(1)
+ _pack_string(fmtiter, string, count-1)
def make_float_packer(size):
def packer(fmtiter):
diff --git a/rpython/rlib/rstruct/test/test_pack.py
b/rpython/rlib/rstruct/test/test_pack.py
--- a/rpython/rlib/rstruct/test/test_pack.py
+++ b/rpython/rlib/rstruct/test/test_pack.py
@@ -102,10 +102,8 @@
assert s == 'hello wo'
def test_pack_pascal(self):
- bigendian = self.endianess == '>'
- fmtiter = FakeFormatIter(bigendian, 'hello')
- standardfmttable.pack_pascal(fmtiter, 8)
- s = fmtiter.result.build()
+ s = self.mypack_fn(standardfmttable.pack_pascal,
+ arg=8, value='hello', size=8)
assert s == '\x05hello\x00\x00'
_______________________________________________
pypy-commit mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-commit