Skip to content

Instantly share code, notes, and snippets.

@jasonmay
Created March 16, 2011 23:46
Show Gist options
  • Save jasonmay/873563 to your computer and use it in GitHub Desktop.
Save jasonmay/873563 to your computer and use it in GitHub Desktop.
sub attr_unpack {
shift if ref($_[0]); # called in object context, ditch the object
my $data = shift;
my ($num, $fg, $bg, $bo, $fa, $st, $ul, $bl, $rv);
$num = unpack ('S', $data);
($fg, $bg, $bo, $fa, $st, $ul, $bl, $rv) = (
$num & 7,
($num >> 4) & 7,
($num >> 8) & 1,
($num >> 9) & 1,
($num >> 10) & 1,
($num >> 11) & 1,
($num >> 12) & 1,
($num >> 13) & 1
);
return ($fg, $bg, $bo, $fa, $st, $ul, $bl, $rv);
}
SV*
attr_unpack(self, sv_buf)
SV *self
SV *sv_buf
PPCODE:
char *buf = SvPV_nolen(sv_buf);
EXTEND(SP, 8);
mPUSHs( newSViv( buf[0] & 7 ) );
mPUSHs( newSViv( (buf[0] >> 4) & 7) );
mPUSHs( newSViv( buf[1] & 1) );
mPUSHs( newSViv( (buf[1] >> 1) & 1) );
mPUSHs( newSViv( (buf[1] >> 2) & 1) );
mPUSHs( newSViv( (buf[1] >> 3) & 1) );
mPUSHs( newSViv( (buf[1] >> 4) & 1) );
mPUSHs( newSViv( (buf[1] >> 5) & 1) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment