IMPROVES-i11 sequal now uses errno and returns -1,0,+1

This commit is contained in:
2026-05-22 19:31:51 -03:00
parent 975bcfbe1f
commit 4cdae6d621

View File

@@ -34,6 +34,7 @@
#include <math.h> #include <math.h>
#include <ctype.h> #include <ctype.h>
#include <float.h> #include <float.h>
#include <errno.h>
#define SBUFF 256 /* Max string size */ #define SBUFF 256 /* Max string size */
@@ -121,7 +122,15 @@ cmp11sht v20251221.0718 (C) 2025 by Ruben C. Benante (MIT Lic)\n\n"
if(fenda == argv[1] || fendb == argv[2]) // string if(fenda == argv[1] || fendb == argv[2]) // string
{ {
if(opt==3) printf("cmp11sht: string\n"); if(opt==3) printf("cmp11sht: string\n");
errno = 0;
res = sequal(argv[1], argv[2], delta, &ratio, s1, s2); res = sequal(argv[1], argv[2], delta, &ratio, s1, s2);
if(errno == EINVAL)
res = 3; /* error → CLI exit 3 */
else if(res > 0)
res = 1; /* a > b → CLI exit 1 */
else if(res < 0)
res = 2; /* a < b → CLI exit 2 */
/* 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);
if(opt==3) printf("ratio: "); if(opt==3) printf("ratio: ");
@@ -132,6 +141,11 @@ 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)
res = 1; /* a > b → CLI exit 1 */
else if(res < 0)
res = 2; /* a < b → CLI exit 2 */
/* 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);
if(opt==3) printf("diff: "); if(opt==3) printf("diff: ");
@@ -151,25 +165,32 @@ cmp11sht v20251221.0718 (C) 2025 by Ruben C. Benante (MIT Lic)\n\n"
int fequal(float a, float b, float delta) int fequal(float a, float b, float delta)
{ {
if(a < b - delta) if(a < b - delta)
return 2; return -1;
if(a > b + delta) if(a > b + delta)
return 1; return 1;
/* b-delta <= a <= b+delta */ /* b-delta <= a <= b+delta */
return 0; return 0;
} }
/* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */
/* compare similarity between two strings /* compare similarity between two strings.
* Return 0 if similar above given threshold * Return:
* -1 if a < b alphabetically * 0 if equal or similar above given threshold
* +1 if a > b alphabetically * -1 if a < b alphabetically (after normalization)
* +1 if a > b alphabetically (after normalization)
* On error: sets errno = EINVAL and returns 0; result is undefined.
* Caller must reset errno = 0 before the call to detect errors.
*/ */
int sequal(char *a, char *b, float thr, float *ratio, char *s1, char *s2) int sequal(char *a, char *b, float thr, float *ratio, char *s1, char *s2)
{ {
int i; int i;
if(!a || !b || !s1 || !s2 || !ratio) return 3; /* error sentinel */ if(!a || !b || !s1 || !s2 || !ratio)
{
errno = EINVAL;
return 0;
}
// remove accents // remove accents
asciify(a, s1); asciify(a, s1);
@@ -194,7 +215,7 @@ int sequal(char *a, char *b, float thr, float *ratio, char *s1, char *s2)
*ratio = shit11(s1, s2); *ratio = shit11(s1, s2);
if(*ratio > thr) if(*ratio > thr)
return 0; return 0;
return (i < 0)? 2 : 1; return (i < 0)? -1 : 1;
} }
/* ---------------------------------------------------------------------- */ /* ---------------------------------------------------------------------- */