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 latin1_mbsinit( const mbstate_t *ps )
15 static bool latin1toc32(
16 char32_t *restrict *restrict p_outbuf,
17 size_t *restrict p_outsz,
18 const char *restrict *restrict p_inbuf,
19 size_t *restrict p_insz,
20 mbstate_t *restrict p_ps
23 while(*p_outsz && *p_insz) {
24 unsigned char c = **p_inbuf;
38 static bool c32tolatin1(
39 char *restrict *restrict p_outbuf,
40 size_t *restrict p_outsz,
41 const char32_t *restrict *restrict p_inbuf,
42 size_t *restrict p_insz,
43 mbstate_t *restrict p_ps
46 while(*p_outsz && *p_insz) {
47 char32_t c = **p_inbuf;
63 const struct _PDCLIB_charcodec_t _PDCLIB_latin1_codec = {
64 .__mbsinit = latin1_mbsinit,
65 .__mbstoc32s = latin1toc32,
66 .__c32stombs = c32tolatin1,
73 #include "_PDCLIB_test.h"
78 // Valid conversion & back
82 char32_t *c32ptr = &c32out[0];
84 char *chrptr = (char*) &abcde[0];
86 mbstate_t mbs = { 0 };
88 TESTCASE(latin1toc32(&c32ptr, &c32rem, &chrptr, &chrrem, &mbs) == true);
89 TESTCASE(c32rem == 0);
90 TESTCASE(chrrem == 0);
91 TESTCASE(c32ptr == &c32out[5]);
92 TESTCASE(chrptr == &abcde[5]);
93 TESTCASE(c32out[0] == 'a' && c32out[1] == 'b' && c32out[2] == 'c' && \
94 c32out[3] == 'd' && c32out[4] == 'e');
103 TESTCASE(c32tolatin1(&chrptr, &chrrem, &c32ptr, &c32rem, &mbs) == true);
104 TESTCASE(c32rem == 0);
105 TESTCASE(chrrem == 0);
106 TESTCASE(c32ptr == &c32out[5]);
107 TESTCASE(chrptr == &chrout[5]);
108 TESTCASE(memcmp(chrout, abcde, 5) == 0);
110 // Invalid conversions
111 char32_t baduni = 0xFFFE;
116 TESTCASE(c32tolatin1(&chrptr, &chrrem, &c32ptr, &c32rem, &mbs) == false);
117 TESTCASE(c32ptr == &baduni);
118 TESTCASE(c32rem == 1);
119 TESTCASE(chrptr == &chrout[0]);
120 TESTCASE(chrrem == 5);