Skip to content

Instantly share code, notes, and snippets.

@grantr
Created June 23, 2010 20:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grantr/450487 to your computer and use it in GitHub Desktop.
Save grantr/450487 to your computer and use it in GitHub Desktop.
# riddle/lib/riddle/client/response.rb
# Return the next integer value from the stream
def next_int
int = next_int_fast(@str, @marker)
@marker += 4
int
end
#TODO should make this independent of endianness (swap makes it only work on little-endian)
inline do |builder|
builder.c <<-EOC
static unsigned long next_int_fast(VALUE str, int mark)
{
char *s;
unsigned long m = 0;
s = RSTRING(str)->ptr + mark;
memcpy((char*)(&m), s, 4);
m = ((((m)&0xFF)<<24) \
|(((m)>>24)&0xFF) \
|(((m)&0x0000FF00)<<8) \
|(((m)&0x00FF0000)>>8) );
return m;
}
EOC
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment