From 71da34861b4cb268f240518574a94957ac70c891 Mon Sep 17 00:00:00 2001 From: Nuno Cruces Date: Mon, 13 Mar 2023 04:19:06 +0000 Subject: [PATCH] Fix time collation. --- embed/sqlite3.wasm | Bin 1034512 -> 1034537 bytes sqlite3/time.c | 21 ++++++++++++--------- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/embed/sqlite3.wasm b/embed/sqlite3.wasm index f584eee0af73616c8f04ca7e8b9b7fab67b2e6db..6f0c6cbba9725031cdb7cbf39fe341694481d7c7 100755 GIT binary patch delta 257 zcmYMtKMKMy6vy$r{8iKH(nY$wboT~=?VyOb_7ZxC4i2so3LZh++#CcC;^r+J{RIby z4@uyg_fBzfjf-0>WPBg`;@B4i5)^1KV8OxQ-WS6%c26SJH6j?WvSwQ5f+<)gR-1Hf zx?Sb)lI(UfA)1$1mBD(X3TZl+wg^=)8F^Nz$6wY=|K6ZSYS|>~vPx@Yt!l>-Lo0Qj oe+JXvGRj3JJtnc+S31W`eAx%>Ss&ZW5Juo3gB-^9_A+^dFJt&MQ2+n{ delta 183 zcmZ2E$$r8l`wf#sn0`KJo+8pdMT8NEnShuXh*^M`6^Pk@m>q~Ywoeh^l%C|jl#!9S zp2BI BE_(m~ diff --git a/sqlite3/time.c b/sqlite3/time.c index 3b7a78c..374047d 100644 --- a/sqlite3/time.c +++ b/sqlite3/time.c @@ -4,15 +4,18 @@ static int time_collation(void *pArg, int nKey1, const void *pKey1, int nKey2, const void *pKey2) { - // If keys are of different length, and both terminated by a Z, - // ignore the Z for collation purposes. - if (nKey1 && nKey2 && nKey1 != nKey2) { - const char *pK1 = (const char *)pKey1; - const char *pK2 = (const char *)pKey2; - if (pK1[nKey1 - 1] == 'Z' && pK2[nKey2 - 1] == 'Z') { - nKey1--; - nKey2--; - } + // Remove a Z suffix if one key is no longer than the other. + // A Z suffix collates before any character but after the empty string. + // This avoids making different keys equal. + const int nK1 = nKey1; + const int nK2 = nKey2; + const char *pK1 = (const char *)pKey1; + const char *pK2 = (const char *)pKey2; + if (nK1 && nK1 <= nK2 && pK1[nK1 - 1] == 'Z') { + nKey1--; + } + if (nK2 && nK2 <= nK1 && pK2[nK2 - 1] == 'Z') { + nKey2--; } int n = nKey1 < nKey2 ? nKey1 : nKey2;