Archive for the 'Code' Category

07/20 Romans and Arabs

6. Zero-based arrays

C arrays began at zero to match memory addressing. In a weakly typed scripting language, there’s exactly no reason to do this. There hasn’t been for decades. Can’t we please start the arrays at 1 where God intended?

I dunno what god[s] have to do with numbers and arrays. But while myArray(VIII) would certainly be geeky enough, there’s a reason why the Roman civilisation disappeared – crashed like an 8-bit processor running Flex or CP/M on January 1, 2000, rather – and the Arabs, after millenia of internal and external fights, massacres and general disarray, are still there. They have the zero, and the Romans dinna have it. Of course, arguing – like some did in the comments – that 0-based [which I favour, if you hadn’t noticed yet] are POLS is a bit disingenuous. See the battles that raged in 2000/2001 regarding when the 21st Century started – people expecting the new century to start on ~0, whereas it starts on ~1. Surprises can smack you in the face – or sour a dinner party – at any corner.

07/13 clic clac

Via Bill de Hóra, unicode converter.

Clic clac!

Why go to the web for that? This thing stays out of the way when not needed:

Automatic copy/paste: when you enter the plain text box, it pastes whatever text the clipboard has, and selects it. In the Unicode box, it pastes the content of the clipboard if it matches the \uXXXX format. After conversion, the result is copied back to the clipboard. What say?

Download  Tested on a TiBook 17″ running 10.3.9. Prolly won’t work on MacIntels. Not me faults, Guv’.

07/10 X-Encodings in Erlang/mb

Been hard at it. But I hit a snag when encountering a plain, innocuous-looking sinogram: 內. Pretty harmless, right? D’uh. Big time. Because in giapponese it’s 内, not 內. Friggin’ variants. 0×5167 vs 0×5185 [If you don’t know what I am talking about, it’s okay. In this case, ignorance is bliss!]. Can I slap someone – preferrably not me? Say hello to hasVariant(X) who just joined us. Nyessss…. Anyway, all fixed now – or rather, for now!

A couple of screenshots is worth 1,000 words!

Here’s a screenshot of the source of KanjiTest.html, in SEE. It’s a cross-table of a few sinograms for a slew of encodings, showing the respective code points for each character in all the encodings. The page itself is in utf-8.
KanjiTest.html screenshot

Here’s a screenshot of the source of KanjiTestUTF8.html. An extract of the former table, if you will.
KanjiTestUTF8.html screenshot

Look, Mom, with only one hand: a UTF-16 encoded page showing a table of sinograms, in UTF-16 of course.
KanjiTestUTF16.html screenshot

Ooooh, lookit, no hands! Same sinograms, in Shift-JIS. Damn, I rock!
KanjiTestSJ.html screenshot

It’s not exactly rosy. The code’s a deluxe candidate for refactoring – read, it’s a mess – but is written in a way that can handle easily as many new encodings as you can throw at me, provided you give me a UTF-8/16 to said encoding cross-ref file. The whole yahzoo [case folding data, Big5, CCCII, Shift-JIS, EUC-KR] is stored in dets tables – UTF-16⇔UTF-8 is an algorithm, thus a tad faster. Because this thing, it’s nice to have, but not exactly a Ferrari. The test that produces these tables, it runs in, ahem, er…, 14 seconds? Don’t reach for your gun right now though, because:
A. I am going to work on speed when functionalities are all in and tested
B. Try to do that right now in Erlang :D – good luck!

So I guess slow is better than nothing. But we’ll work on speed.

Erlang

07/08 Erlang multi-byte module, continued

There are still many dusty corners, and some major refactoring to do, but it is going well, very well indeed.

Erlang

07/06 Erlang mode in emacs

(setq load-path (cons "/sw/lib/erlang/lib/tools-2.4/emacs/" load-path))
(setq erlang-root-dir "/sw/lib/erlang/")
(setq exec-path (cons "/sw/lib/erlang/bin/" exec-path))
(require 'erlang-start)
(global-font-lock-mode t)

Copy/paste is always easier than typing it from the Erlang book I bought recently.

Not that I am planning to use it much, as preliminary tests show that the Erlang mode doesn’t bring much more than I already have with SEE [I wrote an Erlang mode for SEE], a local copy of the Erlang docs open in a series of Firefox tabs [to which I added a basic search function in PHP], and four Terminal sessions. Since I have been using Desktop Manager for a few years now, and have seven active desktops – did I hear someone mumble Adult ADD here? – navigating between the different parts of my own toolchain is actually pleasant, as ⌘-Tab to navigate between apps, and ⌥⌘-[1..7] or ⌥⌘-[⇠ ⇢] to navigate between desktops, help tremendously. Like many people of my age and/or with my kind of mileage on computers, I am happy when my hand doesn’t reach for the mouse for a long stretch.

4 terminal sessions

The tall window on the right is the main debugging session, where I compile the module and test the functions in the Erlang shell. The 3 sessions on the left are: a root session to copy the files into the Erlang main libraries folder – so that the mb module is available from everywhere – another Erlang shell, opened from a different directory, to test that the module *is* indeed available and up to date, and a bash shell to to maintenance stuff. Behind these windows are the four .erl/.hrl files that make up mb, opened in SEE. That’s desktop #6. The docs are usually in desktop #2, in Firefox. Here again, navigation is done with the keyboard, with ⌘-1 to ⌘-9. If I need more tabs than that, I tend to open new windows in order to keep all tabs accessible with ⌘-# shortcuts – in which case I’ll switch between windows with ⌘-`. That’s one keyboard shortcut Apple got right. An OS-level shortcut can be a pain, but this one is very cool.

Erlang