Replace the randomHex() function with separate functions

randomBlob() and hex(). (CVS 3620)

FossilOrigin-Name: f5ad74a9bc57e83c11beb3cf46bb6cd8c9de3f86
This commit is contained in:
drh 2007-01-29 17:58:28 +00:00
parent 63cf66f02e
commit 137c728f5a
5 changed files with 82 additions and 47 deletions

View File

@ -1,5 +1,5 @@
C Add\sthe\srandomhex()\sfunction\sas\sa\sbuilt-in.\s(CVS\s3619)
D 2007-01-29T15:50:06
C Replace\sthe\srandomHex()\sfunction\swith\sseparate\sfunctions\nrandomBlob()\sand\shex().\s(CVS\s3620)
D 2007-01-29T17:58:28
F Makefile.in 7fa74bf4359aa899da5586e394d17735f221315f
F Makefile.linux-gcc 2d8574d1ba75f129aba2019f0b959db380a90935
F README 9c4e2d6706bdcc3efdd773ce752a8cdab4f90028
@ -66,7 +66,7 @@ F src/date.c 393c73fc027597e008dcd81454544659e978b05c
F src/delete.c 804384761144fe1a5035b99f4bd7d706976831bd
F src/experimental.c 1b2d1a6cd62ecc39610e97670332ca073c50792b
F src/expr.c 3d2cf15fd9aa6b60b67f45504782cce72cd07963
F src/func.c 5df5260055fc8225df7daa4a6776763c78839a6a
F src/func.c b7e1e220a6795ecae7649815145ea5f8644dfa5f
F src/hash.c 449f3d6620193aa557f5d86cbc5cc6b87702b185
F src/hash.h 1b3f7e2609141fd571f62199fc38687d262e9564
F src/insert.c e9526ced19978a55687b55faea969b6ff2a53fb4
@ -217,7 +217,7 @@ F test/fts2f.test b5f2dde48199d79e859f59d3d857c17dd62a0129
F test/fts2g.test c69a8ab43ec77d123976ba6cf9422d647ae63032
F test/fts2h.test 223af921323b409d4b5b18ff4e51619541b174bb
F test/fts2i.test 1b22451d1f13f7c509baec620dc3a4a754885dd6
F test/func.test a2a780de5f32fcdda3db9421281f87c7cf5f1077
F test/func.test 71938e2ac704d8ce12f11810d475597640656ae9
F test/hook.test 7e7645fd9a033f79cce8fdff151e32715e7ec50a
F test/in.test 369cb2aa1eab02296b4ec470732fe8c131260b1d
F test/index.test e65df12bed94b2903ee89987115e1578687e9266
@ -406,7 +406,7 @@ F www/fullscanb.gif f7c94cb227f060511f8909e10f570157263e9a25
F www/index-ex1-x-b.gif f9b1d85c3fa2435cf38b15970c7e3aa1edae23a3
F www/index.tcl 7f67d421e4c1f48083c178bee460a27220f4fcc1
F www/indirect1b1.gif adfca361d2df59e34f9c5cac52a670c2bfc303a1
F www/lang.tcl ccbd38d9597117841b130287a19534b14dbf8990
F www/lang.tcl aa6d599679cbd5612d7fd7b1ae733ac1221c2a76
F www/lockingv3.tcl f59b19d6c8920a931f096699d6faaf61c05db55f
F www/mingw.tcl d96b451568c5d28545fefe0c80bee3431c73f69c
F www/nulls.tcl ec35193f92485b87b90a994a01d0171b58823fcf
@ -428,7 +428,7 @@ F www/tclsqlite.tcl bb0d1357328a42b1993d78573e587c6dcbc964b9
F www/vdbe.tcl 87a31ace769f20d3627a64fa1fade7fed47b90d0
F www/version3.tcl 890248cf7b70e60c383b0e84d77d5132b3ead42b
F www/whentouse.tcl 97e2b5cd296f7d8057e11f44427dea8a4c2db513
P 027251a6fc9971b337172436137fabdafec1d264
R 26303d3b8e5ddcde80949e4541bf7afa
P a6001589ab1349f7a6b4af941e9e0fd73d13c1c0
R 81b8a9e1e2346eee4d0809c0a57c1d63
U drh
Z 353a878b84641f62bd1524ee3fc8f147
Z e549908b92c57478539350b74a145cc3

