diff options
| author | David Faulkner <[email protected]> | 2026-08-07 23:40:47 -0500 |
|---|---|---|
| committer | David Faulkner <[email protected]> | 2026-08-07 23:40:47 -0500 |
| commit | b3e9e62599532050fc776c5e8f076915b56c2235 (patch) | |
| tree | af252346106a61b18cc6fc6fdbd32e962d096f1c /test-c8.c | |
Import official C23 code examples for Modern C (Jens Gustedt, 2024)HEADupstream-importmain
- Add official C source files, Makefile, c23-fallback.h, and LICENSE
- Update README.md with study mirror notice
Diffstat (limited to 'test-c8.c')
| -rw-r--r-- | test-c8.c | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/test-c8.c b/test-c8.c new file mode 100644 index 0000000..204841b --- /dev/null +++ b/test-c8.c @@ -0,0 +1,54 @@ +#include <uchar.h> +#include <stdio.h> +#include <string.h> +#include <locale.h> + +void print_string(char const*s) { + char const*str = s; + size_t n = strlen(str)+1; + char8_t buffer[n+1]; + printf("mb→utf8, source: '%s', sliced: ", str); + mbstate_t mb = { 0 }; + for (char8_t *p = buffer; ; p++) { + size_t res = mbrtoc8(p, str, n, &mb); + if (res > -3) { + printf("!error:%zu!\n", -res); + return; + } else if (res == -3) { + printf(",%hhx", *p); + } else if (res) { + printf("|%hhx", *p); + str += res; + } else { + printf("(%hhx)", *p); + str++; + break; + } + } + printf("; target: %s\n", buffer); + memset(buffer, 0, sizeof buffer); + char *p = (char*)buffer; + printf("utf8→mb, source: '%s', ", s); + for (char const *q = s; ; q++) { + size_t res = c8rtomb(p, *q, &mb); + if (res) { + switch (res) { + default: printf("∞"); res = -1; break; + case 4: printf("<-->"); break; + case 3: printf("<->"); break; + case 2: printf("<>"); break; + case 1: printf("x"); break; + } + if (!*p || (res == -1)) break; + else p += res; + } + } + printf("; target: %s\n", buffer); +} + +int main(int argc, char* argv[static argc+1]) { + if (argc > 1) + setlocale(LC_ALL, ""); + print_string(u8"abcdäα⌫"); + print_string(u8"→😇"); +} |
