|
| 1 | +/* |
| 2 | +** GenomicSQLite bundles the UINT extension: https://www.sqlite.org/uintcseq.html |
| 3 | +** |
| 4 | +** This is the (public domain) upstream source file from: |
| 5 | +** https://sqlite.org/src/file/ext/misc/uint.c |
| 6 | +** with minor edits deferring to genomicsqlite.cc:sqlite3_genomicsqlite_init for extension init |
| 7 | +*/ |
| 8 | + |
| 9 | +/* |
| 10 | +** 2020-04-14 |
| 11 | +** |
| 12 | +** The author disclaims copyright to this source code. In place of |
| 13 | +** a legal notice, here is a blessing: |
| 14 | +** |
| 15 | +** May you do good and not evil. |
| 16 | +** May you find forgiveness for yourself and forgive others. |
| 17 | +** May you share freely, never taking more than you give. |
| 18 | +** |
| 19 | +****************************************************************************** |
| 20 | +** |
| 21 | +** This SQLite extension implements the UINT collating sequence. |
| 22 | +** |
| 23 | +** UINT works like BINARY for text, except that embedded strings |
| 24 | +** of digits compare in numeric order. |
| 25 | +** |
| 26 | +** * Leading zeros are handled properly, in the sense that |
| 27 | +** they do not mess of the maginitude comparison of embedded |
| 28 | +** strings of digits. "x00123y" is equal to "x123y". |
| 29 | +** |
| 30 | +** * Only unsigned integers are recognized. Plus and minus |
| 31 | +** signs are ignored. Decimal points and exponential notation |
| 32 | +** are ignored. |
| 33 | +** |
| 34 | +** * Embedded integers can be of arbitrary length. Comparison |
| 35 | +** is *not* limited integers that can be expressed as a |
| 36 | +** 64-bit machine integer. |
| 37 | +*/ |
| 38 | +#include "sqlite3ext.h" |
| 39 | +/* instead of SQLITE_EXTENSION_INIT1, link to the one in genomicsqlite.cc: */ |
| 40 | +extern const sqlite3_api_routines *sqlite3_api; |
| 41 | +#include <assert.h> |
| 42 | +#include <string.h> |
| 43 | +#include <ctype.h> |
| 44 | + |
| 45 | +/* |
| 46 | +** Compare text in lexicographic order, except strings of digits |
| 47 | +** compare in numeric order. |
| 48 | +*/ |
| 49 | +static int uintCollFunc( |
| 50 | + void *notUsed, |
| 51 | + int nKey1, const void *pKey1, |
| 52 | + int nKey2, const void *pKey2 |
| 53 | +){ |
| 54 | + const unsigned char *zA = (const unsigned char*)pKey1; |
| 55 | + const unsigned char *zB = (const unsigned char*)pKey2; |
| 56 | + int i=0, j=0, x; |
| 57 | + (void)notUsed; |
| 58 | + while( i<nKey1 && j<nKey2 ){ |
| 59 | + x = zA[i] - zB[j]; |
| 60 | + if( isdigit(zA[i]) ){ |
| 61 | + int k; |
| 62 | + if( !isdigit(zB[j]) ) return x; |
| 63 | + while( i<nKey1 && zA[i]=='0' ){ i++; } |
| 64 | + while( j<nKey2 && zB[j]=='0' ){ j++; } |
| 65 | + k = 0; |
| 66 | + while( i+k<nKey1 && isdigit(zA[i+k]) |
| 67 | + && j+k<nKey2 && isdigit(zB[j+k]) ){ |
| 68 | + k++; |
| 69 | + } |
| 70 | + if( i+k<nKey1 && isdigit(zA[i+k]) ){ |
| 71 | + return +1; |
| 72 | + }else if( j+k<nKey2 && isdigit(zB[j+k]) ){ |
| 73 | + return -1; |
| 74 | + }else{ |
| 75 | + x = memcmp(zA+i, zB+j, k); |
| 76 | + if( x ) return x; |
| 77 | + i += k; |
| 78 | + j += k; |
| 79 | + } |
| 80 | + }else if( x ){ |
| 81 | + return x; |
| 82 | + }else{ |
| 83 | + i++; |
| 84 | + j++; |
| 85 | + } |
| 86 | + } |
| 87 | + return (nKey1 - i) - (nKey2 - j); |
| 88 | +} |
| 89 | + |
| 90 | +#ifdef _WIN32 |
| 91 | +__declspec(dllexport) |
| 92 | +#endif |
| 93 | +int genomicsqlite_uint_init( |
| 94 | + sqlite3 *db |
| 95 | +){ |
| 96 | + return sqlite3_create_collation(db, "uint", SQLITE_UTF8, 0, uintCollFunc); |
| 97 | +} |
0 commit comments