| 58 | namespace StrUtils { |
| 59 | |
| 60 | bool is_utf8(const char * string) |
| 61 | { |
| 62 | if(!string) |
| 63 | return false; |
| 64 | |
| 65 | const unsigned char * bytes = (const unsigned char *)string; |
| 66 | while(*bytes) |
| 67 | { |
| 68 | if( (// ASCII |
| 69 | // use bytes[0] <= 0x7F to allow ASCII control characters |
| 70 | bytes[0] == 0x09 || |
| 71 | bytes[0] == 0x0A || |
| 72 | bytes[0] == 0x0D || |
| 73 | (0x20 <= bytes[0] && bytes[0] <= 0x7E) |
| 74 | ) |
| 75 | ) { |
| 76 | bytes += 1; |
| 77 | continue; |
| 78 | } |
| 79 | |
| 80 | if( (// non-overlong 2-byte |
| 81 | (0xC2 <= bytes[0] && bytes[0] <= 0xDF) && |
| 82 | (0x80 <= bytes[1] && bytes[1] <= 0xBF) |
| 83 | ) |
| 84 | ) { |
| 85 | bytes += 2; |
| 86 | continue; |
| 87 | } |
| 88 | |
| 89 | if( (// excluding overlongs |
| 90 | bytes[0] == 0xE0 && |
| 91 | (0xA0 <= bytes[1] && bytes[1] <= 0xBF) && |
| 92 | (0x80 <= bytes[2] && bytes[2] <= 0xBF) |
| 93 | ) || |
| 94 | (// straight 3-byte |
| 95 | ((0xE1 <= bytes[0] && bytes[0] <= 0xEC) || |
| 96 | bytes[0] == 0xEE || |
| 97 | bytes[0] == 0xEF) && |
| 98 | (0x80 <= bytes[1] && bytes[1] <= 0xBF) && |
| 99 | (0x80 <= bytes[2] && bytes[2] <= 0xBF) |
| 100 | ) || |
| 101 | (// excluding surrogates |
| 102 | bytes[0] == 0xED && |
| 103 | (0x80 <= bytes[1] && bytes[1] <= 0x9F) && |
| 104 | (0x80 <= bytes[2] && bytes[2] <= 0xBF) |
| 105 | ) |
| 106 | ) { |
| 107 | bytes += 3; |
| 108 | continue; |
| 109 | } |
| 110 | |
| 111 | if( (// planes 1-3 |
| 112 | bytes[0] == 0xF0 && |
| 113 | (0x90 <= bytes[1] && bytes[1] <= 0xBF) && |
| 114 | (0x80 <= bytes[2] && bytes[2] <= 0xBF) && |
| 115 | (0x80 <= bytes[3] && bytes[3] <= 0xBF) |
| 116 | ) || |
| 117 | (// planes 4-15 |
no outgoing calls
no test coverage detected