| 28 | } VMEnv; |
| 29 | |
| 30 | bool is_str_utf8(const char* str) |
| 31 | { |
| 32 | unsigned int nBytes = 0;//UFT8可用1-6个字节编码,ASCII用一个字节 |
| 33 | unsigned char chr = *str; |
| 34 | bool bAllAscii = true; |
| 35 | for (unsigned int i = 0; str[i] != '\0'; ++i){ |
| 36 | chr = *(str + i); |
| 37 | //判断是否ASCII编码,如果不是,说明有可能是UTF8,ASCII用7位编码,最高位标记为0,0xxxxxxx |
| 38 | if (nBytes == 0 && (chr & 0x80) != 0){ |
| 39 | bAllAscii = false; |
| 40 | } |
| 41 | if (nBytes == 0) { |
| 42 | //如果不是ASCII码,应该是多字节符,计算字节数 |
| 43 | if (chr >= 0x80) { |
| 44 | if (chr >= 0xFC && chr <= 0xFD){ |
| 45 | nBytes = 6; |
| 46 | } |
| 47 | else if (chr >= 0xF8){ |
| 48 | nBytes = 5; |
| 49 | } |
| 50 | else if (chr >= 0xF0){ |
| 51 | nBytes = 4; |
| 52 | } |
| 53 | else if (chr >= 0xE0){ |
| 54 | nBytes = 3; |
| 55 | } |
| 56 | else if (chr >= 0xC0){ |
| 57 | nBytes = 2; |
| 58 | } |
| 59 | else{ |
| 60 | return false; |
| 61 | } |
| 62 | nBytes--; |
| 63 | } |
| 64 | } |
| 65 | else{ |
| 66 | //多字节符的非首字节,应为 10xxxxxx |
| 67 | if ((chr & 0xC0) != 0x80){ |
| 68 | return false; |
| 69 | } |
| 70 | //减到为零为止 |
| 71 | nBytes--; |
| 72 | } |
| 73 | } |
| 74 | //违返UTF8编码规则 |
| 75 | if (nBytes != 0) { |
| 76 | return false; |
| 77 | } |
| 78 | if (bAllAscii){ //如果全部都是ASCII, 也是UTF8 |
| 79 | return true; |
| 80 | } |
| 81 | return true; |
| 82 | } |
| 83 | |
| 84 | JNIEnv *getEnv() { |
| 85 | JNIEnv *env; |