IMPROVES-i11 sequal now uses errno and returns -1,0,+1
This commit is contained in:
35
cmp11sht.c
35
cmp11sht.c
@@ -34,6 +34,7 @@
|
||||
#include <math.h>
|
||||
#include <ctype.h>
|
||||
#include <float.h>
|
||||
#include <errno.h>
|
||||
|
||||
#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(opt==3) printf("cmp11sht: string\n");
|
||||
errno = 0;
|
||||
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) printf("%d\n", res);
|
||||
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");
|
||||
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) printf("%d\n", res);
|
||||
if(opt==3) printf("diff: ");
|
||||
@@ -151,7 +165,7 @@ cmp11sht v20251221.0718 (C) 2025 by Ruben C. Benante (MIT Lic)\n\n"
|
||||
int fequal(float a, float b, float delta)
|
||||
{
|
||||
if(a < b - delta)
|
||||
return 2;
|
||||
return -1;
|
||||
if(a > b + delta)
|
||||
return 1;
|
||||
|
||||
@@ -160,16 +174,23 @@ int fequal(float a, float b, float delta)
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
/* compare similarity between two strings
|
||||
* Return 0 if similar above given threshold
|
||||
* -1 if a < b alphabetically
|
||||
* +1 if a > b alphabetically
|
||||
/* compare similarity between two strings.
|
||||
* Return:
|
||||
* 0 if equal or similar above given threshold
|
||||
* -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 i;
|
||||
|
||||
if(!a || !b || !s1 || !s2 || !ratio) return 3; /* error sentinel */
|
||||
if(!a || !b || !s1 || !s2 || !ratio)
|
||||
{
|
||||
errno = EINVAL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
// remove accents
|
||||
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);
|
||||
if(*ratio > thr)
|
||||
return 0;
|
||||
return (i < 0)? 2 : 1;
|
||||
return (i < 0)? -1 : 1;
|
||||
}
|
||||
|
||||
/* ---------------------------------------------------------------------- */
|
||||
|
||||
Reference in New Issue
Block a user