The function will decode an UTF8 character and return the unicode code point. outLength will receive the number of bytes that were decoded. This function is taken from the AngelCode ToolBox.
| 281 | // This function is taken from the AngelCode ToolBox. |
| 282 | // |
| 283 | int asStringDecodeUTF8(const char *encodedBuffer, unsigned int *outLength) |
| 284 | { |
| 285 | const unsigned char *buf = (const unsigned char*)encodedBuffer; |
| 286 | |
| 287 | int value = 0; |
| 288 | int length = -1; |
| 289 | unsigned char byte = buf[0]; |
| 290 | if( (byte & 0x80) == 0 ) |
| 291 | { |
| 292 | // This is the only byte |
| 293 | if( outLength ) *outLength = 1; |
| 294 | return byte; |
| 295 | } |
| 296 | else if( (byte & 0xE0) == 0xC0 ) |
| 297 | { |
| 298 | // There is one more byte |
| 299 | value = int(byte & 0x1F); |
| 300 | length = 2; |
| 301 | |
| 302 | // The value at this moment must not be less than 2, because |
| 303 | // that should have been encoded with one byte only. |
| 304 | if( value < 2 ) |
| 305 | length = -1; |
| 306 | } |
| 307 | else if( (byte & 0xF0) == 0xE0 ) |
| 308 | { |
| 309 | // There are two more bytes |
| 310 | value = int(byte & 0x0F); |
| 311 | length = 3; |
| 312 | } |
| 313 | else if( (byte & 0xF8) == 0xF0 ) |
| 314 | { |
| 315 | // There are three more bytes |
| 316 | value = int(byte & 0x07); |
| 317 | length = 4; |
| 318 | } |
| 319 | |
| 320 | int n = 1; |
| 321 | for( ; n < length; n++ ) |
| 322 | { |
| 323 | byte = buf[n]; |
| 324 | if( (byte & 0xC0) == 0x80 ) |
| 325 | value = (value << 6) + int(byte & 0x3F); |
| 326 | else |
| 327 | break; |
| 328 | } |
| 329 | |
| 330 | if( n == length ) |
| 331 | { |
| 332 | if( outLength ) *outLength = (unsigned)length; |
| 333 | return value; |
| 334 | } |
| 335 | |
| 336 | // The byte sequence isn't a valid UTF-8 byte sequence. |
| 337 | return -1; |
| 338 | } |
| 339 | |
| 340 | // |
no outgoing calls
no test coverage detected