Wednesday, September 9, 2009

double tweet - up to 280 chars tweets!

Update
At least one other person did the same and before this post, here there's the prove.
The algo seems to be almost the same, except the length is probably padded to be module of 2 (\x00 at the end) so you can use double-tweet gadget to decode gareth message as well :)


Via encode template and same bookmark link, I would like to introduce my last simple experiment: WebReflection::double-tweet (just a click to give it a try, another one to remove)

The Concept

Twitter lets us type messages with a maximum of 140 characters. Fortunately, twitter accepts Unicode characters and still fortunately, it is extremely easy to pack two ASCII characters into a single Unicode one. Accordingly, 140 ASCII characters could be packed into 280.

A Fast ASCII pack / unpack

function ASCIIPack(s){
// WebReflection Mit Style License
for(var r = [], i = 0, length = s.length, c; i < length; ++i){
c = s.charCodeAt(i);
r.push(++i < length ? (c << 8) + s.charCodeAt(i) : c);
};
return String.fromCharCode.apply(String, r);
};

function ASCIIUnpack(s){
// WebReflection Mit Style License
for(var r = [], i = 0, length = s.length, c; i < length; ++i){
c = s.charCodeAt(i);
0xff < c ? r.push(c >> 8, c & 0xff) : r.push(c);
};
return String.fromCharCode.apply(String, r);
};
The main reason my code could be faster than others common de/encoder, is the usage of apply over a single String.fromCharCode call. You can try to pack and unpack massive documents but please do not forget I am using this for twitter ;)

Safe Pack - What Is It

I have already tested this technique for a 280 tweet but instantly I realized that except some geek one able to decode the message, nobody could receive, understand, or be involved in that tweet. In few words, I somehow killed twitter beauty, concept, and that is why I have added a safe option.
Basically, targets, specified via @target, keys, specified via #key and urls are preserved by defaults. It is still possible to disable this feature and send a 280 ASCCI characters tweet, but I think it does not make much sense.

About Searches Or Search Engines

Being a tweet search inevitable small, all we need to look for is a combination of the clean word, plus the packed one, trying with and without a space before to be sure that word has not been encoded in a different way. Moreover, if for some reason twitter will implement this search option, something I honestly do not think at all, its internal conversion will be still fast, assuming the search is performed via binary match and that the unpack option is that simple/fast (I know, we are talking about billions or records, that is why I think they'll never do it)

Conclusion

If I am not late with this double-tweet idea, I guess we can add the sixth way to send more than 140 characters via twitter :)

P.S. if you pass a link with some text after the anchor, it will be automatically put in the text area

P.S.

As somebody suggested, with few changes the function could theoretically pack 3 ASCII in a range 0 - 0xFFFFFF (3 bytes per char, UTF-8)
The problem is that JavaScript supports mainly range 0 - 0xFFFF so there is no point to even write down the function ;)

No comments:

Post a Comment