Re: c4_Bytes.Modify() mangling data

Brian Kelley <[email protected]>
Newsgroups gmane.comp.db.metakit
Message-ID <[email protected]>
I was inserting strings of length 2 which was why it worked for me. 
Yours were larger.  It turns out that you can't cross the end boundary
when using modify.  So if you are inserting a string of length 10, you
can't insert it into a string of length 9.  Also, if you are inserting
it into a string of length 12, you can only insert at 0 or 1!

Using this, I wrote a safe modify function that handles this boundary
case.  Let me know if it works for you:


import Mk4py
import random

s = Mk4py.storage()
v = s.getas("v[b:B]")
p = Mk4py.property("B", "b")
v.append()
#
# view.modify(byteprop, rownum, string, offset, diff)
#  Store (partial) byte property contents. A non-zero value of diff
removes (<0) or inserts (>0) bytes.

def modify(v, property, rownum, string, offset, diff):
    # safe modify
    val = getattr(v[rownum], property.name)
    l = len(val)
    ls = len(string)
    if  l > ls and offset < l-ls:
        return v.modify(property, rownum, string, offset, diff)
        
    while string:
        chunksize = max(l-offset, 1)
        s1, string = string[:chunksize], string[chunksize:]
        v.modify(property, rownum, s1, offset, len(s1))
        offset += chunksize
        
pystring = v[0].b = "test"

for i in range(2000):
   # Create a random range string
   x = random.randrange(10)
   randstring = str(x)*x

   # insert it at a random position in the text
   pos = max(1, random.randrange(len(v[0].b)) - len(randstring) + 2)
   #v.modify(p, 0, randstring, pos, len(randstring))
   modify(v, p, 0, randstring, pos, len(randstring))
   # Keep a matching python string
   pystring = pystring[:pos] + randstring + pystring[pos:]

   if pystring != v[0].b:
       print v[0].b
       print pystring
       print len(randstring)
       raise "ERROR: string mismatch"
_____________________________________________
Metakit mailing list  -  [email protected]
http://www.equi4.com/mailman/listinfo/metakit
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.