My first ruby script is coming along nicely
Among other things, thanks to the nice folks at #ruby-lang.
Ruby has some really nice schticks. A few of the things I currently love about ruby:
(more…)
Among other things, thanks to the nice folks at #ruby-lang.
Ruby has some really nice schticks. A few of the things I currently love about ruby:
(more…)
I wanted to setup a counter for a friends web site, where all the logic is done in the SQL – the reason is that MySQL offers atomic updates (unlike some [read: most] RDBMS which I won’t name here), so I don’t need to lock anything- If I can do all the logic in one statement I get multi-thread safey for free :-).
(more…)
Its not like I don’t know Ruby
I’ve read about Ruby a lot, and practiced with some code snippets and even started on a large app (some kind of management service with an XML-RPC frontend) – but I haven’t yet written something meaningful with Ruby, something that I can look at and say – “Ye, I can write Ruby real nice now”.
I had the same problem with Python for a while, then I sat down and ported my web-comics-strip-fetching application to Python (and a few other modifications such as a DB backend, split mails, CLI etc’).
When I got down and dirty for a while and came up with a gem – then I can say that I know how to program in Ruby.
That I’ve written so far anyway. I’m sure its possible to write a smaller one using perl or maybe even C, but this is the smallest Java decoder I’ve seen.
private final static char[] base64CharMap = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" .toCharArray(); private final static byte[] base64RevCharMap = new byte[256]; static { for (byte i = 0; i < base64CharMap.length; i++) base64RevCharMap[base64CharMap[i]] = i; } /** * Decode a base64 encoded data back into a byte array * @param text base64 decoded data * @return binary */ public static byte[] base64(String text) { if (text == null) return null; char[] chars = text.trim().toCharArray(); int sind = 0, dind = 0, shl = 0; byte[] out = new byte[chars.length/4*3]; while (sind < chars.length && chars[sind+1] != '=') { out[dind++] = (byte) ( (base64RevCharMap[chars[sind++]] << (shl+2)) | (base64RevCharMap[chars[sind]] >>> (4-shl))); if ((shl = (shl + 2) % 6) == 0) sind++; } return Arrays.copyOf(out, dind); }
Now I need to work on my encoder, which is both a mess and way too big, unlike the decoder above which is just a mess 🙂
Removed wisestring (which was stupid in the first place) in favor of returning a proper byte array. Also cleaned up some syntax.