View File

@ -1 +1 @@
a6001589ab1349f7a6b4af941e9e0fd73d13c1c0
f5ad74a9bc57e83c11beb3cf46bb6cd8c9de3f86

View File

@ -16,7 +16,7 @@
** sqliteRegisterBuildinFunctions() found at the bottom of the file.
** All other code has file scope.
**
** $Id: func.c,v 1.135 2007/01/29 15:50:06 drh Exp $
** $Id: func.c,v 1.136 2007/01/29 17:58:28 drh Exp $
*/
#include "sqliteInt.h"
#include <ctype.h>
@ -273,30 +273,22 @@ static void randomFunc(
}
/*
** Implementation of randomhex(N). Return a random hexadecimal string
** that is N characters long.
** Implementation of randomblob(N). Return a random blob
** that is N bytes long.
*/
static void randomHex(
static void randomBlob(
sqlite3_context *context,
int argc,
sqlite3_value **argv
){
int n, i, j;
unsigned char c, zBuf[1001];
int n;
unsigned char *p;
assert( argc==1 );
n = sqlite3_value_int(argv[0]);
if( n&1 ) n++;
if( n<2 ) n = 2;
if( n>sizeof(zBuf)-1 ) n = sizeof(zBuf)-1;
sqlite3Randomness(n/2, zBuf);
for(i=n-1, j=n/2-1; i>=1; i-=2, j--){
static const char zDigits[] = "0123456789ABCDEF";
c = zBuf[j];
zBuf[i] = zDigits[c&0xf];
zBuf[i-1] = zDigits[c>>4];
}
zBuf[n] = 0;
sqlite3_result_text(context, (char*)zBuf, n, SQLITE_TRANSIENT);
if( n<1 ) n = 1;
p = sqlite3_malloc(n);
sqlite3Randomness(n, p);
sqlite3_result_blob(context, (char*)p, n, sqlite3_free);
}
/*
@ -575,6 +567,12 @@ static void versionFunc(
sqlite3_result_text(context, sqlite3_version, -1, SQLITE_STATIC);
}
/* Array for converting from half-bytes (nybbles) into ASCII hex
** digits. */
static const char hexdigits[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
/*
** EXPERIMENTAL - This is not an official function. The interface may
@ -600,10 +598,6 @@ static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
break;
}
case SQLITE_BLOB: {
static const char hexdigits[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'
};
char *zText = 0;
int nBlob = sqlite3_value_bytes(argv[0]);
char const *zBlob = sqlite3_value_blob(argv[0]);
@ -649,11 +643,41 @@ static void quoteFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
}
}
/*
** The hex() function. Interpret the argument as a blob. Return
** a hexadecimal rendering as text.
*/
static void hexFunc(
sqlite3_context *context,
int argc,
sqlite3_value **argv
){
int i, n;
const unsigned char *pBlob;
char *zHex, *z;
assert( argc==1 );
pBlob = sqlite3_value_blob(argv[0]);
n = sqlite3_value_bytes(argv[0]);
z = zHex = sqlite3_malloc(n*2 + 1);
if( zHex==0 ) return;
for(i=0; i<n; i++, pBlob++){
unsigned char c = *pBlob;
*(z++) = hexdigits[(c>>4)&0xf];
*(z++) = hexdigits[c&0xf];
}
*z = 0;
sqlite3_result_text(context, zHex, n*2, sqlite3_free);
}
#ifdef SQLITE_SOUNDEX
/*
** Compute the soundex encoding of a word.
*/
static void soundexFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
static void soundexFunc(
sqlite3_context *context,
int argc,
sqlite3_value **argv
){
char zResult[8];
const u8 *zIn;
int i, j;
@ -1049,9 +1073,10 @@ void sqlite3RegisterBuiltinFunctions(sqlite3 *db){
{ "coalesce", -1, 0, SQLITE_UTF8, 0, ifnullFunc },
{ "coalesce", 0, 0, SQLITE_UTF8, 0, 0 },
{ "coalesce", 1, 0, SQLITE_UTF8, 0, 0 },
{ "hex", 1, 0, SQLITE_UTF8, 0, hexFunc },
{ "ifnull", 2, 0, SQLITE_UTF8, 1, ifnullFunc },
{ "random", -1, 0, SQLITE_UTF8, 0, randomFunc },
{ "randomhex", 1, 0, SQLITE_UTF8, 0, randomHex },
{ "randomblob", 1, 0, SQLITE_UTF8, 0, randomBlob },
{ "nullif", 2, 0, SQLITE_UTF8, 1, nullifFunc },
{ "sqlite_version", 0, 0, SQLITE_UTF8, 0, versionFunc},
{ "quote", 1, 0, SQLITE_UTF8, 0, quoteFunc },

View File

@ -11,7 +11,7 @@
# This file implements regression tests for SQLite library. The
# focus of this file is testing built-in functions.
#
# $Id: func.test,v 1.56 2007/01/29 15:50:06 drh Exp $
# $Id: func.test,v 1.57 2007/01/29 17:58:28 drh Exp $
set testdir [file dirname $argv0]
source $testdir/tester.tcl
@ -303,21 +303,28 @@ do_test func-9.2 {
} {integer}
do_test func-9.3 {
execsql {
SELECT randomhex(32) is not null;
SELECT randomblob(32) is not null;
}
} {1}
do_test func-9.4 {
execsql {
SELECT typeof(randomhex(32));
SELECT typeof(randomblob(32));
}
} {text}
} {blob}
do_test func-9.5 {
execsql {
SELECT length(randomhex(32)), length(randomhex(-5)),
length(randomhex(2000)), length(randomhex(31));
SELECT length(randomblob(32)), length(randomblob(-5)),
length(randomblob(2000))
}
} {32 2 1000 32}
} {32 1 2000}
# The "hex()" function was added in order to be able to render blobs
# generated by randomblob(). So this seems like a good place to test
# hex().
#
do_test func-9.10 {
execsql {SELECT hex(x'00112233445566778899aAbBcCdDeEfF')}
} {00112233445566778899AABBCCDDEEFF}
# Use the "sqlite_register_test_function" TCL command which is part of
# the text fixture in order to verify correct operation of some of

View File

@ -1,7 +1,7 @@
#
# Run this Tcl script to generate the lang-*.html files.
#
set rcsid {$Id: lang.tcl,v 1.119 2007/01/29 15:50:06 drh Exp $}
set rcsid {$Id: lang.tcl,v 1.120 2007/01/29 17:58:28 drh Exp $}
source common.tcl
if {[llength $argv]>0} {
@ -1282,6 +1282,12 @@ both arguments are NULL then NULL is returned. This behaves the same as
<b>coalesce()</b> above.</td>
</tr>
<tr>
<td valign="top" align="right">hex(<i>X</i>)</td>
<td valign="top">The argument is interpreted as a BLOB. The result
is a hexadecimal rendering of the content of that blob.</td>
</tr>
<tr>
<td valign="top" align="right">last_insert_rowid()</td>
<td valign="top">Return the ROWID of the last row insert from this
@ -1374,12 +1380,9 @@ between -9223372036854775808 and +9223372036854775807.</td>
</tr>
<tr>
<td valign="top" align="right">randomhex(<i>N</i>)</td>
<td valign="top">Return a pseudo-random hexadecimal string that is
<i>N</i> characters in length. <i>N</i> should be an even integer between
2 and 1000. The intended use of this function is to generate
universally unique identifiers (UUID). For that purpose, it is recommended
that <i>N</i> be at least 32.</td>
<td valign="top" align="right">randomblob(<i>N</i>)</td>
<td valign="top">Return a <i>N</i>-byte blob containing pseudo-random bytes.
<i>N</i> should be a postive integer.</td>
</tr>
<tr>