1
0
mirror of https://git.code.sf.net/p/zint/code synced 2025-12-18 10:27:09 +00:00

ECI: get_best_eci(): just assert is_valid_utf8() as checked

prior to being called
manual: update to latest GS1 General Specifications (v24), pandoc
  (3.1.12.1)
This commit is contained in:
gitlost
2024-02-28 02:57:43 +00:00
parent f0d8901d9e
commit 11b3c18aed
8 changed files with 36 additions and 41 deletions

View File

@@ -1,7 +1,7 @@
/* eci.c - Extended Channel Interpretations */
/*
libzint - the open source barcode library
Copyright (C) 2009-2023 Robin Stuart <rstuart114@gmail.com>
Copyright (C) 2009-2024 Robin Stuart <rstuart114@gmail.com>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
@@ -764,7 +764,7 @@ INTERNAL int utf8_to_eci(const int eci, const unsigned char source[], unsigned c
return 0;
}
/* Find the lowest single-byte ECI mode which will encode a given set of Unicode text */
/* Find the lowest single-byte ECI mode which will encode a given set of Unicode text, assuming valid UTF-8 */
INTERNAL int get_best_eci(const unsigned char source[], int length) {
int eci = 3;
/* Note: attempting single-byte conversions only, so get_eci_length() unnecessary */
@@ -782,14 +782,12 @@ INTERNAL int get_best_eci(const unsigned char source[], int length) {
eci++;
} while (eci < 25);
if (!is_valid_utf8(source, length)) {
return 0;
}
assert(is_valid_utf8(source, length));
return 26; /* If all of these fail, use UTF-8! */
}
/* Call `get_best_eci()` for each segment. Returns 0 on failure, first ECI set on success */
/* Call `get_best_eci()` for each segment, assuming valid UTF-8. Returns 0 on failure, first ECI set on success */
INTERNAL int get_best_eci_segs(struct zint_symbol *symbol, struct zint_seg segs[], const int seg_count) {
const int default_eci = symbol->symbology == BARCODE_GRIDMATRIX ? 29 : symbol->symbology == BARCODE_UPNQR ? 4 : 3;
int first_eci_set = 0;

View File

@@ -1,6 +1,6 @@
/*
libzint - the open source barcode library
Copyright (C) 2019-2022 Robin Stuart <rstuart114@gmail.com>
Copyright (C) 2019-2024 Robin Stuart <rstuart114@gmail.com>
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
@@ -1381,14 +1381,12 @@ static void test_get_best_eci(const testCtx *const p_ctx) {
};
/* s/\/\*[ 0-9]*\*\//\=printf("\/\*%3d*\/", line(".") - line("'<")): */
struct item data[] = {
/* 0*/ { "\300\301", -1, 0 },
/* 1*/ { "ÀÁ", -1, 3 },
/* 2*/ { "Ђ", -1, 7 },
/* 3*/ { "Ѐ", -1, 26 }, /* Cyrillic U+0400 not in single-byte code pages */
/* 4*/ { "β", -1, 9 },
/* 5*/ { "˜", -1, 23 },
/* 6*/ { "βЂ", -1, 26 },
/* 7*/ { "AB\200", -1, 0 },
/* 0*/ { "ÀÁ", -1, 3 },
/* 1*/ { "Ђ", -1, 7 },
/* 2*/ { "Ѐ", -1, 26 }, /* Cyrillic U+0400 not in single-byte code pages */
/* 3*/ { "β", -1, 9 },
/* 4*/ { "˜", -1, 23 },
/* 5*/ { "βЂ", -1, 26 },
};
int data_size = ARRAY_SIZE(data);
int i, length, ret;
@@ -1417,16 +1415,15 @@ static void test_get_best_eci_segs(const testCtx *const p_ctx) {
};
/* s/\/\*[ 0-9]*\*\//\=printf("\/\*%3d*\/", line(".") - line("'<")): */
struct item data[] = {
/* 0*/ { { { TU("\300\301"), -1, 0 }, { TU(""), -1, 0 }, { TU(""), 0, 0 } }, 0, 0 },
/* 1*/ { { { TU("A"), -1, 0 }, { TU("\300\301"), -1, 0 }, { TU(""), 0, 0 } }, 0, 0 },
/* 2*/ { { { TU("A"), -1, 0 }, { TU("ÀÁ"), -1, 0 }, { TU(""), 0, 0 } }, 0, 0 }, /* As 1st seg default ECI, 3 not returned */
/* 3*/ { { { TU("A"), -1, 4 }, { TU("ÀÁ"), -1, 0 }, { TU(""), 0, 0 } }, 3, 0 },
/* 4*/ { { { TU("A"), -1, 0 }, { TU("Ђ"), -1, 0 }, { TU(""), 0, 0 } }, 7, 0 },
/* 5*/ { { { TU("A"), -1, 4 }, { TU("Ђ"), -1, 0 }, { TU(""), 0, 0 } }, 7, 0 },
/* 6*/ { { { TU("A"), -1, 0 }, { TU("Ђ"), -1, 7 }, { TU("Ѐ"), -1, 0 } }, 26, 0 }, /* Cyrillic U+0400 not in single-byte code pages */
/* 7*/ { { { TU("A"), -1, 0 }, { TU("Ђ"), -1, 0 }, { TU("β"), -1, 0 } }, 7, 0 },
/* 8*/ { { { TU("A"), -1, 0 }, { TU("Ђ"), -1, 7 }, { TU("β"), -1, 0 } }, 9, 0 },
/* 9*/ { { { TU("˜"), -1, 0 }, { TU("Ђ"), -1, 7 }, { TU(""), 0, 0 } }, 23, 23 },
/* 0*/ { { { TU("A"), -1, 0 }, { TU(""), -1, 0 }, { TU(""), 0, 0 } }, 0, 0 },
/* 1*/ { { { TU("A"), -1, 0 }, { TU("ÀÁ"), -1, 0 }, { TU(""), 0, 0 } }, 0, 0 }, /* As 1st seg default ECI, 3 not returned */
/* 2*/ { { { TU("A"), -1, 4 }, { TU("ÀÁ"), -1, 0 }, { TU(""), 0, 0 } }, 3, 0 },
/* 3*/ { { { TU("A"), -1, 0 }, { TU("Ђ"), -1, 0 }, { TU(""), 0, 0 } }, 7, 0 },
/* 4*/ { { { TU("A"), -1, 4 }, { TU("Ђ"), -1, 0 }, { TU(""), 0, 0 } }, 7, 0 },
/* 5*/ { { { TU("A"), -1, 0 }, { TU("Ђ"), -1, 7 }, { TU("Ѐ"), -1, 0 } }, 26, 0 }, /* Cyrillic U+0400 not in single-byte code pages */
/* 6*/ { { { TU("A"), -1, 0 }, { TU("Ђ"), -1, 0 }, { TU("β"), -1, 0 } }, 7, 0 },
/* 7*/ { { { TU("A"), -1, 0 }, { TU("Ђ"), -1, 7 }, { TU("β"), -1, 0 } }, 9, 0 },
/* 8*/ { { { TU("˜"), -1, 0 }, { TU("Ђ"), -1, 7 }, { TU(""), 0, 0 } }, 23, 23 },
};
int data_size = ARRAY_SIZE(data);
int i, j, seg_count, ret;