Skip to content

Instantly share code, notes, and snippets.

@Veejay
Created April 10, 2012 17:55
Show Gist options
  • Save Veejay/2353248 to your computer and use it in GitHub Desktop.
Save Veejay/2353248 to your computer and use it in GitHub Desktop.
pike.c
int
mbtowc(wchar_t *p, char *s, size_t n)
{
long l;
int c0, c, nc;
Tab *t;
if(s == 0)
return 0;
nc = 0;
if(n <= nc)
return -1;
c0 = *s & 0xff;
l = c0;
for(t=tab; t->cmask; t++) {
nc++;
if((c0 & t->cmask) == t->cval) {
l &= t->lmask;
if(l < t->lval)
return -1;
*p = l;
return nc;
}
if(n <= nc)
return -1;
s++;
c = (*s ^ 0x80) & 0xFF;
if(c & 0xC0)
return -1;
l = (l<<6) | c;
}
return -1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment