int _RTL_FUNC c16tomb(char *s, char16_t c16) { size_t rv ; if (!s) { return 0 ; } rv = c16rtomb(s,c16, &__getRtlData()->wctomb_st); return (int)rv; }
int main(int argc, char *argv[]) { mbstate_t s; char buf[MB_LEN_MAX + 1]; /* * C/POSIX locale. */ printf("1..1\n"); /* * If the buffer argument is NULL, c16 is implicitly 0, * c16rtomb() resets its internal state. */ assert(c16rtomb(NULL, L'\0', NULL) == 1); assert(c16rtomb(NULL, 0xdc00, NULL) == 1); /* Null wide character. */ memset(&s, 0, sizeof(s)); memset(buf, 0xcc, sizeof(buf)); assert(c16rtomb(buf, 0, &s) == 1); assert((unsigned char)buf[0] == 0 && (unsigned char)buf[1] == 0xcc); /* Latin letter A, internal state. */ assert(c16rtomb(NULL, L'\0', NULL) == 1); assert(c16rtomb(NULL, L'A', NULL) == 1); /* Latin letter A. */ memset(&s, 0, sizeof(s)); memset(buf, 0xcc, sizeof(buf)); assert(c16rtomb(buf, L'A', &s) == 1); assert((unsigned char)buf[0] == 'A' && (unsigned char)buf[1] == 0xcc); /* Unicode character 'Pile of poo'. */ memset(&s, 0, sizeof(s)); memset(buf, 0xcc, sizeof(buf)); assert(c16rtomb(buf, 0xd83d, &s) == 0); assert(c16rtomb(buf, 0xdca9, &s) == (size_t)-1); assert(errno == EILSEQ); assert((unsigned char)buf[0] == 0xcc); /* * ISO8859-1. */ assert(strcmp(setlocale(LC_CTYPE, "en_US.ISO8859-1"), "en_US.ISO8859-1") == 0); /* Unicode character 'Euro sign'. */ memset(&s, 0, sizeof(s)); memset(buf, 0xcc, sizeof(buf)); assert(c16rtomb(buf, 0x20ac, &s) == (size_t)-1); assert(errno == EILSEQ); assert((unsigned char)buf[0] == 0xcc); /* * ISO8859-15. */ assert(strcmp(setlocale(LC_CTYPE, "en_US.ISO8859-15"), "en_US.ISO8859-15") == 0); /* Unicode character 'Euro sign'. */ memset(&s, 0, sizeof(s)); memset(buf, 0xcc, sizeof(buf)); assert(c16rtomb(buf, 0x20ac, &s) == 1); assert((unsigned char)buf[0] == 0xa4 && (unsigned char)buf[1] == 0xcc); /* * UTF-8. */ assert(strcmp(setlocale(LC_CTYPE, "en_US.UTF-8"), "en_US.UTF-8") == 0); /* Unicode character 'Pile of poo'. */ memset(&s, 0, sizeof(s)); memset(buf, 0xcc, sizeof(buf)); assert(c16rtomb(buf, 0xd83d, &s) == 0); assert(c16rtomb(buf, 0xdca9, &s) == 4); assert((unsigned char)buf[0] == 0xf0 && (unsigned char)buf[1] == 0x9f && (unsigned char)buf[2] == 0x92 && (unsigned char)buf[3] == 0xa9 && (unsigned char)buf[4] == 0xcc); /* Invalid code; 'Pile of poo' without the trail surrogate. */ memset(&s, 0, sizeof(s)); memset(buf, 0xcc, sizeof(buf)); assert(c16rtomb(buf, 0xd83d, &s) == 0); assert(c16rtomb(buf, L'A', &s) == (size_t)-1); assert(errno == EILSEQ); assert((unsigned char)buf[0] == 0xcc); /* Invalid code; 'Pile of poo' without the lead surrogate. */ memset(&s, 0, sizeof(s)); memset(buf, 0xcc, sizeof(buf)); assert(c16rtomb(buf, 0xdca9, &s) == (size_t)-1); assert(errno == EILSEQ); assert((unsigned char)buf[0] == 0xcc); printf("ok 1 - c16rtomb()\n"); }
static int do_test (void) { if (setlocale (LC_ALL, "de_DE.UTF-8") == NULL) { puts ("cannot set locale"); return 1; } int result = 0; char32_t c32 = 48; do { if (c32 >= 0xd800 && c32 <= 0xe000) continue; char buf[20]; size_t n1 = c32rtomb (buf, c32, NULL); if (n1 <= 0) { printf ("c32rtomb for U'\\x%" PRIx32 "' failed\n", (uint32_t) c32); result = 1; continue; } char32_t c32out; size_t n2 = mbrtoc32 (&c32out, buf, n1, NULL); if ((ssize_t) n2 < 0) { printf ("mbrtoc32 for U'\\x%" PRIx32 "' failed\n", (uint32_t) c32); result = 1; continue; } if (n2 != n1) { printf ("mbrtoc32 for U'\\x%" PRIx32 "' consumed %zu bytes, not %zu\n", (uint32_t) c32, n2, n1); result = 1; } else if (c32out != c32) { printf ("mbrtoc32 for U'\\x%" PRIx32 "' produced U'\\x%" PRIx32 "\n", (uint32_t) c32, (uint32_t) c32out); result = 1; } char16_t c16; size_t n3 = mbrtoc16 (&c16, buf, n1, NULL); if (n3 != n1) { printf ("mbrtoc16 for U'\\x%" PRIx32 "' did not consume all bytes\n", (uint32_t) c32); result = 1; continue; } if (c32 < 0x10000) { if (c16 != c32) { printf ("mbrtoc16 for U'\\x%" PRIx32 "' produce u'\\x%" PRIx16 "'\n", (uint32_t) c32, (uint16_t) c16); result = 1; continue; } } else { buf[0] = '1'; char16_t c16_2; size_t n4 = mbrtoc16 (&c16_2, buf, 1, NULL); if (n4 != (size_t) -3) { printf ("second mbrtoc16 for U'\\x%" PRIx32 "' did not return -3\n", (uint32_t) c32); result = 1; continue; } if (c32 != (((uint32_t) (c16 - 0xd7c0)) << 10) + (c16_2 - 0xdc00)) { printf ("mbrtoc16 for U'\\x%" PRIx32 "' returns U'\\x%" PRIx32 "\n", (uint32_t) c32, (((uint32_t) (c16 - 0xd7c0)) << 10) + (c16_2 - 0xdc00)); result = 1; continue; } } buf[0] = '\0'; char16_t c16_nul; n3 = mbrtoc16 (&c16_nul, buf, n1, NULL); if (n3 != 0) { printf ("mbrtoc16 for '\\0' returns %zd\n", n3); result = 1; continue; } if (c32 < 0x10000) { size_t n5 = c16rtomb (buf, c16, NULL); if ((ssize_t) n5 < 0) { printf ("c16rtomb for U'\\x%" PRIx32 "' failed with %zd\n", (uint32_t) c32, n5); result = 1; continue; } if (n5 != n1) { printf ("c16rtomb for U'\\x%" PRIx32 "' produced %zu bytes instead of %zu bytes\n", (uint32_t) c32, n5, n1); result = 1; continue; } } } while ((c32 += 0x1111) <= U'\x12000'); return result; }