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 asciitoc32(
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;
37 static bool c32toascii(
38 char *restrict *restrict p_outbuf,
39 size_t *restrict p_outsz,
40 const char32_t *restrict *restrict p_inbuf,
41 size_t *restrict p_insz,
42 mbstate_t *restrict p_ps
45 while(*p_outsz && *p_insz) {
46 char32_t c = **p_inbuf;
62 struct _PDCLIB_charcodec _PDCLIB_ascii_codec = {
63 .__mbstoc32s = asciitoc32,
64 .__c32stombs = c32toascii,
71 #include <_PDCLIB_test.h>
76 // Valid conversion & back
80 char32_t *c32ptr = &c32out[0];
82 char *chrptr = (char*) &abcde[0];
84 mbstate_t mbs = { 0 };
86 TESTCASE(asciitoc32(&c32ptr, &c32rem, &chrptr, &chrrem, &mbs) == true);
87 TESTCASE(c32rem == 0);
88 TESTCASE(chrrem == 0);
89 TESTCASE(c32ptr == &c32out[5]);
90 TESTCASE(chrptr == &abcde[5]);
91 TESTCASE(c32out[0] == 'a' && c32out[1] == 'b' && c32out[2] == 'c' && \
92 c32out[3] == 'd' && c32out[4] == 'e');
101 TESTCASE(c32toascii(&chrptr, &chrrem, &c32ptr, &c32rem, &mbs) == true);
102 TESTCASE(c32rem == 0);
103 TESTCASE(chrrem == 0);
104 TESTCASE(c32ptr == &c32out[5]);
105 TESTCASE(chrptr == &chrout[5]);
106 TESTCASE(memcmp(chrout, abcde, 5) == 0);
108 // Invalid conversions
109 char badascii = '\xC0';
114 TESTCASE(asciitoc32(&c32ptr, &c32rem, &chrptr, &chrrem, &mbs) == false);
115 TESTCASE(c32ptr == &c32out[0]);
116 TESTCASE(c32rem == 5);
117 TESTCASE(chrptr == &badascii);
118 TESTCASE(chrrem == 1);
120 char32_t baduni = 0xC0;
125 TESTCASE(c32toascii(&chrptr, &chrrem, &c32ptr, &c32rem, &mbs) == false);
126 TESTCASE(c32ptr == &baduni);
127 TESTCASE(c32rem == 1);
128 TESTCASE(chrptr == &chrout[0]);
129 TESTCASE(chrrem == 5);