[#3986] Re: Principle of least effort -- another Ruby virtue. — Andrew Hunt <andy@...>

> Principle of Least Effort.

14 messages 2000/07/14

[#4043] What are you using Ruby for? — Dave Thomas <Dave@...>

16 messages 2000/07/16

[#4139] Facilitating Ruby self-propagation with the rig-it autopolymorph application. — Conrad Schneiker <schneik@...>

Hi,

11 messages 2000/07/20

[ruby-talk:04081] Re: unpack and "bn" with n>16

From: Aleksi Niemel<aleksi.niemela@...>
Date: 2000-07-17 12:28:00 UTC
List: ruby-talk #4081
> is there any way to do what i meant to do? aka
> 
> p ["1", "1", "1","1"].pack("b4b2b1b1") 
> #-> [ 0b00010111 ].pack("C")
> #-> "\027"

I guess you can modify the pack.c easily to do successive unpacking (in one
pattern, or one call). You have to move the 'bits' initialization to
function level and count remaining bits explicitly (i&7 would not return it
anymore).

	  case 'b':
	    {
		VALUE bitstr;
		char *t;
		int bits, i;

		if (p[-1] == '*' || len > (send - s) * 8)
		    len = (send - s) * 8;
		bits = 0;
		rb_ary_push(ary, bitstr = rb_str_new(0, len));
		t = RSTRING(bitstr)->ptr;
		for (i=0; i<len; i++) {
		    if (i & 7) bits >>= 1;
		    else bits = *s++;
		    *t++ = (bits & 1) ? '1' : '0';
		}
	    }
	    break;

The packing part isn't much harder, but there you have to remember to
"flush" the 'bitstore' when successive "bN" entries end.

In any case, I suggest that you'd contribute a real patch to matz and name
your 'successive b-entries operated upon single byte' -version to some other
name than 'b'.

I'd also suggest that pack should be left as it is and general class
BitHandling should be introduced with enough capabilities to satisfy most
cryptographical software etc. needs. It would be proper place for speedy
handling of bit packing, unpacking, buffering, counting and different
platforms could be hided behind that abstraction so people wouldn't use slow
8-bit bit-routines on 64-bit (or bigger) machines.

Maybe you can read it from my answer that I don't know any other easy way
than hacking :). One black art, super magic "trick" is yet to be mentioned:
you can use Ruby to code the effect you want ;-].

	- Aleksi

In This Thread

Prev Next