github.com/ncruces/go-sqlite3@v0.15.1-0.20240520133447-53eef1510ff0/sqlite3/time.c (about)

     1  #include <stddef.h>
     2  #include <string.h>
     3  
     4  #include "sqlite3.h"
     5  
     6  static int time_collation(void *pArg, int nKey1, const void *pKey1, int nKey2,
     7                            const void *pKey2) {
     8    // Remove a Z suffix if one key is no longer than the other.
     9    // A Z suffix collates before any character but after the empty string.
    10    // This avoids making different keys equal.
    11    const int nK1 = nKey1;
    12    const int nK2 = nKey2;
    13    const char *pK1 = (const char *)pKey1;
    14    const char *pK2 = (const char *)pKey2;
    15    if (nK1 && nK1 <= nK2 && pK1[nK1 - 1] == 'Z') {
    16      nKey1--;
    17    }
    18    if (nK2 && nK2 <= nK1 && pK2[nK2 - 1] == 'Z') {
    19      nKey2--;
    20    }
    21  
    22    int n = nKey1 < nKey2 ? nKey1 : nKey2;
    23    int rc = memcmp(pKey1, pKey2, n);
    24    if (rc == 0) {
    25      rc = nKey1 - nKey2;
    26    }
    27    return rc;
    28  }
    29  
    30  int sqlite3_time_init(sqlite3 *db, char **pzErrMsg,
    31                        const sqlite3_api_routines *pApi) {
    32    sqlite3_create_collation_v2(db, "time", SQLITE_UTF8, /*arg=*/NULL,
    33                                time_collation,
    34                                /*destroy=*/NULL);
    35    return SQLITE_OK;
    36  }