3 This file is part of the Public Domain C Library (PDCLib).
4 Permission is granted to use, modify, and / or redistribute at will.
11 static bool asciitoc32(
12 char32_t **restrict p_outbuf,
13 size_t *restrict p_outsz,
14 const char **restrict p_inbuf,
15 size_t *restrict p_insz,
16 mbstate_t *restrict p_ps
19 while(*p_outsz && *p_insz) {
20 unsigned char c = **p_inbuf;
36 static bool c32toascii(
37 char **restrict p_outbuf,
38 size_t *restrict p_outsz,
39 const char32_t **restrict p_inbuf,
40 size_t *restrict p_insz,
41 mbstate_t *restrict p_ps
44 while(*p_outsz && *p_insz) {
45 char32_t c = **p_inbuf;
61 _PDCLIB_charcodec _PDCLIB_ascii_codec = {
62 .__mbstoc32s = asciitoc32,
63 .__c32stombs = c32toascii,
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(asciitoc32(&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(c32toascii(&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 char badascii = '\xC0';
112 TESTCASE(asciitoc32(&c32ptr, &c32rem, &chrptr, &chrrem, &mbs) == false);
113 TESTCASE(c32ptr == &c32out[0]);
114 TESTCASE(c32rem == 5);
115 TESTCASE(chrptr == &badascii);
116 TESTCASE(chrrem == 1);
118 char32_t baduni = 0xC0;
123 TESTCASE(c32toascii(&chrptr, &chrrem, &c32ptr, &c32rem, &mbs) == false);
124 TESTCASE(c32ptr == &baduni);
125 TESTCASE(c32rem == 1);
126 TESTCASE(chrptr == &chrout[0]);
127 TESTCASE(chrrem == 5);