Skip to content

Instantly share code, notes, and snippets.

@grantr grantr/gist:450487
Created Jun 23, 2010

What would you like to do?
# 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
#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;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.