Skip to content

Instantly share code, notes, and snippets.

@sacko87
Last active August 29, 2015 13:57
Show Gist options
  • Save sacko87/9492912 to your computer and use it in GitHub Desktop.
Save sacko87/9492912 to your computer and use it in GitHub Desktop.
#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;
}
#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