summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/unicode.h2
-rw-r--r--src/target/test.c58
-rw-r--r--src/text.c2
-rw-r--r--src/unicode.c4
4 files changed, 39 insertions, 27 deletions
diff --git a/include/unicode.h b/include/unicode.h
index f950f12..0a34ce3 100644
--- a/include/unicode.h
+++ b/include/unicode.h
@@ -1,2 +1,2 @@
-unsigned long utf8_strlen(void *string);
+unsigned long utf8_strlen(char *string);
void utf8to32_strcpy(wchar_t *dest, char *src);
diff --git a/src/target/test.c b/src/target/test.c
index 60439a5..0959eaa 100644
--- a/src/target/test.c
+++ b/src/target/test.c
@@ -7,51 +7,63 @@ struct u_pair {
wchar_t *utf32;
};
-struct u_pair *text_fixture;
+struct u_pair text_fixture[] = {
+ {
+ .utf8 = "Single byte ascii symbols string",
+ .utf32 = L"Single byte ascii symbols string"
+ },
+ {
+ .utf8 = "\u041A\u0438\u0440\u0438\u043B\u043B\u0438\u0446\u0430",
+ .utf32 = L"\u041A\u0438\u0440\u0438\u043B\u043B\u0438\u0446\u0430"
+ },
+ {
+ .utf8 = "\u0814\u0820\u080C\u081D\u0813\u0829\u0809\u080C",
+ .utf32 = L"\u0814\u0820\u080C\u081D\u0813\u0829\u0809\u080C"
+ },
+ {
+ .utf8 = "𓁹",
+ .utf32 = L"𓁹"
+ }
+};
+
-static void *test_text_setup(const MunitParameter params[], void *user_data) {
+static void *test_utf8_strlen_setup(const MunitParameter params[], void *user_data) {
(void) params;
(void) user_data;
return text_fixture;
}
-static MunitResult test_text(const MunitParameter params[], void *fixture) {
+static MunitResult test_utf8_strlen(const MunitParameter params[], void *fixture) {
(void) params;
struct u_pair *f = fixture;
munit_assert_ulong(utf8_strlen(f[0].utf8), ==, strlen(f[0].utf8));
+ munit_assert_ulong(utf8_strlen(f[1].utf8) * 2, ==, strlen(f[1].utf8));
+ munit_assert_ulong(utf8_strlen(f[2].utf8) * 3, ==, strlen(f[2].utf8));
+ munit_assert_ulong(utf8_strlen(f[3].utf8) * 4, ==, strlen(f[3].utf8));
return MUNIT_OK;
}
-static MunitTest test_suite_tests[] = {
- {
- "/example/parameters",
- test_text,
- test_text_setup,
- NULL,
- MUNIT_TEST_OPTION_NONE,
- NULL
- },
- {NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL}
-};
-
static const MunitSuite test_suite = {
"unit",
- test_suite_tests,
+ (MunitTest[]){
+ {
+ "/unicode/utf8_strlen",
+ test_utf8_strlen,
+ test_utf8_strlen_setup,
+ NULL,
+ MUNIT_TEST_OPTION_NONE,
+ NULL
+ },
+ {NULL, NULL, NULL, NULL, MUNIT_TEST_OPTION_NONE, NULL}
+ },
NULL,
1,
MUNIT_SUITE_OPTION_NONE
};
int main(int argc, char **argv) {
- text_fixture = (struct u_pair []){
- {
- .utf8 = "Single byte ascii symbols string",
- .utf32 = L"Single byte ascii symbols string"
- }
- };
-
return munit_suite_main(&test_suite, NULL, argc, argv);
}
diff --git a/src/text.c b/src/text.c
index 8c110d7..f66ad2d 100644
--- a/src/text.c
+++ b/src/text.c
@@ -62,7 +62,7 @@ struct idlist *load_texts(char *filename)
text->scene = malloc(sizeof(char) * (strlen((char *)ev.data.scalar.value) + 1));
strcpy(text->scene, (char *)ev.data.scalar.value);
} else if (!strcmp((char *)event.data.scalar.value, "text")) {
- text->text = calloc((utf8_strlen(ev.data.scalar.value)) + 1, sizeof(unsigned int));
+ text->text = calloc((utf8_strlen((char *)ev.data.scalar.value)) + 1, sizeof(unsigned int));
utf8to32_strcpy(text->text, (char *)ev.data.scalar.value);
} else if (!strcmp((char *)event.data.scalar.value, "font")) {
text->font = malloc(sizeof(char) * (strlen((char *)ev.data.scalar.value) + 1));
diff --git a/src/unicode.c b/src/unicode.c
index dea541a..8bde67d 100644
--- a/src/unicode.c
+++ b/src/unicode.c
@@ -17,10 +17,10 @@ static inline unsigned int utf8_char_len(unsigned char c)
return 0;
}
-unsigned long utf8_strlen(void *string)
+unsigned long utf8_strlen(char *string)
{
unsigned long len = 0, keep = 0;
- for (unsigned char *c = string; *c; (keep ? --keep : ++len), ++c)
+ for (char *c = string; *c; (keep ? --keep : ++len), ++c)
if (!keep)
keep = (keep = utf8_char_len(*c)) ? keep - 1 : keep;
return len;