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.
| 236 | // This function is taken from the AngelCode ToolBox. |
| 237 | // |
| 238 | int asStringEncodeUTF8(unsigned int value, char *outEncodedBuffer) |
| 239 | { |
| 240 | unsigned char *buf = (unsigned char*)outEncodedBuffer; |
| 241 | |
| 242 | int length = -1; |
| 243 | |
| 244 | if( value <= 0x7F ) |
| 245 | { |
| 246 | buf[0] = static_cast<unsigned char>(value); |
| 247 | return 1; |
| 248 | } |
| 249 | else if( value >= 0x80 && value <= 0x7FF ) |
| 250 | { |
| 251 | // Encode it with 2 characters |
| 252 | buf[0] = static_cast<unsigned char>(0xC0 + (value >> 6)); |
| 253 | length = 2; |
| 254 | } |
| 255 | else if( (value >= 0x800 && value <= 0xD7FF) || (value >= 0xE000 && value <= 0xFFFF) ) |
| 256 | { |
| 257 | // Note: Values 0xD800 to 0xDFFF are not valid unicode characters |
| 258 | buf[0] = static_cast<unsigned char>(0xE0 + (value >> 12)); |
| 259 | length = 3; |
| 260 | } |
| 261 | else if( value >= 0x10000 && value <= 0x10FFFF ) |
| 262 | { |
| 263 | buf[0] = static_cast<unsigned char>(0xF0 + (value >> 18)); |
| 264 | length = 4; |
| 265 | } |
| 266 | |
| 267 | int n = length-1; |
| 268 | for( ; n > 0; n-- ) |
| 269 | { |
| 270 | buf[n] = static_cast<unsigned char>(0x80 + (value & 0x3F)); |
| 271 | value >>= 6; |
| 272 | } |
| 273 | |
| 274 | return length; |
| 275 | } |
| 276 | |
| 277 | // |
| 278 | // The function will decode an UTF8 character and return the unicode code point. |
no outgoing calls
no test coverage detected