Hi Folks,
I recently had a go at writing a Factor object to pull bitstrings of
arbitrary length from a bytearray. Since I'm still a bit of a newbie, could
you give any commentary on the coding or the style and how it could be
improved?
USING: kernel locals accessors math sequences math.bitwise ;
TUPLE: bitreader bytearray curpos curbit ;: <bitreader> ( bytearray --
bitreader ) 0 0 bitreader boa ;
: advance ( reader -- reader ) 0 >>curbit [ 1 + ] change-curpos ;
: check-advance ( reader -- reader ) dup curbit>> 8 = [ advance ] when ;
:: readsinglevalue ( bits reader -- result ) reader curpos>> reader
bytearray>> nth
reader curbit>> dup bits
1 - + swap bit-range
reader [ bits + ]
change-curbit check-advance drop ;
: bitsleft ( reader -- result ) curbit>> 8 swap - ;
DEFER: readbits
:: readmultivalue ( bits reader -- result ) reader bitsleft :> bitsextra
bitsextra reader readsinglevalue
bits bitsextra - reader readbits
bitsextra shift + ;
:: readbits ( bits reader -- result ) reader curbit>> bits + 8 < [
bits reader readsinglevalue ] [ bits reader readmultivalue ] if ;
Also, is there any widget for the Factor UI that behaves like a
combo-box or list-box for entering a string?
Thanks very much,
Mark.
------------------------------------------------------------------------------
Download BIRT iHub F-Type - The Free Enterprise-Grade BIRT Server
from Actuate! Instantly Supercharge Your Business Reports and Dashboards
with Interactivity, Sharing, Native Excel Exports, App Integration & more
Get technology previously reserved for billion-dollar corporations, FREE
http://pubads.g.doubleclick.net/gampad/clk?id=164703151&iu=/4140/ostg.clktrk
_______________________________________________
Factor-talk mailing list
Factor-talk@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/factor-talk