3 This file is part of the Public Domain C Library (PDCLib).
4 Permission is granted to use, modify, and / or redistribute at will.
10 #include <_PDCLIB_encoding.h>
12 static bool latin1toc32(
13 char32_t **restrict p_outbuf,
14 size_t *restrict p_outsz,
15 const char **restrict p_inbuf,
16 size_t *restrict p_insz,
17 mbstate_t *restrict p_ps
20 while(*p_outsz && *p_insz) {
21 unsigned char c = **p_inbuf;
35 static bool c32tolatin1(
36 char **restrict p_outbuf,
37 size_t *restrict p_outsz,
38 const char32_t **restrict p_inbuf,
39 size_t *restrict p_insz,
40 mbstate_t *restrict p_ps
43 while(*p_outsz && *p_insz) {
44 char32_t c = **p_inbuf;
60 _PDCLIB_charcodec_t _PDCLIB_latin1_codec = {
61 .__mbstoc32s = latin1toc32,
62 .__c32stombs = c32tolatin1,
68 #include <_PDCLIB_test.h>
73 // Valid conversion & back
77 char32_t *c32ptr = &c32out[0];
79 char *chrptr = (char*) &abcde[0];
81 mbstate_t mbs = { 0 };
83 TESTCASE(latin1toc32(&c32ptr, &c32rem, &chrptr, &chrrem, &mbs) == true);
84 TESTCASE(c32rem == 0);
85 TESTCASE(chrrem == 0);
86 TESTCASE(c32ptr == &c32out[5]);
87 TESTCASE(chrptr == &abcde[5]);
88 TESTCASE(c32out[0] == 'a' && c32out[1] == 'b' && c32out[2] == 'c' && \
89 c32out[3] == 'd' && c32out[4] == 'e');
98 TESTCASE(c32tolatin1(&chrptr, &chrrem, &c32ptr, &c32rem, &mbs) == true);
99 TESTCASE(c32rem == 0);
100 TESTCASE(chrrem == 0);
101 TESTCASE(c32ptr == &c32out[5]);
102 TESTCASE(chrptr == &chrout[5]);
103 TESTCASE(memcmp(chrout, abcde, 5) == 0);
105 // Invalid conversions
106 char32_t baduni = 0xFFFE;
111 TESTCASE(c32tolatin1(&chrptr, &chrrem, &c32ptr, &c32rem, &mbs) == false);
112 TESTCASE(c32ptr == &baduni);
113 TESTCASE(c32rem == 1);
114 TESTCASE(chrptr == &chrout[0]);
115 TESTCASE(chrrem == 5);