diff options
| author | Edward Thomson <ethomson@edwardthomson.com> | 2021-04-14 22:22:11 +0100 |
|---|---|---|
| committer | Edward Thomson <ethomson@edwardthomson.com> | 2021-04-14 23:02:51 +0100 |
| commit | cb136cddd93046d46c6a8c5ee2a3f5c30a001e97 (patch) | |
| tree | f5d88259016e7b5b95fda5d190ec416c25b56c74 /tests/core | |
| parent | 1d95b59b4dbd8eda3f83f8af2a4ae07c7cdfc245 (diff) | |
| download | libgit2-cb136cddd93046d46c6a8c5ee2a3f5c30a001e97.tar.gz | |
utf8: introduce git_utf8_char_length
Introduce a function to determine the number of Unicode characters in a
given UTF-8 string.
Diffstat (limited to 'tests/core')
| -rw-r--r-- | tests/core/utf8.c | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/tests/core/utf8.c b/tests/core/utf8.c new file mode 100644 index 000000000..021828e9e --- /dev/null +++ b/tests/core/utf8.c @@ -0,0 +1,19 @@ +#include "clar_libgit2.h" + +void test_core_utf8__char_length(void) +{ + cl_assert_equal_i(0, git_utf8_char_length("", 0)); + cl_assert_equal_i(1, git_utf8_char_length("$", 1)); + cl_assert_equal_i(5, git_utf8_char_length("abcde", 5)); + cl_assert_equal_i(1, git_utf8_char_length("\xc2\xa2", 2)); + cl_assert_equal_i(2, git_utf8_char_length("\x24\xc2\xa2", 3)); + cl_assert_equal_i(1, git_utf8_char_length("\xf0\x90\x8d\x88", 4)); + + /* uncontinued character counted as single characters */ + cl_assert_equal_i(2, git_utf8_char_length("\x24\xc2", 2)); + cl_assert_equal_i(3, git_utf8_char_length("\x24\xc2\xc2\xa2", 4)); + + /* invalid characters are counted as single characters */ + cl_assert_equal_i(4, git_utf8_char_length("\x24\xc0\xc0\x34", 4)); + cl_assert_equal_i(4, git_utf8_char_length("\x24\xf5\xfd\xc2", 4)); +} |
