Skip to content Skip to sidebar Skip to footer

How Can I Improve Performance Of Utf32 To Utf16 Surrogate Pair Converter

I am using following converter to do the splits of Unicode chars not in 'normal' plane. function toUTF16Pair(x) { var first = Math.floor((x - 0x10000) / 0x400) + 0xD800;

Solution 1:

As usual did it myself with some binary magic. Please try to beat this.

functiontoUTF16Pair(x) {
    return'\\u' + ((((x - 0x10000) >> 0x0a) | 0x0) + 0xD800).toString(16) 
         + '\\u' + (((x - 0x10000) & 0x3FF) + 0xDC00).toString(16)
}

In case anyone is wondering how it works: >> 0x0a - is binary right shift 10 positions that is equivalent to division by 1024. | 0x0 - is equivalent of Math.floor& 0x3FF - because modulo of 2 can be expressed as (x % n == x & (n - 1)) which in my case is & 1063 in decimal.

Hope this saves you some time.

Post a Comment for "How Can I Improve Performance Of Utf32 To Utf16 Surrogate Pair Converter"