The function will encode the unicode code point into the outEncodedBuffer, and then return the length of the encoded value. If the input value is not a valid unicode code point, then the function will return -1. This function is taken from the AngelCode ToolBox.
| 345 | // This function is taken from the AngelCode ToolBox. |
| 346 | // |
| 347 | int asStringEncodeUTF16(unsigned int value, char *outEncodedBuffer) |
| 348 | { |
| 349 | if( value < 0x10000 ) |
| 350 | { |
| 351 | #ifndef AS_BIG_ENDIAN |
| 352 | outEncodedBuffer[0] = (value & 0xFF); |
| 353 | outEncodedBuffer[1] = ((value >> 8) & 0xFF); |
| 354 | #else |
| 355 | outEncodedBuffer[1] = (value & 0xFF); |
| 356 | outEncodedBuffer[0] = ((value >> 8) & 0xFF); |
| 357 | #endif |
| 358 | return 2; |
| 359 | } |
| 360 | else |
| 361 | { |
| 362 | value -= 0x10000; |
| 363 | int surrogate1 = ((value >> 10) & 0x3FF) + 0xD800; |
| 364 | int surrogate2 = (value & 0x3FF) + 0xDC00; |
| 365 | |
| 366 | #ifndef AS_BIG_ENDIAN |
| 367 | outEncodedBuffer[0] = (surrogate1 & 0xFF); |
| 368 | outEncodedBuffer[1] = ((surrogate1 >> 8) & 0xFF); |
| 369 | outEncodedBuffer[2] = (surrogate2 & 0xFF); |
| 370 | outEncodedBuffer[3] = ((surrogate2 >> 8) & 0xFF); |
| 371 | #else |
| 372 | outEncodedBuffer[1] = (surrogate1 & 0xFF); |
| 373 | outEncodedBuffer[0] = ((surrogate1 >> 8) & 0xFF); |
| 374 | outEncodedBuffer[3] = (surrogate2 & 0xFF); |
| 375 | outEncodedBuffer[2] = ((surrogate2 >> 8) & 0xFF); |
| 376 | #endif |
| 377 | |
| 378 | return 4; |
| 379 | } |
| 380 | } |
| 381 | |
| 382 | |
| 383 | END_AS_NAMESPACE |
no outgoing calls
no test coverage detected