Last active
August 29, 2015 13:57
-
-
Save sacko87/9492912 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "utf16.h" | |
#include <errno.h> | |
uint8_t | |
isutf16(const unsigned char* string) | |
{ | |
uint16_t W1, W2; | |
unsigned char *ptr = (unsigned char*) string; | |
while((W1 = *((uint16_t*) ptr)) != 0x0L) { | |
if(W1 < 0xd800 || W1 > 0xdfff) { | |
ptr += 2; | |
continue; | |
} | |
if(W1 >= 0xd800 && W1 <= 0xdbff) { | |
W2 = *((uint16_t*) (ptr + 2)); | |
if(W2 >= 0xdc00 && W2 <= 0xdfff) { | |
ptr += 4; | |
continue; | |
} | |
} | |
errno = EINVAL; | |
return 0; | |
} | |
return 1; | |
} | |
int32_t | |
utf16len(const unsigned char* string) | |
{ | |
uint16_t W1, W2; | |
int len = 0; | |
unsigned char *ptr = (unsigned char*) string; | |
while((W1 = *((uint16_t*) ptr)) != 0x0L) { | |
if(W1 < 0xd800 || W1 > 0xdfff) { | |
len++; ptr += 2; | |
continue; | |
} | |
if(W1 >= 0xd800 && W1 <= 0xdbff) { | |
W2 = *((uint16_t*) (ptr + 2)); | |
if(W2 >= 0xdc00 && W2 <= 0xdfff) { | |
len++; ptr += 4; | |
continue; | |
} | |
} | |
errno = EINVAL; | |
return -1; | |
} | |
return len; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _UTF_16_H | |
#define _UTF_16_H | |
#include <stdint.h> | |
/** | |
* | |
*/ | |
uint8_t isutf16(const unsigned char*); | |
/** | |
* | |
*/ | |
int32_t utf16len(const unsigned char*); | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment