size_t u16_mbsnlen (const uint16_t *s, size_t n) { size_t characters; characters = 0; while (n > 0) { ucs4_t uc; int count = u16_mbtoucr (&uc, s, n); characters++; if (count == -2) break; if (count <= 0) count = 1; s += count; n -= count; } return characters; }
int main () { ucs4_t uc; int ret; /* Test NUL unit input. */ { static const uint16_t input[] = { 0 }; uc = 0xBADFACE; ret = u16_mbtoucr (&uc, input, 1); ASSERT (ret == 1); ASSERT (uc == 0); } /* Test ISO 646 unit input. */ { ucs4_t c; uint16_t buf[1]; for (c = 0; c < 0x80; c++) { buf[0] = c; uc = 0xBADFACE; ret = u16_mbtoucr (&uc, buf, 1); ASSERT (ret == 1); ASSERT (uc == c); } } /* Test BMP unit input. */ { static const uint16_t input[] = { 0x20AC }; uc = 0xBADFACE; ret = u16_mbtoucr (&uc, input, 1); ASSERT (ret == 1); ASSERT (uc == 0x20AC); } /* Test 2-units character input. */ { static const uint16_t input[] = { 0xD835, 0xDD1F }; uc = 0xBADFACE; ret = u16_mbtoucr (&uc, input, 2); ASSERT (ret == 2); ASSERT (uc == 0x1D51F); } /* Test incomplete/invalid 1-unit input. */ { static const uint16_t input[] = { 0xD835 }; uc = 0xBADFACE; ret = u16_mbtoucr (&uc, input, 1); ASSERT (ret == -2); ASSERT (uc == 0xFFFD); } { static const uint16_t input[] = { 0xDD1F }; uc = 0xBADFACE; ret = u16_mbtoucr (&uc, input, 1); ASSERT (ret == -1); ASSERT (uc == 0xFFFD); } return 0; }
DST_UNIT * FUNC (const SRC_UNIT *s, size_t n, DST_UNIT *resultbuf, size_t *lengthp) { const SRC_UNIT *s_end = s + n; /* Output string accumulator. */ DST_UNIT *result; size_t allocated; size_t length; if (resultbuf != NULL) { result = resultbuf; allocated = *lengthp; } else { result = NULL; allocated = 0; } length = 0; /* Invariants: result is either == resultbuf or == NULL or malloc-allocated. If length > 0, then result != NULL. */ while (s < s_end) { ucs4_t uc; int count; /* Fetch a Unicode character from the input string. */ count = u16_mbtoucr (&uc, s, s_end - s); if (count < 0) { if (!(result == resultbuf || result == NULL)) free (result); errno = EILSEQ; return NULL; } s += count; /* Store it in the output string. */ if (length + 1 > allocated) { DST_UNIT *memory; allocated = (allocated > 0 ? 2 * allocated : 12); if (length + 1 > allocated) allocated = length + 1; if (result == resultbuf || result == NULL) memory = (DST_UNIT *) malloc (allocated * sizeof (DST_UNIT)); else memory = (DST_UNIT *) realloc (result, allocated * sizeof (DST_UNIT)); if (memory == NULL) { if (!(result == resultbuf || result == NULL)) free (result); errno = ENOMEM; return NULL; } if (result == resultbuf && length > 0) memcpy ((char *) memory, (char *) result, length * sizeof (DST_UNIT)); result = memory; } result[length++] = uc; } if (length == 0) { if (result == NULL) { /* Return a non-NULL value. NULL means error. */ result = (DST_UNIT *) malloc (1); if (result == NULL) { errno = ENOMEM; return NULL; } } } else if (result != resultbuf && length < allocated) { /* Shrink the allocated memory if possible. */ DST_UNIT *memory; memory = (DST_UNIT *) realloc (result, length * sizeof (DST_UNIT)); if (memory != NULL) result = memory; } *lengthp = length; return result; }