Merge "Fix wchar_t signedness problems found on x86-64."

This commit is contained in:
Elliott Hughes 2014-05-02 02:31:23 +00:00 committed by Gerrit Code Review
commit 790890fd5c
1 changed files with 10 additions and 11 deletions

View File

@ -198,9 +198,6 @@ size_t mbsrtowcs(wchar_t* dst, const char** src, size_t len, mbstate_t* ps) {
} }
size_t wcrtomb(char* s, wchar_t wc, mbstate_t*) { size_t wcrtomb(char* s, wchar_t wc, mbstate_t*) {
unsigned char lead;
int i, len;
if (s == NULL) { if (s == NULL) {
// Reset to initial shift state (no-op). // Reset to initial shift state (no-op).
return 1; return 1;
@ -216,18 +213,20 @@ size_t wcrtomb(char* s, wchar_t wc, mbstate_t*) {
// We always output the shortest sequence possible. Also specify the // We always output the shortest sequence possible. Also specify the
// first few bits of the first octet, which contains the information // first few bits of the first octet, which contains the information
// about the sequence length. // about the sequence length.
uint8_t lead;
size_t length;
if ((wc & ~0x7f) == 0) { if ((wc & ~0x7f) == 0) {
lead = 0; lead = 0;
len = 1; length = 1;
} else if ((wc & ~0x7ff) == 0) { } else if ((wc & ~0x7ff) == 0) {
lead = 0xc0; lead = 0xc0;
len = 2; length = 2;
} else if ((wc & ~0xffff) == 0) { } else if ((wc & ~0xffff) == 0) {
lead = 0xe0; lead = 0xe0;
len = 3; length = 3;
} else if ((wc & ~0x1fffff) == 0) { } else if ((wc & ~0x1fffff) == 0) {
lead = 0xf0; lead = 0xf0;
len = 4; length = 4;
} else { } else {
errno = EILSEQ; errno = EILSEQ;
return ERR_ILLEGAL_SEQUENCE; return ERR_ILLEGAL_SEQUENCE;
@ -237,13 +236,13 @@ size_t wcrtomb(char* s, wchar_t wc, mbstate_t*) {
// of 6 bits, least significant last. The first octet is // of 6 bits, least significant last. The first octet is
// a special case because it contains the sequence length // a special case because it contains the sequence length
// information. // information.
for (i = len - 1; i > 0; i--) { for (size_t i = length - 1; i > 0; i--) {
s[i] = (wc & 0x3f) | 0x80; s[i] = (wc & 0x3f) | 0x80;
wc >>= 6; wc >>= 6;
} }
*s = (wc & 0xff) | lead; *s = (wc & 0xff) | lead;
return len; return length;
} }
size_t wcsnrtombs(char* dst, const wchar_t** src, size_t nwc, size_t len, mbstate_t* ps) { size_t wcsnrtombs(char* dst, const wchar_t** src, size_t nwc, size_t len, mbstate_t* ps) {
@ -252,7 +251,7 @@ size_t wcsnrtombs(char* dst, const wchar_t** src, size_t nwc, size_t len, mbstat
if (dst == NULL) { if (dst == NULL) {
for (i = o = 0; i < nwc; i++, o += r) { for (i = o = 0; i < nwc; i++, o += r) {
wchar_t wc = (*src)[i]; wchar_t wc = (*src)[i];
if (wc < 0x80) { if (static_cast<uint32_t>(wc) < 0x80) {
// Fast path for plain ASCII characters. // Fast path for plain ASCII characters.
if (wc == 0) { if (wc == 0) {
return o; return o;
@ -270,7 +269,7 @@ size_t wcsnrtombs(char* dst, const wchar_t** src, size_t nwc, size_t len, mbstat
for (i = o = 0; i < nwc && o < len; i++, o += r) { for (i = o = 0; i < nwc && o < len; i++, o += r) {
wchar_t wc = (*src)[i]; wchar_t wc = (*src)[i];
if (wc < 0x80) { if (static_cast<uint32_t>(wc) < 0x80) {
// Fast path for plain ASCII characters. // Fast path for plain ASCII characters.
dst[o] = wc; dst[o] = wc;
if (wc == 0) { if (wc == 0) {