Skip to content

Instantly share code, notes, and snippets.

@jawj
Last active August 29, 2015 14:13
Show Gist options
  • Save jawj/dcba4090e4da511e2385 to your computer and use it in GitHub Desktop.
Save jawj/dcba4090e4da511e2385 to your computer and use it in GitHub Desktop.
String CRC in CoffeeScript
# tested against OS X cksum: `echo -n 'hello' | cksum -o 3` == "hello".crc32()
String.CRC32Table = # ref: http://www.w3.org/TR/PNG/#D-CRCAppendix
for c in [0..255] # (can clobber c with impunity below thanks to CoffeeScript)
for k in [0..7]
c = if c & 1 then 0xedb88320 ^ (c >>> 1) else (c >>> 1)
c >>> 0
String::crc32 = (crc = 0xffffffff) ->
for i in [0...@length] by 1
crc = String.CRC32Table[(crc ^ @charCodeAt i) & 0xff] ^ (crc >>> 8)
(crc ^ 0xffffffff) >>> 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment