Index: src/func.c ================================================================== --- src/func.c +++ src/func.c @@ -165,10 +165,59 @@ sqlite3_result_double(context, rVal); break; } } } + +/* +** 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. ** ** substr(x,p1,p2) returns p2 characters of x[] beginning with p1. @@ -1534,10 +1583,11 @@ FUNCTION(max, -1, 1, 1, minmaxFunc ), FUNCTION(max, 0, 1, 1, 0 ), 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 ), #ifndef SQLITE_OMIT_FLOATING_POINT FUNCTION(round, 1, 0, 0, roundFunc ), ADDED test/instr.test Index: test/instr.test ================================================================== --- /dev/null +++ test/instr.test @@ -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