Implementation of the INSTR() SQL function, as found in SQL Server, MySQL,

and Oracle.

FossilOrigin-Name: 49ccae964f3a8ae5aab87f56503121e09424545f
This commit is contained in:
drh 2012-10-25 03:07:29 +00:00
parent 8749c1831a
commit d55e07296b
4 changed files with 271 additions and 7 deletions

View File

@ -1,5 +1,5 @@
C Make\ssure\ssubstructure\selements\shave\sproper\salignment\sin\sthe\sICU\stokenizers\nof\sFTS2\sand\sFTS3.
D 2012-10-19T02:10:53.511
C Implementation\sof\sthe\sINSTR()\sSQL\sfunction,\sas\sfound\sin\sSQL\sServer,\sMySQL,\nand\sOracle.
D 2012-10-25T03:07:29.004
F Makefile.arm-wince-mingw32ce-gcc d6df77f1f48d690bd73162294bbba7f59507c72f
F Makefile.in 5f4f26109f9d80829122e0e09f9cda008fa065fb
F Makefile.linux-gcc 91d710bdc4998cb015f39edf3cb314ec4f4d7e23
@ -133,7 +133,7 @@ F src/delete.c e35684ad93c741266b086610d2efd709b7946853
F src/expr.c 57fb8e7a05d4147e40b9f4c439e37ed2abab9332
F src/fault.c 160a0c015b6c2629d3899ed2daf63d75754a32bb
F src/fkey.c c82a04e7a92bb728f9ab972b76590403283be2af
F src/func.c cbb90dc84b22eea25caf39528d342279e61b8898
F src/func.c 1755cafdb8f2a291681f1ea55e0215518ebd6e52
F src/global.c fb44b11e02e06c995e6ed6642509edd23599d584
F src/hash.c ac3470bbf1ca4ae4e306a8ecb0fdf1731810ffe4
F src/hash.h 2894c932d84d9f892d4b4023a75e501f83050970
@ -544,6 +544,7 @@ F test/insert2.test 4f3a04d168c728ed5ec2c88842e772606c7ce435
F test/insert3.test 1b7db95a03ad9c5013fdf7d6722b6cd66ee55e30
F test/insert4.test 87f6798f31d60c4e177622fcc3663367e6ecbd90
F test/insert5.test 394f96728d1258f406fe5f5aeb0aaf29487c39a6
F test/instr.test a34e1d46a9eefb098a7167ef0e730a4a3d82fba0
F test/intarray.test 066b7d7ac38d25bf96f87f1b017bfc687551cdd4
F test/interrupt.test 42e7cf98646fd9cb4a3b131a93ed3c50b9e149f1
F test/intpkey.test 7af30f6ae852d8d1c2b70e4bf1551946742e92d8
@ -1021,7 +1022,10 @@ F tool/vdbe-compress.tcl f12c884766bd14277f4fcedcae07078011717381
F tool/warnings-clang.sh f6aa929dc20ef1f856af04a730772f59283631d4
F tool/warnings.sh fbc018d67fd7395f440c28f33ef0f94420226381
F tool/win/sqlite.vsix 67d8a99aceb56384a81b3f30d6c71743146d2cc9
P 0482c73bfdf80b0c0ce9abea706554d7ddf36f69
R 359b18ae3c286a09e1c7579425de96fe
P aaa2d9b0db74d8452d9294de17cff786ab4ec7c8
R f112d1955e32a3fb5862640b210a6a2c
T *branch * instr
T *sym-instr *
T -sym-trunk *
U drh
Z 09f8af841cfaabcfb0c92e8b07cb012e
Z 0dc399021047fb88f550870a282a864e

View File

@ -1 +1 @@
aaa2d9b0db74d8452d9294de17cff786ab4ec7c8
49ccae964f3a8ae5aab87f56503121e09424545f

View File

