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 ascii_mbsinit( const mbstate_t *ps )
15 static bool asciitoc32(
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;
40 static bool c32toascii(
41 char *restrict *restrict p_outbuf,
42 size_t *restrict p_outsz,
43 const char32_t *restrict *restrict p_inbuf,
44 size_t *restrict p_insz,
45 mbstate_t *restrict p_ps
48 while(*p_outsz && *p_insz) {
49 char32_t c = **p_inbuf;
65 const struct _PDCLIB_charcodec_t _PDCLIB_ascii_codec = {
66 .__mbsinit = ascii_mbsinit,
67 .__mbstoc32s = asciitoc32,
68 .__c32stombs = c32toascii,
75 #include "_PDCLIB_test.h"
80 // Valid conversion & back
84 char32_t *c32ptr = &c32out[0];
86 char *chrptr = (char*) &abcde[0];
88 mbstate_t mbs = { 0 };
90 TESTCASE(asciitoc32(&c32ptr, &c32rem, &chrptr, &chrrem, &mbs) == true);
91 TESTCASE(c32rem == 0);
92 TESTCASE(chrrem == 0);
93 TESTCASE(c32ptr == &c32out[5]);
94 TESTCASE(chrptr == &abcde[5]);
95 TESTCASE(c32out[0] == 'a' && c32out[1] == 'b' && c32out[2] == 'c' && \
96 c32out[3] == 'd' && c32out[4] == 'e');
105 TESTCASE(c32toascii(&chrptr, &chrrem, &c32ptr, &c32rem, &mbs) == true);
106 TESTCASE(c32rem == 0);
107 TESTCASE(chrrem == 0);
108 TESTCASE(c32ptr == &c32out[5]);
109 TESTCASE(chrptr == &chrout[5]);
110 TESTCASE(memcmp(chrout, abcde, 5) == 0);
112 // Invalid conversions
113 char badascii = '\xC0';
118 TESTCASE(asciitoc32(&c32ptr, &c32rem, &chrptr, &chrrem, &mbs) == false);
119 TESTCASE(c32ptr == &c32out[0]);
120 TESTCASE(c32rem == 5);
121 TESTCASE(chrptr == &badascii);
122 TESTCASE(chrrem == 1);
124 char32_t baduni = 0xC0;
129 TESTCASE(c32toascii(&chrptr, &chrrem, &c32ptr, &c32rem, &mbs) == false);
130 TESTCASE(c32ptr == &baduni);
131 TESTCASE(c32rem == 1);
132 TESTCASE(chrptr == &chrout[0]);
133 TESTCASE(chrrem == 5);