SQLite
Check-in [49ccae964f]
Not logged in
Overview
SHA1 Hash:49ccae964f3a8ae5aab87f56503121e09424545f
Date: 2012-10-25 03:07:29
User: drh
Comment:Implementation of the INSTR() SQL function, as found in SQL Server, MySQL, and Oracle.
Tags And Properties
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/func.c

163
164
165
166
167
168
169

















































170
171
172
173
174
175
176
....
1532
1533
1534
1535
1536
1537
1538

1539
1540
1541
1542
1543
1544
1545
      double rVal = sqlite3_value_double(argv[0]);
      if( rVal<0 ) rVal = -rVal;
      sqlite3_result_double(context, rVal);
      break;
    }
  }
}


















































/*
** Implementation of the substr() function.
**
** substr(x,p1,p2)  returns p2 characters of x[] beginning with p1.
** p1 is 1-indexed.  So substr(x,1,1) returns the first character
** of x.  If x is text, then we actually count UTF-8 characters.
................................................................................
    FUNCTION(min,                0, 0, 1, 0                ),
    AGGREGATE(min,               1, 0, 1, minmaxStep,      minMaxFinalize ),
    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(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        ),
    FUNCTION(round,              2, 0, 0, roundFunc        ),
#endif







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>







163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
....
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
      double rVal = sqlite3_value_double(argv[0]);
      if( rVal<0 ) rVal = -rVal;
      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.
** p1 is 1-indexed.  So substr(x,1,1) returns the first character
** of x.  If x is text, then we actually count UTF-8 characters.
................................................................................
    FUNCTION(min,                0, 0, 1, 0                ),
    AGGREGATE(min,               1, 0, 1, minmaxStep,      minMaxFinalize ),
    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        ),
    FUNCTION(round,              2, 0, 0, roundFunc        ),
#endif

Added test/instr.test





































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
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