diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/engine.c | 1 | ||||
-rw-r--r-- | src/target/test.c | 57 | ||||
-rw-r--r-- | src/target/tetris.c (renamed from src/main.c) | 8 | ||||
-rw-r--r-- | src/text.c | 53 | ||||
-rw-r--r-- | src/unicode.c | 52 |
5 files changed, 115 insertions, 56 deletions
diff --git a/src/engine.c b/src/engine.c index d9cfe98..482c6df 100644 --- a/src/engine.c +++ b/src/engine.c @@ -11,6 +11,7 @@ #include "vector.h" #include "text.h" #include "field.h" +#include "unicode.h" #include "painter.h" #include "engine.h" diff --git a/src/target/test.c b/src/target/test.c new file mode 100644 index 0000000..60439a5 --- /dev/null +++ b/src/target/test.c @@ -0,0 +1,57 @@ +#include "munit.h" + +#include "unicode.h" + +struct u_pair { + char *utf8; + wchar_t *utf32; +}; + +struct u_pair *text_fixture; + +static void *test_text_setup(const MunitParameter params[], void *user_data) { + (void) params; + (void) user_data; + + return text_fixture; +} + +static MunitResult test_text(const MunitParameter params[], void *fixture) { + (void) params; + struct u_pair *f = fixture; + + munit_assert_ulong(utf8_strlen(f[0].utf8), ==, strlen(f[0].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, + 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/main.c b/src/target/tetris.c index bf9014a..2419c8d 100644 --- a/src/main.c +++ b/src/target/tetris.c @@ -1,11 +1,11 @@ -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <time.h> #include <SFML/System/Clock.h> #include <SFML/Window/Keyboard.h> #include <SFML/Graphics/RenderWindow.h> #include <SFML/Graphics/Font.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <time.h> #include "common.h" #include "idlist.h" @@ -1,63 +1,12 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> -#include <SFML/Graphics/Font.h> -#include <SFML/Graphics/Text.h> -#include <SFML/Graphics.h> #include <yaml.h> -#include "common.h" #include "idlist.h" #include "vector.h" #include "text.h" - -static inline unsigned int utf8_char_len(unsigned char c) -{ - if (c > 0x00 && c < 0xC0) - return 1; - else if (c >= 0xC2 && c < 0xE0) - return 2; - else if (c >= 0xE0 && c < 0xF0) - return 3; - else if (c >= 0xF0 && c < 0xF5) - return 4; - else - return 0; -} - -unsigned long utf8_strlen(void *string) -{ - unsigned long len = 0, keep = 0; - for (unsigned char *c = string; *c; (keep ? --keep : ++len), ++c) - if (!keep) - keep = (keep = utf8_char_len(*c)) ? keep - 1 : keep; - return len; -} - -void utf8to32_strcpy(wchar_t *dest, char *src) -{ - wchar_t *dc = dest; - char *c = src; - unsigned long len = 0; - while (*c) { - int clen = utf8_char_len(*c); - if (clen == 1) { - dc[len] = c[0] & 0x7f; - } else if (clen == 2) { - dc[len] = ((c[0] & 0x1f) << 6) | ((c[1] & 0x3f) << 0); - } else if (clen == 3) { - dc[len] = ((c[0] & 0x0f) << 12) | ((c[1] & 0x3f) << 6) | ((c[2] & 0x3f) << 0); - } else if (clen == 4) { - dc[len] = ((c[0] & 0x07) << 18) | ((c[1] & 0x3f) << 12) | ((c[2] & 0x3f) << 6) | ((c[3] & 0x3f) << 0); - } else { - dc[len] = 0; - return; - } - c += clen; - ++len; - } - dc[len] = 0; -} +#include "unicode.h" static FILE *openFile(char *filename) { diff --git a/src/unicode.c b/src/unicode.c new file mode 100644 index 0000000..dea541a --- /dev/null +++ b/src/unicode.c @@ -0,0 +1,52 @@ +#include <stdlib.h> +#include <string.h> + +#include "unicode.h" + +static inline unsigned int utf8_char_len(unsigned char c) +{ + if (c > 0x00 && c < 0xC0) + return 1; + else if (c >= 0xC2 && c < 0xE0) + return 2; + else if (c >= 0xE0 && c < 0xF0) + return 3; + else if (c >= 0xF0 && c < 0xF5) + return 4; + else + return 0; +} + +unsigned long utf8_strlen(void *string) +{ + unsigned long len = 0, keep = 0; + for (unsigned char *c = string; *c; (keep ? --keep : ++len), ++c) + if (!keep) + keep = (keep = utf8_char_len(*c)) ? keep - 1 : keep; + return len; +} + +void utf8to32_strcpy(wchar_t *dest, char *src) +{ + wchar_t *dc = dest; + char *c = src; + unsigned long len = 0; + while (*c) { + int clen = utf8_char_len(*c); + if (clen == 1) { + dc[len] = c[0] & 0x7f; + } else if (clen == 2) { + dc[len] = ((c[0] & 0x1f) << 6) | ((c[1] & 0x3f) << 0); + } else if (clen == 3) { + dc[len] = ((c[0] & 0x0f) << 12) | ((c[1] & 0x3f) << 6) | ((c[2] & 0x3f) << 0); + } else if (clen == 4) { + dc[len] = ((c[0] & 0x07) << 18) | ((c[1] & 0x3f) << 12) | ((c[2] & 0x3f) << 6) | ((c[3] & 0x3f) << 0); + } else { + dc[len] = 0; + return; + } + c += clen; + ++len; + } + dc[len] = 0; +} |