@ -168,6 +168,55 @@ static void absFunc(sqlite3_context *context, int argc, sqlite3_value **argv){
}
}
/*
** Implementation of the instr() function.
**
** instr(haystack,needle) finds the first occurrence of needle
** in haystack and returns the number of previous characters plus 1,
** or 0 if needle does not occur within haystack.
**
** If both haystack and needle are BLOBs, then the result is one more than
** the number of bytes in haystack prior to the first occurrence of needle,
** or 0 if needle never occurs in haystack.
*/
static void instrFunc(
sqlite3_context *context,
int argc,
sqlite3_value **argv
){
const unsigned char *zHaystack;
const unsigned char *zNeedle;
int nHaystack;
int nNeedle;
int typeHaystack, typeNeedle;
int N = 1;
int isText;
typeHaystack = sqlite3_value_type(argv[0]);
typeNeedle = sqlite3_value_type(argv[1]);
if( typeHaystack==SQLITE_NULL || typeNeedle==SQLITE_NULL ) return;
nHaystack = sqlite3_value_bytes(argv[0]);
nNeedle = sqlite3_value_bytes(argv[1]);
if( typeHaystack==SQLITE_BLOB && typeNeedle==SQLITE_BLOB ){
zHaystack = sqlite3_value_blob(argv[0]);
zNeedle = sqlite3_value_blob(argv[1]);
isText = 0;
}else{
zHaystack = sqlite3_value_text(argv[0]);
zNeedle = sqlite3_value_text(argv[1]);
isText = 1;
}
while( nNeedle<=nHaystack && memcmp(zHaystack, zNeedle, nNeedle)!=0 ){
N++;
do{
nHaystack--;
zHaystack++;
}while( isText && (zHaystack[0]&0xc0)==0x80 );
}
if( nNeedle>nHaystack ) N = 0;
sqlite3_result_int(context, N);
}
/*
** Implementation of the substr() function.
**
@ -1536,6 +1585,7 @@ void sqlite3RegisterGlobalFunctions(void){
AGGREGATE(max, 1, 1, 1, minmaxStep, minMaxFinalize ),
FUNCTION2(typeof, 1, 0, 0, typeofFunc, SQLITE_FUNC_TYPEOF),
FUNCTION2(length, 1, 0, 0, lengthFunc, SQLITE_FUNC_LENGTH),
FUNCTION(instr, 2, 0, 0, instrFunc ),
FUNCTION(substr, 2, 0, 0, substrFunc ),
FUNCTION(substr, 3, 0, 0, substrFunc ),
FUNCTION(abs, 1, 0, 0, absFunc ),

210
test/instr.test Normal file
View File

@ -0,0 +1,210 @@
# 2012 October 24
#
# The author disclaims copyright to this source code. In place of
# a legal notice, here is a blessing:
#
# May you do good and not evil.
# May you find forgiveness for yourself and forgive others.
# May you share freely, never taking more than you give.
#
#***********************************************************************
# This file implements regression tests for SQLite library. The
# focus of this file is testing the built-in INSTR() functions.
#
set testdir [file dirname $argv0]
source $testdir/tester.tcl
# Create a table to work with.
#
do_test instr-1.1 {
db eval {SELECT instr('abcdefg','a');}
} {1}
do_test instr-1.2 {
db eval {SELECT instr('abcdefg','b');}
} {2}
do_test instr-1.3 {
db eval {SELECT instr('abcdefg','c');}
} {3}
do_test instr-1.4 {
db eval {SELECT instr('abcdefg','d');}
} {4}
do_test instr-1.5 {
db eval {SELECT instr('abcdefg','e');}
} {5}
do_test instr-1.6 {
db eval {SELECT instr('abcdefg','f');}
} {6}
do_test instr-1.7 {
db eval {SELECT instr('abcdefg','g');}
} {7}
do_test instr-1.8 {
db eval {SELECT instr('abcdefg','h');}
} {0}
do_test instr-1.9 {
db eval {SELECT instr('abcdefg','abcdefg');}
} {1}
do_test instr-1.10 {
db eval {SELECT instr('abcdefg','abcdefgh');}
} {0}
do_test instr-1.11 {
db eval {SELECT instr('abcdefg','bcdefg');}
} {2}
do_test instr-1.12 {
db eval {SELECT instr('abcdefg','bcdefgh');}
} {0}
do_test instr-1.13 {
db eval {SELECT instr('abcdefg','cdefg');}
} {3}
do_test instr-1.14 {
db eval {SELECT instr('abcdefg','cdefgh');}
} {0}
do_test instr-1.15 {
db eval {SELECT instr('abcdefg','defg');}
} {4}
do_test instr-1.16 {
db eval {SELECT instr('abcdefg','defgh');}
} {0}
do_test instr-1.17 {
db eval {SELECT instr('abcdefg','efg');}
} {5}
do_test instr-1.18 {
db eval {SELECT instr('abcdefg','efgh');}
} {0}
do_test instr-1.19 {
db eval {SELECT instr('abcdefg','fg');}
} {6}
do_test instr-1.20 {
db eval {SELECT instr('abcdefg','fgh');}
} {0}
do_test instr-1.21 {
db eval {SELECT coalesce(instr('abcdefg',NULL),'nil');}
} {nil}
do_test instr-1.22 {
db eval {SELECT coalesce(instr(NULL,'x'),'nil');}
} {nil}
do_test instr-1.23 {
db eval {SELECT instr(12345,34);}
} {3}
do_test instr-1.24 {
db eval {SELECT instr(123456.78,34);}
} {3}
do_test instr-1.25 {
db eval {SELECT instr(123456.78,x'3334');}
} {3}
do_test instr-1.26 {
db eval {SELECT instr('äbcdefg','efg');}
} {5}
do_test instr-1.27 {
db eval {SELECT instr('€xyzzy','xyz');}
} {2}
do_test instr-1.28 {
db eval {SELECT instr('abc€xyzzy','xyz');}
} {5}
do_test instr-1.29 {
db eval {SELECT instr('abc€xyzzy','€xyz');}
} {4}
do_test instr-1.30 {
db eval {SELECT instr('abc€xyzzy','c€xyz');}
} {3}
do_test instr-1.31 {
db eval {SELECT instr(x'0102030405',x'01');}
} {1}
do_test instr-1.32 {
db eval {SELECT instr(x'0102030405',x'02');}
} {2}
do_test instr-1.33 {
db eval {SELECT instr(x'0102030405',x'03');}
} {3}
do_test instr-1.34 {
db eval {SELECT instr(x'0102030405',x'04');}
} {4}
do_test instr-1.35 {
db eval {SELECT instr(x'0102030405',x'05');}
} {5}
do_test instr-1.36 {
db eval {SELECT instr(x'0102030405',x'06');}
} {0}
do_test instr-1.37 {
db eval {SELECT instr(x'0102030405',x'0102030405');}
} {1}
do_test instr-1.38 {
db eval {SELECT instr(x'0102030405',x'02030405');}
} {2}
do_test instr-1.39 {
db eval {SELECT instr(x'0102030405',x'030405');}
} {3}
do_test instr-1.40 {
db eval {SELECT instr(x'0102030405',x'0405');}
} {4}
do_test instr-1.41 {
db eval {SELECT instr(x'0102030405',x'0506');}
} {0}
do_test instr-1.42 {
db eval {SELECT instr(x'0102030405',x'');}
} {1}
do_test instr-1.43 {
db eval {SELECT instr(x'',x'');}
} {1}
do_test instr-1.44 {
db eval {SELECT instr('','');}
} {1}
do_test instr-1.45 {
db eval {SELECT instr('abcdefg','');}
} {1}
unset -nocomplain longstr
set longstr abcdefghijklmonpqrstuvwxyz
append longstr $longstr
append longstr $longstr
append longstr $longstr
append longstr $longstr
append longstr $longstr
append longstr $longstr
append longstr $longstr
append longstr $longstr
append longstr $longstr
append longstr $longstr
append longstr $longstr
append longstr $longstr
# puts [string length $longstr]
append longstr Xabcde
do_test instr-1.46 {
db eval {SELECT instr($longstr,'X');}
} {106497}
do_test instr-1.47 {
db eval {SELECT instr($longstr,'Y');}
} {0}
do_test instr-1.48 {
db eval {SELECT instr($longstr,'Xa');}
} {106497}
do_test instr-1.49 {
db eval {SELECT instr($longstr,'zXa');}
} {106496}
set longstr [string map {a ä} $longstr]
do_test instr-1.50 {
db eval {SELECT instr($longstr,'X');}
} {106497}
do_test instr-1.51 {
db eval {SELECT instr($longstr,'Y');}
} {0}
do_test instr-1.52 {
db eval {SELECT instr($longstr,'Xä');}
} {106497}
do_test instr-1.53 {
db eval {SELECT instr($longstr,'zXä');}
} {106496}
do_test instr-1.54 {
db eval {SELECT instr(x'78c3a4e282ac79','x');}
} {1}
do_test instr-1.55 {
db eval {SELECT instr(x'78c3a4e282ac79','y');}
} {4}
do_test instr-1.56 {
db eval {SELECT instr(x'78c3a4e282ac79',x'79');}
} {7}
do_test instr-1.57 {
db eval {SELECT instr('xä€y',x'79');}
} {4}
finish_test