60 lines
2.5 KiB
C
60 lines
2.5 KiB
C
/* ************************************************************************ *
|
|
* lib11sht.h, v1.0 *
|
|
* Fuzzy comparison library: public API *
|
|
* *
|
|
* Copyright (C) 2025 by Ruben Carlo Benante <rcb@beco.cc> *
|
|
* GNU GPL version 2 or later. *
|
|
* ************************************************************************ */
|
|
|
|
#ifndef LIB11SHT_H
|
|
#define LIB11SHT_H
|
|
|
|
#include <stddef.h> /* size_t */
|
|
|
|
#define LEVN_SBUFF 256 /* recommended size for s1/s2 buffers */
|
|
|
|
/* Compare similarity between two strings (after asciify + trim + lowercase).
|
|
* Symmetric in shape with fequal(a, b, delta).
|
|
* Returns:
|
|
* 0 if equal or similar above the lratio threshold
|
|
* -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.
|
|
* Caller must reset errno = 0 before the call to detect errors.
|
|
*
|
|
* Parameters:
|
|
* a, b input strings (NUL-terminated, may contain UTF-8 accented Latin chars)
|
|
* shold Levenshtein similarity threshold 0.0..1.0; matches above this count as equal
|
|
*/
|
|
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 filled with the normalized inputs
|
|
* s1_size size of s1 in bytes (writes capped at s1_size-1 + final NUL)
|
|
* s2_size size of s2 in bytes (writes capped at s2_size-1 + final NUL)
|
|
*/
|
|
int sequal_full(char *a, char *b, float shold, float *ratio,
|
|
char *s1, size_t s1_size,
|
|
char *s2, size_t s2_size);
|
|
|
|
/* Compare two floats within ±delta.
|
|
* Returns:
|
|
* 0 if |a - b| <= delta
|
|
* -1 if a < b - delta
|
|
* +1 if a > b + delta
|
|
*/
|
|
int fequal(float a, float b, float delta);
|
|
|
|
/* String trim: removes leading + trailing whitespace (including UTF-8
|
|
* NBSP bytes 0xC2 / 0xA0) AND collapses internal runs of whitespace
|
|
* to a single space. Modifies s in place. Caller's buffer must already
|
|
* be NUL-terminated. */
|
|
void trim(char *s);
|
|
|
|
#endif /* LIB11SHT_H */
|