-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathamalgamation_demo.c
More file actions
59 lines (52 loc) · 1.54 KB
/
amalgamation_demo.c
File metadata and controls
59 lines (52 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "simdutf_c.h"
int main(void) {
printf("SIMDUTF C API demo\n");
const char *source = "1234";
/* validate UTF-8 */
if (!simdutf_validate_utf8(source, 4)) {
puts("invalid UTF-8");
return EXIT_FAILURE;
}
puts("valid UTF-8");
/* Convert UTF-8 -> UTF-16LE */
size_t expected_utf16 = simdutf_utf16_length_from_utf8(source, 4);
char16_t *utf16_output =
(char16_t *)malloc(expected_utf16 * sizeof(char16_t));
if (!utf16_output)
return EXIT_FAILURE;
size_t utf16words = simdutf_convert_utf8_to_utf16le(source, 4, utf16_output);
printf("wrote %zu UTF-16LE words.\n", utf16words);
if (!simdutf_validate_utf16le(utf16_output, utf16words)) {
puts("invalid UTF-16LE");
free(utf16_output);
return EXIT_FAILURE;
}
puts("valid UTF-16LE");
/* Convert back UTF-16LE -> UTF-8 */
size_t expected_utf8 =
simdutf_utf8_length_from_utf16le(utf16_output, utf16words);
char *utf8_output = (char *)malloc(expected_utf8 + 1);
if (!utf8_output) {
free(utf16_output);
return EXIT_FAILURE;
}
size_t utf8words =
simdutf_convert_utf16le_to_utf8(utf16_output, utf16words, utf8_output);
utf8_output[utf8words] = '\0';
printf("wrote %zu UTF-8 words.\n", utf8words);
puts(utf8_output);
if (strcmp(utf8_output, source) != 0) {
puts("bad conversion");
free(utf16_output);
free(utf8_output);
return EXIT_FAILURE;
} else {
puts("perfect round trip");
}
free(utf16_output);
free(utf8_output);
return EXIT_SUCCESS;
}