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 *restrict p_outbuf,
14 size_t *restrict p_outsz,
15 const char *restrict *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 *restrict p_outbuf,
37 size_t *restrict p_outsz,
38 const char32_t *restrict *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 struct _PDCLIB_charcodec _PDCLIB_latin1_codec = {
61 .__mbstoc32s = latin1toc32,
62 .__c32stombs = c32tolatin1,
69 #include <_PDCLIB_test.h>
74 // Valid conversion & back
78 char32_t *c32ptr = &c32out[0];
80 char *chrptr = (char*) &abcde[0];
82 mbstate_t mbs = { 0 };
84 TESTCASE(latin1toc32(&c32ptr, &c32rem, &chrptr, &chrrem, &mbs) == true);
85 TESTCASE(c32rem == 0);
86 TESTCASE(chrrem == 0);
87 TESTCASE(c32ptr == &c32out[5]);
88 TESTCASE(chrptr == &abcde[5]);
89 TESTCASE(c32out[0] == 'a' && c32out[1] == 'b' && c32out[2] == 'c' && \
90 c32out[3] == 'd' && c32out[4] == 'e');
99 TESTCASE(c32tolatin1(&chrptr, &chrrem, &c32ptr, &c32rem, &mbs) == true);
100 TESTCASE(c32rem == 0);
101 TESTCASE(chrrem == 0);
102 TESTCASE(c32ptr == &c32out[5]);
103 TESTCASE(chrptr == &chrout[5]);
104 TESTCASE(memcmp(chrout, abcde, 5) == 0);
106 // Invalid conversions
107 char32_t baduni = 0xFFFE;
112 TESTCASE(c32tolatin1(&chrptr, &chrrem, &c32ptr, &c32rem, &mbs) == false);
113 TESTCASE(c32ptr == &baduni);
114 TESTCASE(c32rem == 1);
115 TESTCASE(chrptr == &chrout[0]);
116 TESTCASE(chrrem == 5);