cleaning comments

This commit is contained in:
2026-05-23 07:24:05 -03:00
parent f615ee0242
commit 8c14340adc
5 changed files with 26 additions and 26 deletions

View File

@@ -1,6 +1,6 @@
/* ************************************************************************ * /* ************************************************************************ *
* cmp11sht.c, v20251221.085434 * * cmp11sht.c, v20251221.085434 *
* Fuzzy comparison CLI thin shell over lib11sht * * Fuzzy comparison CLI -- thin shell over lib11sht *
* * * *
* Copyright (C) 2025 by Ruben Carlo Benante <rcb@beco.cc> * * Copyright (C) 2025 by Ruben Carlo Benante <rcb@beco.cc> *
* GNU GPL version 2 or later. * * GNU GPL version 2 or later. *
@@ -92,11 +92,11 @@ cmp11sht v20251221.0718 (C) 2025 by Ruben C. Benante (MIT Lic)\n\n"
res = sequal_full(argv[1], argv[2], delta, &ratio, res = sequal_full(argv[1], argv[2], delta, &ratio,
s1, LEVN_SBUFF, s2, LEVN_SBUFF); s1, LEVN_SBUFF, s2, LEVN_SBUFF);
if(errno == EINVAL) if(errno == EINVAL)
res = 3; /* error CLI exit 3 */ res = 3; /* error -> CLI exit 3 */
else if(res > 0) else if(res > 0)
res = 1; /* a > b CLI exit 1 */ res = 1; /* a > b -> CLI exit 1 */
else if(res < 0) else if(res < 0)
res = 2; /* a < b CLI exit 2 */ res = 2; /* a < b -> CLI exit 2 */
/* res == 0 stays 0 (equal) */ /* res == 0 stays 0 (equal) */
if(opt==3) printf("result: "); if(opt==3) printf("result: ");
if(opt) printf("%d\n", res); if(opt) printf("%d\n", res);
@@ -109,9 +109,9 @@ cmp11sht v20251221.0718 (C) 2025 by Ruben C. Benante (MIT Lic)\n\n"
if(opt==3) printf("cmp11sht: float\n"); if(opt==3) printf("cmp11sht: float\n");
res = fequal(a, b, delta); res = fequal(a, b, delta);
if(res > 0) if(res > 0)
res = 1; /* a > b CLI exit 1 */ res = 1; /* a > b -> CLI exit 1 */
else if(res < 0) else if(res < 0)
res = 2; /* a < b CLI exit 2 */ res = 2; /* a < b -> CLI exit 2 */
/* res == 0 stays 0 (equal) */ /* res == 0 stays 0 (equal) */
if(opt==3) printf("result: "); if(opt==3) printf("result: ");
if(opt) printf("%d\n", res); if(opt) printf("%d\n", res);

View File

