get_best_eci: check UTF-8 before returning 26; move is_valid_utf8 to common

This commit is contained in:
gitlost 2021-03-21 17:35:52 +00:00
parent 7f74414666
commit 2d962c6321
7 changed files with 104 additions and 64 deletions

View file

@ -589,47 +589,6 @@ static void test_strip_bom(void) {
testFinish();
}
STATIC_UNLESS_ZINT_TEST int is_valid_utf8(const unsigned char source[], const int length);
static void test_is_valid_utf8(int index) {
testStart("");
int ret;
struct item {
char* data;
int length;
int ret;
char* comment;
};
// s/\/\*[ 0-9]*\*\//\=printf("\/*%3d*\/", line(".") - line("'<"))
struct item data[] = {
/* 0*/ { "", -1, 1, "" },
/* 1*/ { "abcdefghijklmnopqrstuvwxyz", -1, 1, "" },
/* 2*/ { "éa", -1, 1, "" },
/* 3*/ { "a\000b", 3, 1, "Embedded nul" },
/* 4*/ { "\357\273\277a", -1, 1, "Bom" },
/* 5*/ { "a\xC2", -1, 0, "Missing 2nd byte" },
/* 6*/ { "a\200b", -1, 0, "Orphan continuation 0x80" },
/* 7*/ { "\300\201", -1, 0, "Overlong 0xC081" },
/* 8*/ { "\355\240\200", -1, 0, "Surrogate 0xEDA080" },
};
int data_size = ARRAY_SIZE(data);
for (int i = 0; i < data_size; i++) {
if (index != -1 && i != index) continue;
int length = data[i].length == -1 ? (int) strlen(data[i].data) : data[i].length;
ret = is_valid_utf8((const unsigned char *) data[i].data, length);
assert_equal(ret, data[i].ret, "i:%d ret %d != %d\n", i, ret, data[i].ret);
}
testFinish();
}
int main(int argc, char *argv[]) {
testFunction funcs[] = { /* name, func, has_index, has_generate, has_debug */
@ -643,7 +602,6 @@ int main(int argc, char *argv[]) {
{ "test_valid_id", test_valid_id, 0, 0, 0 },
{ "test_error_tag", test_error_tag, 1, 0, 0 },
{ "test_strip_bom", test_strip_bom, 0, 0, 0 },
{ "test_is_valid_utf8", test_is_valid_utf8, 1, 0, 0 },
};
testRun(argc, argv, funcs, ARRAY_SIZE(funcs));