4 ** The author disclaims copyright to this source code. In place of
5 ** a legal notice, here is a blessing:
7 ** May you do good and not evil.
8 ** May you find forgiveness for yourself and forgive others.
9 ** May you share freely, never taking more than you give.
11 ******************************************************************************
13 ** This SQLite extension implements the UINT collating sequence.
15 ** UINT works like BINARY for text, except that embedded strings
16 ** of digits compare in numeric order.
18 ** * Leading zeros are handled properly, in the sense that
19 ** they do not mess of the maginitude comparison of embedded
20 ** strings of digits. "x00123y" is equal to "x123y".
22 ** * Only unsigned integers are recognized. Plus and minus
23 ** signs are ignored. Decimal points and exponential notation
26 ** * Embedded integers can be of arbitrary length. Comparison
27 ** is *not* limited integers that can be expressed as a
28 ** 64-bit machine integer.
30 #include "sqlite3ext.h"
31 SQLITE_EXTENSION_INIT1
37 ** Compare text in lexicographic order, except strings of digits
38 ** compare in numeric order.
40 static int uintCollFunc(
42 int nKey1
, const void *pKey1
,
43 int nKey2
, const void *pKey2
45 const unsigned char *zA
= (const unsigned char*)pKey1
;
46 const unsigned char *zB
= (const unsigned char*)pKey2
;
49 while( i
<nKey1
&& j
<nKey2
){
53 if( !isdigit(zB
[j
]) ) return x
;
54 while( i
<nKey1
&& zA
[i
]=='0' ){ i
++; }
55 while( j
<nKey2
&& zB
[j
]=='0' ){ j
++; }
57 while( i
+k
<nKey1
&& isdigit(zA
[i
+k
])
58 && j
+k
<nKey2
&& isdigit(zB
[j
+k
]) ){
61 if( i
+k
<nKey1
&& isdigit(zA
[i
+k
]) ){
63 }else if( j
+k
<nKey2
&& isdigit(zB
[j
+k
]) ){
66 x
= memcmp(zA
+i
, zB
+j
, k
);
78 return (nKey1
- i
) - (nKey2
- j
);
84 int sqlite3_uint_init(
87 const sqlite3_api_routines
*pApi
89 SQLITE_EXTENSION_INIT2(pApi
);
90 (void)pzErrMsg
; /* Unused parameter */
91 return sqlite3_create_collation(db
, "uint", SQLITE_UTF8
, 0, uintCollFunc
);