@@ -14,7 +14,7 @@
#include "lib11sht.h" #include "lib11sht.h"
/* Internal helpers kept private to this translation unit. */ /* Internal helpers -- kept private to this translation unit. */
static int uselesschar(int c); static int uselesschar(int c);
/* trim is public (declared in lib11sht.h) */ /* trim is public (declared in lib11sht.h) */
static void asciify(const char *src, char *dest, size_t dest_size); static void asciify(const char *src, char *dest, size_t dest_size);
@@ -111,7 +111,7 @@ static float shit11(char *s1, char *s2)
return 0.0; /* one empty == no similarity */ return 0.0; /* one empty == no similarity */
/* Defensive cap: refuse to allocate too-large VLA. Bounds the stack /* Defensive cap: refuse to allocate too-large VLA. Bounds the stack
* matrix at LEVN_SBUFF × LEVN_SBUFF × sizeof(int) = ~256 KB worst case. */ * matrix at LEVN_SBUFF * LEVN_SBUFF * sizeof(int) = ~256 KB worst case. */
if(len1 >= LEVN_SBUFF || len2 >= LEVN_SBUFF) if(len1 >= LEVN_SBUFF || len2 >= LEVN_SBUFF)
return 0.0; /* treat as "completely dissimilar" */ return 0.0; /* treat as "completely dissimilar" */
@@ -218,7 +218,7 @@ static void asciify(const char *src, char *dest, size_t dest_size)
"AEIOUAEIOUAEIOUAEIOUAEIOU" "AEIOUAEIOUAEIOUAEIOUAEIOU"
"aeiouaeiouaeiouaeiouaeiou" "aeiouaeiouaeiouaeiouaeiou"
"aoCcNn123" "aoCcNn123"
" "; /* NBSP space */ " "; /* NBSP -> space */
const char *translit[] = { const char *translit[] = {
"Á","É","Í","Ó","Ú", "À","È","Ì","Ò","Ù", "Á","É","Í","Ó","Ú", "À","È","Ì","Ò","Ù",
"Ã","","Ĩ","Õ","Ũ", "Â","Ê","Î","Ô","Û", "Ã","","Ĩ","Õ","Ũ", "Â","Ê","Î","Ô","Û",

View File

@@ -42,7 +42,7 @@
* 0 if equal or similar above the shold threshold * 0 if equal or similar above the shold threshold
* -1 if a < b alphabetically (after normalization) * -1 if a < b alphabetically (after normalization)
* +1 if a > b alphabetically (after normalization) * +1 if a > b alphabetically (after normalization)
* On error: sets errno = EINVAL and returns 0 see ERROR CONVENTION above. * On error: sets errno = EINVAL and returns 0 -- see ERROR CONVENTION above.
* *
* Parameters: * Parameters:
* a, b input strings (NUL-terminated, may contain UTF-8 accented Latin chars). * a, b input strings (NUL-terminated, may contain UTF-8 accented Latin chars).
@@ -68,17 +68,17 @@ int sequal(char *a, char *b, float shold);
* comparison. The Levenshtein ratio in *ratio is computed on the * comparison. The Levenshtein ratio in *ratio is computed on the
* normalized contents of s1 / s2 (i.e. on the possibly-truncated buffer * normalized contents of s1 / s2 (i.e. on the possibly-truncated buffer
* data), NOT on the original a / b strings. To compare without truncation, * data), NOT on the original a / b strings. To compare without truncation,
* pass buffers at least as large as the longest input LEVN_SBUFF (256) * pass buffers at least as large as the longest input -- LEVN_SBUFF (256)
* is the recommended floor. * is the recommended floor.
* *
* On error: same convention as sequal sets errno = EINVAL and returns 0; * On error: same convention as sequal -- sets errno = EINVAL and returns 0;
* *ratio, s1, s2 are not modified in that case. See ERROR CONVENTION above. * *ratio, s1, s2 are not modified in that case. See ERROR CONVENTION above.
*/ */
int sequal_full(char *a, char *b, float shold, float *ratio, int sequal_full(char *a, char *b, float shold, float *ratio,
char *s1, size_t s1_size, char *s1, size_t s1_size,
char *s2, size_t s2_size); char *s2, size_t s2_size);
/* Compare two floats within ±delta. /* Compare two floats within +/-delta.
* Returns: * Returns:
* 0 if |a - b| <= delta * 0 if |a - b| <= delta
* -1 if a < b - delta * -1 if a < b - delta

View File

@@ -1,5 +1,5 @@
# Copyright (C) 2026 by Prof. Dr. Ruben Carlo Benante # Copyright (C) 2026 by Prof. Dr. Ruben Carlo Benante
# levenshtein library makefile # levenshtein library -- makefile
# #
# Usage: # Usage:
# make build both: cmp11sht (CLI) and test_lib11sht # make build both: cmp11sht (CLI) and test_lib11sht

View File

@@ -1,4 +1,4 @@
/* Stress tests for lib11sht aim to break upper / lower bounds. /* Stress tests for lib11sht -- aim to break upper / lower bounds.
* Compile: gcc -Wall -Wextra -O2 test_lib11sht.c lib11sht.c -lm -o test_lib11sht * Compile: gcc -Wall -Wextra -O2 test_lib11sht.c lib11sht.c -lm -o test_lib11sht
* Run: ./test_lib11sht * Run: ./test_lib11sht
* Exit 0 if all tests pass, non-zero on first failure. * Exit 0 if all tests pass, non-zero on first failure.
@@ -64,7 +64,7 @@ int main(void)
CHECK(errno == 0 && r == 0, "no errno, returns 0 (equal at boundary)"); CHECK(errno == 0 && r == 0, "no errno, returns 0 (equal at boundary)");
} }
/* Test 4: long input WITH accents UTF-8 multi-byte at offset 250. /* Test 4: long input WITH accents -- UTF-8 multi-byte at offset 250.
* Should asciify each accent to ASCII, total visible chars < 256. */ * Should asciify each accent to ASCII, total visible chars < 256. */
{ {
char a[400] = "", b[400] = ""; char a[400] = "", b[400] = "";
@@ -72,15 +72,15 @@ int main(void)
/* Fill with 240 'a' then put "ÃÃÃÃÃÃ" (UTF-8: 0xC3 0x83 each, 12 bytes) */ /* Fill with 240 'a' then put "ÃÃÃÃÃÃ" (UTF-8: 0xC3 0x83 each, 12 bytes) */
for(i = 0; i < 240; i++) { a[i] = 'a'; b[i] = 'a'; } for(i = 0; i < 240; i++) { a[i] = 'a'; b[i] = 'a'; }
a[240] = b[240] = '\0'; a[240] = b[240] = '\0';
strcat(a, "ÃÃÃÃÃÃ"); /* +12 bytes UTF-8, 6 chars visible asciify to 'AAAAAA' */ strcat(a, "ÃÃÃÃÃÃ"); /* +12 bytes UTF-8, 6 chars visible -> asciify to 'AAAAAA' */
strcat(b, "ÃÃÃÃÃÃ"); strcat(b, "ÃÃÃÃÃÃ");
printf("Test 4: 240 'a' + 6×à (multi-byte near boundary)\n"); printf("Test 4: 240 'a' + 6x A-tilde (multi-byte near boundary)\n");
errno = 0; errno = 0;
r = sequal(a, b, 0.85); r = sequal(a, b, 0.85);
CHECK(errno == 0 && r == 0, "no errno, returns 0 (equal)"); CHECK(errno == 0 && r == 0, "no errno, returns 0 (equal)");
} }
/* Test 5: full-detail call with undersized buffers this is the actual /* Test 5: full-detail call with undersized buffers -- this is the actual
* realistic break path. Pass 64-byte buffers to sequal_full. */ * realistic break path. Pass 64-byte buffers to sequal_full. */
{ {
char small1[64], small2[64]; char small1[64], small2[64];
@@ -99,7 +99,7 @@ int main(void)
/* ----- LOWER bound tests ----- */ /* ----- LOWER bound tests ----- */
/* Test 6: NULL inputs should set errno=EINVAL and return 0 */ /* Test 6: NULL inputs -- should set errno=EINVAL and return 0 */
{ {
printf("Test 6: NULL inputs via sequal_full\n"); printf("Test 6: NULL inputs via sequal_full\n");
errno = 0; errno = 0;
@@ -123,7 +123,7 @@ int main(void)
CHECK(errno == EINVAL && r == 0, "NULL s2: errno=EINVAL, returns 0"); CHECK(errno == EINVAL && r == 0, "NULL s2: errno=EINVAL, returns 0");
} }
/* Test 7: zero-size buffers should set errno=EINVAL */ /* Test 7: zero-size buffers -- should set errno=EINVAL */
{ {
printf("Test 7: zero-size buffers\n"); printf("Test 7: zero-size buffers\n");
errno = 0; errno = 0;
@@ -142,23 +142,23 @@ int main(void)
/* single char equal */ /* single char equal */
errno = 0; errno = 0;
r = sequal("a", "a", 0.85); r = sequal("a", "a", 0.85);
CHECK(r == 0 && errno == 0, "'a' vs 'a' 0 (equal)"); CHECK(r == 0 && errno == 0, "'a' vs 'a' -> 0 (equal)");
/* single char different */ /* single char different */
errno = 0; errno = 0;
r = sequal("a", "b", 0.85); r = sequal("a", "b", 0.85);
CHECK(r != 0 && errno == 0, "'a' vs 'b' non-zero (different)"); CHECK(r != 0 && errno == 0, "'a' vs 'b' -> non-zero (different)");
/* threshold = 0.0: any non-zero similarity matches 0 even when different */ /* threshold = 0.0: any non-zero similarity matches -> 0 even when different */
r = sequal("hello", "world", 0.0); r = sequal("hello", "world", 0.0);
CHECK(r == 0, "shold=0.0 makes any non-empty pair 'match'"); CHECK(r == 0, "shold=0.0 makes any non-empty pair 'match'");
/* threshold = 1.0: only exact-after-normalize strict strcmp */ /* threshold = 1.0: only exact-after-normalize -> strict strcmp */
r = sequal("hello", "Hello", 1.0); r = sequal("hello", "Hello", 1.0);
CHECK(r == 0, "shold=1.0 + case-only diff still matches (normalized exact)"); CHECK(r == 0, "shold=1.0 + case-only diff still matches (normalized exact)");
r = sequal("hello", "world", 1.0); r = sequal("hello", "world", 1.0);
CHECK(r != 0, "shold=1.0 + completely different non-zero"); CHECK(r != 0, "shold=1.0 + completely different -> non-zero");
/* whitespace-only strings */ /* whitespace-only strings */
r = sequal(" ", " ", 0.85); r = sequal(" ", " ", 0.85);