This function (written in C for convenience, but this is not important to the question) determines the size of an array. I'm sure it can be converted to an if-else chain or even to an equation, but I am not clever enough to see how. (I tried to write down the obvious if-else chain but got bogged down in cases.)
// 0 <= from <= 0x10FFFF
// 1 <= len <= 0x10FFFF
unsigned int size_for_block(unsigned int from, unsigned int len)
{
unsigned int size = 0;
for (unsigned int i = 0; i < len; i++) {
unsigned int point = from + i;
if (0xD800 <= point && point <= 0xDFFF)
;
else if (point <= 0xFFFF)
size += 1;
else
size += 2;
}
return size;
}
If there is a general, idiotproof technique for converting this kind of loop to arithmetic, that would be an ideal answer. Failing that, the solution for this instance would be fine.
By combining nmclean's answer and the concept from this question, I now have this:
which I have exhaustively tested to always produce the same results as the original, and which clearly shows how to do this sort of thing in the general case.