Skip to content

Commit

Permalink
Remove old u8_isvalid
Browse files Browse the repository at this point in the history
  • Loading branch information
ScottPJones committed May 14, 2015
1 parent 17ccfa5 commit dbb1ee8
Showing 1 changed file with 0 additions and 67 deletions.
67 changes: 0 additions & 67 deletions src/support/utf8.c
Original file line number Diff line number Diff line change
Expand Up @@ -615,73 +615,6 @@ size_t u8_printf(const char *fmt, ...)
return cnt;
}

/* based on the valid_utf8 routine from the PCRE library by Philip Hazel
length is in bytes, since without knowing whether the string is valid
it's hard to know how many characters there are! */
DLLEXPORT int u8_isvalid_old(const char *str, size_t length)
{
const unsigned char *p, *pend = (unsigned char*)str + length;
unsigned char c;
int ret = 1; /* ASCII */
int ab;

for (p = (unsigned char*)str; p < pend; p++) {
c = *p;
if (c < 128)
continue;
ret = 2; /* non-ASCII UTF-8 */
if ((c & 0xc0) != 0xc0)
return 0;
ab = trailingBytesForUTF8[c];
if (length < ab)
return 0;
length -= ab;

p++;
/* Check top bits in the second byte */
if ((*p & 0xc0) != 0x80)
return 0;

/* Check for overlong sequences for each different length */
switch (ab) {
/* Check for xx00 000x */
case 1:
if ((c & 0x3e) == 0) return 0;
continue; /* We know there aren't any more bytes to check */

/* Check for 1110 0000, xx0x xxxx */
case 2:
if (c == 0xe0 && (*p & 0x20) == 0) return 0;
break;

/* Check for 1111 0000, xx00 xxxx */
case 3:
if (c == 0xf0 && (*p & 0x30) == 0) return 0;
break;

/* Check for 1111 1000, xx00 0xxx */
case 4:
if (c == 0xf8 && (*p & 0x38) == 0) return 0;
break;

/* Check for leading 0xfe or 0xff,
and then for 1111 1100, xx00 00xx */
case 5:
if (c == 0xfe || c == 0xff ||
(c == 0xfc && (*p & 0x3c) == 0)) return 0;
break;
}

/* Check for valid bytes after the 2nd, if any; all must start 10 */
while (--ab > 0) {
if ((*(++p) & 0xc0) != 0x80) return 0;
}
}

return ret;
}

/* Rewritten completely, original code not based on anything else
length is in bytes, since without knowing whether the string is valid
Expand Down

0 comments on commit dbb1ee8

Please sign in to comment.