IMPROVES-i6 wrapper sequal(a,b,shold) : sequal_full(...)
This commit is contained in:
@@ -89,7 +89,7 @@ cmp11sht v20251221.0718 (C) 2025 by Ruben C. Benante (MIT Lic)\n\n"
|
|||||||
{
|
{
|
||||||
if(opt==3) printf("cmp11sht: string\n");
|
if(opt==3) printf("cmp11sht: string\n");
|
||||||
errno = 0;
|
errno = 0;
|
||||||
res = sequal(argv[1], argv[2], delta, &ratio, s1, s2);
|
res = sequal_full(argv[1], argv[2], delta, &ratio, s1, s2);
|
||||||
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)
|
||||||
|
|||||||
14
lib11sht.c
14
lib11sht.c
@@ -36,7 +36,17 @@ int fequal(float a, float b, float delta)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* ---------------------------------------------------------------------- */
|
/* ---------------------------------------------------------------------- */
|
||||||
int sequal(char *a, char *b, float thr, float *ratio, char *s1, char *s2)
|
/* Simple wrapper: most callers don't need the ratio or normalized buffers.
|
||||||
|
* Symmetric with fequal(a, b, delta). */
|
||||||
|
int sequal(char *a, char *b, float shold)
|
||||||
|
{
|
||||||
|
float ratio;
|
||||||
|
char s1[LEVN_SBUFF], s2[LEVN_SBUFF];
|
||||||
|
return sequal_full(a, b, shold, &ratio, s1, s2);
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ---------------------------------------------------------------------- */
|
||||||
|
int sequal_full(char *a, char *b, float shold, float *ratio, char *s1, char *s2)
|
||||||
{
|
{
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
@@ -67,7 +77,7 @@ int sequal(char *a, char *b, float thr, float *ratio, char *s1, char *s2)
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
*ratio = shit11(s1, s2);
|
*ratio = shit11(s1, s2);
|
||||||
if(*ratio > thr)
|
if(*ratio > shold)
|
||||||
return 0;
|
return 0;
|
||||||
return (i < 0)? -1 : 1;
|
return (i < 0)? -1 : 1;
|
||||||
}
|
}
|
||||||
|
|||||||
21
lib11sht.h
21
lib11sht.h
@@ -12,8 +12,9 @@
|
|||||||
#define LEVN_SBUFF 256 /* min size for s1/s2 buffers and bounded input */
|
#define LEVN_SBUFF 256 /* min size for s1/s2 buffers and bounded input */
|
||||||
|
|
||||||
/* Compare similarity between two strings (after asciify + trim + lowercase).
|
/* Compare similarity between two strings (after asciify + trim + lowercase).
|
||||||
|
* Symmetric in shape with fequal(a, b, delta).
|
||||||
* Returns:
|
* Returns:
|
||||||
* 0 if equal or similar above the given threshold ratio
|
* 0 if equal or similar above the lratio 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; comparison result is undefined.
|
* On error: sets errno = EINVAL and returns 0; comparison result is undefined.
|
||||||
@@ -21,12 +22,20 @@
|
|||||||
*
|
*
|
||||||
* 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)
|
||||||
* thr Levenshtein similarity threshold 0.0..1.0; matches above this count as equal
|
* shold Levenshtein similarity threshold 0.0..1.0; matches above this count as equal
|
||||||
* ratio out: Levenshtein similarity 0.0..1.0 (1.0 on exact-after-normalize)
|
|
||||||
* s1, s2 out: caller-provided buffers (>= LEVN_SBUFF) filled with the
|
|
||||||
* normalized inputs (useful for diagnostics; can be ignored)
|
|
||||||
*/
|
*/
|
||||||
int sequal(char *a, char *b, float thr, float *ratio, char *s1, char *s2);
|
int sequal(char *a, char *b, float shold);
|
||||||
|
|
||||||
|
/* Full variant of sequal: same comparison but also returns diagnostics.
|
||||||
|
* Used by callers that need the computed ratio or the normalized strings
|
||||||
|
* (e.g. cmp11sht CLI's -o / -n flags).
|
||||||
|
*
|
||||||
|
* Extra parameters:
|
||||||
|
* ratio out: Levenshtein similarity 0.0..1.0 (1.0 on exact-after-normalize)
|
||||||
|
* s1, s2 out: caller-provided buffers (>= LEVN_SBUFF) filled with the
|
||||||
|
* normalized inputs
|
||||||
|
*/
|
||||||
|
int sequal_full(char *a, char *b, float shold, float *ratio, char *s1, char *s2);
|
||||||
|
|
||||||
/* Compare two floats within ±delta.
|
/* Compare two floats within ±delta.
|
||||||
* Returns:
|
* Returns:
|
||||||
|
|||||||
Reference in New Issue
Block a user