From 0861b814130ab77b0e228e6f41a8c898959d69f1 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Tue, 9 Aug 2022 02:09:51 +0200 Subject: Two more fixes on csview comparison. --- include/stc/csview.h | 7 +++++-- src/utf8code.c | 4 ++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/include/stc/csview.h b/include/stc/csview.h index 61ab4d6b..2efeff5b 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -123,8 +123,11 @@ STC_INLINE csview cstr_u8_slice(const cstr* self , size_t u8p1, size_t u8p2) #endif /* ---- Container helper functions ---- */ -STC_INLINE int csview_cmp(const csview* x, const csview* y) - { return strcmp(x->str, y->str); } +STC_INLINE int csview_cmp(const csview* x, const csview* y) { + size_t n = x->size < y->size ? x->size : y->size; + int c = memcmp(x->str, y->str, n); + return c ? c : x->size - y->size; +} STC_INLINE int csview_icmp(const csview* x, const csview* y) { return utf8_icmp_sv(*x, *y); } diff --git a/src/utf8code.c b/src/utf8code.c index 1790ad62..ebd434ea 100644 --- a/src/utf8code.c +++ b/src/utf8code.c @@ -110,10 +110,10 @@ int utf8_icmp_sv(const csview s1, const csview s2) { do { utf8_decode(&d1, (uint8_t)s1.str[j1++]); } while (d1.state); do { utf8_decode(&d2, (uint8_t)s2.str[j2++]); } while (d2.state); int32_t c = utf8_casefold(d1.codep) - utf8_casefold(d2.codep); - if (c || !s2.str[j2 - 1]) // OK if s1.size / s2.size are npos + if (c || !s2.str[j2 - 1]) // OK if s1.size and s2.size are npos return c; } - return 0; + return s1.size - s2.size; } bool utf8_isupper(uint32_t c) { -- cgit v1.2.3