Author: Antonio Cuni <anto.c...@gmail.com> Branch: faster-rstruct-2 Changeset: r91268:77c4134d96d9 Date: 2017-05-12 17:36 +0200 http://bitbucket.org/pypy/pypy/changeset/77c4134d96d9/
Log: implement MutableStringBuffer.typed_write diff --git a/rpython/rlib/mutbuffer.py b/rpython/rlib/mutbuffer.py --- a/rpython/rlib/mutbuffer.py +++ b/rpython/rlib/mutbuffer.py @@ -1,7 +1,8 @@ -from rpython.rtyper.lltypesystem import lltype +from rpython.rtyper.lltypesystem import lltype, llmemory from rpython.rtyper.lltypesystem.lloperation import llop from rpython.rtyper.lltypesystem.rstr import STR, mallocstr from rpython.rtyper.annlowlevel import llstr, hlstr +from rpython.rlib.objectmodel import specialize from rpython.rlib.buffer import Buffer class MutableStringBuffer(Buffer): @@ -48,3 +49,12 @@ def setzeros(self, index, count): for i in range(index, index+count): self.setitem(i, '\x00') + + @specialize.ll_and_arg(1) + def typed_write(self, TP, byte_offset, value): + base_ofs = (llmemory.offsetof(STR, 'chars') + + llmemory.itemoffsetof(STR.chars, 0)) + scale_factor = llmemory.sizeof(lltype.Char) + value = lltype.cast_primitive(TP, value) + llop.gc_store_indexed(lltype.Void, self.ll_val, byte_offset, value, + scale_factor, base_ofs) diff --git a/rpython/rlib/test/test_mutbuffer.py b/rpython/rlib/test/test_mutbuffer.py --- a/rpython/rlib/test/test_mutbuffer.py +++ b/rpython/rlib/test/test_mutbuffer.py @@ -1,4 +1,6 @@ import pytest +import struct +from rpython.rtyper.lltypesystem import lltype, rffi from rpython.rlib.mutbuffer import MutableStringBuffer class TestMutableStringBuffer(object): @@ -28,3 +30,13 @@ buf.setslice(0, 'ABCDEFGH') buf.setzeros(2, 3) assert buf.finish() == 'AB\x00\x00\x00FGH' + + def test_typed_write(self): + expected = struct.pack('ifqd', 0x1234, 123.456, 0x12345678, 789.123) + buf = MutableStringBuffer(24) + buf.typed_write(rffi.INT, 0, 0x1234) + buf.typed_write(rffi.FLOAT, 4, 123.456) + buf.typed_write(rffi.LONGLONG, 8, 0x12345678) + buf.typed_write(rffi.DOUBLE, 16, 789.123) + s = buf.finish() + assert s == expected _______________________________________________ pypy-commit mailing list pypy-commit@python.org https://mail.python.org/mailman/listinfo/pypy-commit