Hex Artifact Content
Not logged in

Artifact b8713502ffdcf144fa908edcdfe750173465a542:

File src/search.c part of check-in [232d10b736] - Begin adding a search capability; the code is not yet connected up. Also update to the latest version of SQLite. by drh on 2009-10-18 18:21:37.

0000: 2f 2a 0a 2a 2a 20 43 6f 70 79 72 69 67 68 74 20  /*.** Copyright 
0010: 28 63 29 20 32 30 30 39 20 44 2e 20 52 69 63 68  (c) 2009 D. Rich
0020: 61 72 64 20 48 69 70 70 0a 2a 2a 0a 2a 2a 20 54  ard Hipp.**.** T
0030: 68 69 73 20 70 72 6f 67 72 61 6d 20 69 73 20 66  his program is f
0040: 72 65 65 20 73 6f 66 74 77 61 72 65 3b 20 79 6f  ree software; yo
0050: 75 20 63 61 6e 20 72 65 64 69 73 74 72 69 62 75  u can redistribu
0060: 74 65 20 69 74 20 61 6e 64 2f 6f 72 0a 2a 2a 20  te it and/or.** 
0070: 6d 6f 64 69 66 79 20 69 74 20 75 6e 64 65 72 20  modify it under 
0080: 74 68 65 20 74 65 72 6d 73 20 6f 66 20 74 68 65  the terms of the
0090: 20 47 4e 55 20 47 65 6e 65 72 61 6c 20 50 75 62   GNU General Pub
00a0: 6c 69 63 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 76  lic.** License v
00b0: 65 72 73 69 6f 6e 20 32 20 61 73 20 70 75 62 6c  ersion 2 as publ
00c0: 69 73 68 65 64 20 62 79 20 74 68 65 20 46 72 65  ished by the Fre
00d0: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64  e Software Found
00e0: 61 74 69 6f 6e 2e 0a 2a 2a 0a 2a 2a 20 54 68 69  ation..**.** Thi
00f0: 73 20 70 72 6f 67 72 61 6d 20 69 73 20 64 69 73  s program is dis
0100: 74 72 69 62 75 74 65 64 20 69 6e 20 74 68 65 20  tributed in the 
0110: 68 6f 70 65 20 74 68 61 74 20 69 74 20 77 69 6c  hope that it wil
0120: 6c 20 62 65 20 75 73 65 66 75 6c 2c 0a 2a 2a 20  l be useful,.** 
0130: 62 75 74 20 57 49 54 48 4f 55 54 20 41 4e 59 20  but WITHOUT ANY 
0140: 57 41 52 52 41 4e 54 59 3b 20 77 69 74 68 6f 75  WARRANTY; withou
0150: 74 20 65 76 65 6e 20 74 68 65 20 69 6d 70 6c 69  t even the impli
0160: 65 64 20 77 61 72 72 61 6e 74 79 20 6f 66 0a 2a  ed warranty of.*
0170: 2a 20 4d 45 52 43 48 41 4e 54 41 42 49 4c 49 54  * MERCHANTABILIT
0180: 59 20 6f 72 20 46 49 54 4e 45 53 53 20 46 4f 52  Y or FITNESS FOR
0190: 20 41 20 50 41 52 54 49 43 55 4c 41 52 20 50 55   A PARTICULAR PU
01a0: 52 50 4f 53 45 2e 20 20 53 65 65 20 74 68 65 20  RPOSE.  See the 
01b0: 47 4e 55 0a 2a 2a 20 47 65 6e 65 72 61 6c 20 50  GNU.** General P
01c0: 75 62 6c 69 63 20 4c 69 63 65 6e 73 65 20 66 6f  ublic License fo
01d0: 72 20 6d 6f 72 65 20 64 65 74 61 69 6c 73 2e 0a  r more details..
01e0: 2a 2a 20 0a 2a 2a 20 59 6f 75 20 73 68 6f 75 6c  ** .** You shoul
01f0: 64 20 68 61 76 65 20 72 65 63 65 69 76 65 64 20  d have received 
0200: 61 20 63 6f 70 79 20 6f 66 20 74 68 65 20 47 4e  a copy of the GN
0210: 55 20 47 65 6e 65 72 61 6c 20 50 75 62 6c 69 63  U General Public
0220: 0a 2a 2a 20 4c 69 63 65 6e 73 65 20 61 6c 6f 6e  .** License alon
0230: 67 20 77 69 74 68 20 74 68 69 73 20 6c 69 62 72  g with this libr
0240: 61 72 79 3b 20 69 66 20 6e 6f 74 2c 20 77 72 69  ary; if not, wri
0250: 74 65 20 74 6f 20 74 68 65 0a 2a 2a 20 46 72 65  te to the.** Fre
0260: 65 20 53 6f 66 74 77 61 72 65 20 46 6f 75 6e 64  e Software Found
0270: 61 74 69 6f 6e 2c 20 49 6e 63 2e 2c 20 35 39 20  ation, Inc., 59 
0280: 54 65 6d 70 6c 65 20 50 6c 61 63 65 20 2d 20 53  Temple Place - S
0290: 75 69 74 65 20 33 33 30 2c 0a 2a 2a 20 42 6f 73  uite 330,.** Bos
02a0: 74 6f 6e 2c 20 4d 41 20 20 30 32 31 31 31 2d 31  ton, MA  02111-1
02b0: 33 30 37 2c 20 55 53 41 2e 0a 2a 2a 0a 2a 2a 20  307, USA..**.** 
02c0: 41 75 74 68 6f 72 20 63 6f 6e 74 61 63 74 20 69  Author contact i
02d0: 6e 66 6f 72 6d 61 74 69 6f 6e 3a 0a 2a 2a 20 20  nformation:.**  
02e0: 20 64 72 68 40 68 77 61 63 69 2e 63 6f 6d 0a 2a   drh@hwaci.com.*
02f0: 2a 20 20 20 68 74 74 70 3a 2f 2f 77 77 77 2e 68  *   http://www.h
0300: 77 61 63 69 2e 63 6f 6d 2f 64 72 68 2f 0a 2a 2a  waci.com/drh/.**
0310: 0a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  .***************
0320: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0330: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0340: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0350: 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a 2a  ****************
0360: 0a 2a 2a 0a 2a 2a 20 54 68 69 73 20 66 69 6c 65  .**.** This file
0370: 20 63 6f 6e 74 61 69 6e 73 20 63 6f 64 65 20 74   contains code t
0380: 6f 20 69 6d 70 6c 65 6d 65 6e 74 20 74 68 65 20  o implement the 
0390: 22 2f 64 6f 63 22 20 77 65 62 20 70 61 67 65 20  "/doc" web page 
03a0: 61 6e 64 20 72 65 6c 61 74 65 64 0a 2a 2a 20 70  and related.** p
03b0: 61 67 65 73 2e 0a 2a 2f 0a 23 69 6e 63 6c 75 64  ages..*/.#includ
03c0: 65 20 22 63 6f 6e 66 69 67 2e 68 22 0a 23 69 6e  e "config.h".#in
03d0: 63 6c 75 64 65 20 22 73 65 61 72 63 68 2e 68 22  clude "search.h"
03e0: 0a 23 69 6e 63 6c 75 64 65 20 3c 61 73 73 65 72  .#include <asser
03f0: 74 2e 68 3e 0a 0a 23 69 66 20 49 4e 54 45 52 46  t.h>..#if INTERF
0400: 41 43 45 0a 2f 2a 0a 2a 2a 20 41 20 63 6f 6d 70  ACE./*.** A comp
0410: 69 6c 65 64 20 73 65 61 72 63 68 20 70 61 74 74  iled search patt
0420: 65 72 0a 2a 2f 0a 73 74 72 75 63 74 20 53 65 61  er.*/.struct Sea
0430: 72 63 68 20 7b 0a 20 20 69 6e 74 20 6e 54 65 72  rch {.  int nTer
0440: 6d 3b 0a 20 20 73 74 72 75 63 74 20 73 72 63 68  m;.  struct srch
0450: 54 65 72 6d 20 7b 0a 20 20 20 20 63 68 61 72 20  Term {.    char 
0460: 2a 7a 3b 0a 20 20 20 20 69 6e 74 20 6e 3b 0a 20  *z;.    int n;. 
0470: 20 7d 20 61 5b 38 5d 3b 0a 7d 3b 0a 23 65 6e 64   } a[8];.};.#end
0480: 69 66 0a 0a 2f 2a 0a 2a 2a 20 43 6f 6d 70 69 6c  if../*.** Compil
0490: 65 20 61 20 73 65 61 72 63 68 20 70 61 74 74 65  e a search patte
04a0: 72 6e 0a 2a 2f 0a 53 65 61 72 63 68 20 2a 73 65  rn.*/.Search *se
04b0: 61 72 63 68 5f 69 6e 69 74 28 63 6f 6e 73 74 20  arch_init(const 
04c0: 63 68 61 72 20 2a 7a 50 61 74 74 65 72 6e 29 7b  char *zPattern){
04d0: 0a 20 20 69 6e 74 20 6e 50 61 74 74 65 72 6e 20  .  int nPattern 
04e0: 3d 20 73 74 72 6c 65 6e 28 7a 50 61 74 74 65 72  = strlen(zPatter
04f0: 6e 29 3b 0a 20 20 53 65 61 72 63 68 20 2a 70 3b  n);.  Search *p;
0500: 0a 20 20 63 68 61 72 20 2a 7a 3b 0a 20 20 69 6e  .  char *z;.  in
0510: 74 20 69 3b 0a 0a 20 20 70 20 3d 20 6d 61 6c 6c  t i;..  p = mall
0520: 6f 63 28 20 6e 50 61 74 74 65 72 6e 20 2b 20 73  oc( nPattern + s
0530: 69 7a 65 6f 66 28 2a 70 29 20 2b 20 31 29 3b 0a  izeof(*p) + 1);.
0540: 20 20 69 66 28 20 70 3d 3d 30 20 29 20 66 6f 73    if( p==0 ) fos
0550: 73 69 6c 5f 70 61 6e 69 63 28 22 6f 75 74 20 6f  sil_panic("out o
0560: 66 20 6d 65 6d 6f 72 79 22 29 3b 0a 20 20 7a 20  f memory");.  z 
0570: 3d 20 28 63 68 61 72 2a 29 26 70 5b 31 5d 3b 0a  = (char*)&p[1];.
0580: 20 20 73 74 72 63 70 79 28 7a 2c 20 7a 50 61 74    strcpy(z, zPat
0590: 74 65 72 6e 29 3b 0a 20 20 6d 65 6d 73 65 74 28  tern);.  memset(
05a0: 70 2c 20 30 2c 20 73 69 7a 65 6f 66 28 2a 70 29  p, 0, sizeof(*p)
05b0: 29 3b 0a 20 20 77 68 69 6c 65 28 20 2a 7a 20 26  );.  while( *z &
05c0: 26 20 70 2d 3e 6e 54 65 72 6d 3c 73 69 7a 65 6f  & p->nTerm<sizeo
05d0: 66 28 70 2d 3e 61 29 2f 73 69 7a 65 6f 66 28 70  f(p->a)/sizeof(p
05e0: 2d 3e 61 5b 30 5d 29 20 29 7b 0a 20 20 20 20 77  ->a[0]) ){.    w
05f0: 68 69 6c 65 28 20 21 69 73 61 6c 6e 75 6d 28 2a  hile( !isalnum(*
0600: 7a 29 20 26 26 20 2a 7a 20 29 7b 20 7a 2b 2b 3b  z) && *z ){ z++;
0610: 20 7d 0a 20 20 20 20 69 66 28 20 2a 7a 3d 3d 30   }.    if( *z==0
0620: 20 29 20 62 72 65 61 6b 3b 0a 20 20 20 20 70 2d   ) break;.    p-
0630: 3e 61 5b 70 2d 3e 6e 54 65 72 6d 5d 2e 7a 20 3d  >a[p->nTerm].z =
0640: 20 7a 3b 0a 20 20 20 20 66 6f 72 28 69 3d 31 3b   z;.    for(i=1;
0650: 20 69 73 61 6c 6e 75 6d 28 7a 5b 69 5d 29 20 7c   isalnum(z[i]) |
0660: 7c 20 7a 5b 69 5d 3d 3d 27 5f 27 3b 20 69 2b 2b  | z[i]=='_'; i++
0670: 29 7b 7d 0a 20 20 20 20 70 2d 3e 61 5b 70 2d 3e  ){}.    p->a[p->
0680: 6e 54 65 72 6d 5d 2e 6e 20 3d 20 69 3b 0a 20 20  nTerm].n = i;.  
0690: 20 20 7a 20 2b 3d 20 69 3b 0a 20 20 20 20 70 2d    z += i;.    p-
06a0: 3e 6e 54 65 72 6d 2b 2b 3b 0a 20 20 7d 0a 20 20  >nTerm++;.  }.  
06b0: 72 65 74 75 72 6e 20 70 3b 0a 7d 0a 0a 0a 2f 2a  return p;.}.../*
06c0: 0a 2a 2a 20 44 65 73 74 72 6f 79 20 61 20 73 65  .** Destroy a se
06d0: 61 72 63 68 20 63 6f 6e 74 65 78 74 2e 0a 2a 2f  arch context..*/
06e0: 0a 76 6f 69 64 20 73 65 61 72 63 68 5f 65 6e 64  .void search_end
06f0: 28 53 65 61 72 63 68 20 2a 70 29 7b 0a 20 20 66  (Search *p){.  f
0700: 72 65 65 28 70 29 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a  ree(p);.}../*.**
0710: 20 54 68 65 73 65 73 20 63 68 61 72 61 63 74 65   Theses characte
0720: 72 73 20 63 6f 6e 73 74 69 74 75 74 65 20 61 20  rs constitute a 
0730: 77 6f 72 64 20 62 6f 75 6e 64 61 72 79 0a 2a 2f  word boundary.*/
0740: 0a 73 74 61 74 69 63 20 63 6f 6e 73 74 20 63 68  .static const ch
0750: 61 72 20 69 73 42 6f 75 6e 64 61 72 79 5b 5d 20  ar isBoundary[] 
0760: 3d 20 7b 0a 20 20 31 2c 20 31 2c 20 31 2c 20 31  = {.  1, 1, 1, 1
0770: 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 20 20  , 1, 1, 1, 1,   
0780: 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    1, 1, 1, 1, 1,
0790: 20 31 2c 20 31 2c 20 31 2c 0a 20 20 31 2c 20 31   1, 1, 1,.  1, 1
07a0: 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 1, 1, 1, 1, 1,
07b0: 20 31 2c 20 20 20 20 20 31 2c 20 31 2c 20 31 2c   1,     1, 1, 1,
07c0: 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 0a   1, 1, 1, 1, 1,.
07d0: 20 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c    1, 1, 1, 1, 1,
07e0: 20 31 2c 20 31 2c 20 31 2c 20 20 20 20 20 31 2c   1, 1, 1,     1,
07f0: 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c 20   1, 1, 1, 1, 1, 
0800: 31 2c 20 31 2c 0a 20 20 30 2c 20 30 2c 20 30 2c  1, 1,.  0, 0, 0,
0810: 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
0820: 20 20 20 20 30 2c 20 30 2c 20 31 2c 20 31 2c 20      0, 0, 1, 1, 
0830: 31 2c 20 31 2c 20 31 2c 20 31 2c 0a 20 20 31 2c  1, 1, 1, 1,.  1,
0840: 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
0850: 30 2c 20 30 2c 20 20 20 20 20 30 2c 20 30 2c 20  0, 0,     0, 0, 
0860: 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
0870: 2c 0a 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20  ,.  0, 0, 0, 0, 
0880: 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 20 20 20  0, 0, 0, 0,     
0890: 30 2c 20 30 2c 20 30 2c 20 31 2c 20 31 2c 20 31  0, 0, 0, 1, 1, 1
08a0: 2c 20 31 2c 20 30 2c 0a 20 20 31 2c 20 30 2c 20  , 1, 0,.  1, 0, 
08b0: 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
08c0: 2c 20 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30  ,     0, 0, 0, 0
08d0: 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20  , 0, 0, 0, 0,.  
08e0: 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
08f0: 2c 20 30 2c 20 30 2c 20 20 20 20 20 30 2c 20 30  , 0, 0,     0, 0
0900: 2c 20 30 2c 20 31 2c 20 31 2c 20 31 2c 20 31 2c  , 0, 1, 1, 1, 1,
0910: 20 31 2c 0a 20 20 30 2c 20 30 2c 20 30 2c 20 30   1,.  0, 0, 0, 0
0920: 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 20  , 0, 0, 0, 0,   
0930: 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c    0, 0, 0, 0, 0,
0940: 20 30 2c 20 30 2c 20 30 2c 0a 20 20 30 2c 20 30   0, 0, 0,.  0, 0
0950: 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
0960: 20 30 2c 20 20 20 20 20 30 2c 20 30 2c 20 30 2c   0,     0, 0, 0,
0970: 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a   0, 0, 0, 0, 0,.
0980: 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c    0, 0, 0, 0, 0,
0990: 20 30 2c 20 30 2c 20 30 2c 20 20 20 20 20 30 2c   0, 0, 0,     0,
09a0: 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
09b0: 30 2c 20 30 2c 0a 20 20 30 2c 20 30 2c 20 30 2c  0, 0,.  0, 0, 0,
09c0: 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
09d0: 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20      0, 0, 0, 0, 
09e0: 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20 30 2c  0, 0, 0, 0,.  0,
09f0: 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20   0, 0, 0, 0, 0, 
0a00: 30 2c 20 30 2c 20 20 20 20 20 30 2c 20 30 2c 20  0, 0,     0, 0, 
0a10: 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
0a20: 2c 0a 20 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20  ,.  0, 0, 0, 0, 
0a30: 30 2c 20 30 2c 20 30 2c 20 30 2c 20 20 20 20 20  0, 0, 0, 0,     
0a40: 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
0a50: 2c 20 30 2c 20 30 2c 0a 20 20 30 2c 20 30 2c 20  , 0, 0,.  0, 0, 
0a60: 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
0a70: 2c 20 20 20 20 20 30 2c 20 30 2c 20 30 2c 20 30  ,     0, 0, 0, 0
0a80: 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 0a 20 20  , 0, 0, 0, 0,.  
0a90: 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30  0, 0, 0, 0, 0, 0
0aa0: 2c 20 30 2c 20 30 2c 20 20 20 20 20 30 2c 20 30  , 0, 0,     0, 0
0ab0: 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c 20 30 2c  , 0, 0, 0, 0, 0,
0ac0: 20 30 2c 0a 7d 3b 0a 0a 2f 2a 0a 2a 2a 20 43 6f   0,.};../*.** Co
0ad0: 6d 70 61 72 65 20 61 20 73 65 61 72 63 68 20 70  mpare a search p
0ae0: 61 74 74 65 72 6e 20 61 67 61 69 6e 73 74 20 61  attern against a
0af0: 6e 20 69 6e 70 75 74 20 73 74 72 69 6e 67 20 61  n input string a
0b00: 6e 64 20 72 65 74 75 72 6e 20 61 20 73 63 6f 72  nd return a scor
0b10: 65 2e 0a 2a 2a 0a 2a 2a 20 53 63 6f 72 69 6e 67  e..**.** Scoring
0b20: 3a 0a 2a 2a 20 20 20 2a 20 20 41 6c 6c 20 74 65  :.**   *  All te
0b30: 72 6d 73 20 6d 75 73 74 20 6d 61 74 63 68 20 61  rms must match a
0b40: 74 20 6c 65 61 73 74 20 6f 6e 63 65 20 6f 72 20  t least once or 
0b50: 74 68 65 20 73 63 6f 72 65 20 69 73 20 7a 65 72  the score is zer
0b60: 6f 0a 2a 2a 20 20 20 2a 20 20 31 30 20 62 6f 6e  o.**   *  10 bon
0b70: 75 73 20 70 6f 69 6e 74 73 20 69 66 20 74 68 65  us points if the
0b80: 20 66 69 72 73 74 20 6f 63 63 75 72 72 61 6e 63   first occurranc
0b90: 65 20 69 73 20 61 6e 20 65 78 61 63 74 20 6d 61  e is an exact ma
0ba0: 74 63 68 0a 2a 2a 20 20 20 2a 20 20 31 20 61 64  tch.**   *  1 ad
0bb0: 64 69 74 69 6f 6e 61 6c 20 70 6f 69 6e 74 20 66  ditional point f
0bc0: 6f 72 20 65 61 63 68 20 73 75 62 73 65 71 75 65  or each subseque
0bd0: 6e 74 20 6d 61 74 63 68 20 6f 66 20 74 68 65 20  nt match of the 
0be0: 73 61 6d 65 20 77 6f 72 64 0a 2a 2a 20 20 20 2a  same word.**   *
0bf0: 20 20 45 78 74 72 61 20 70 6f 69 6e 74 73 20 6f    Extra points o
0c00: 66 20 74 77 6f 20 63 6f 6e 73 65 63 75 74 69 76  f two consecutiv
0c10: 65 20 77 6f 72 64 73 20 6f 66 20 74 68 65 20 70  e words of the p
0c20: 61 74 74 65 72 6e 20 61 72 65 20 63 6f 6e 73 65  attern are conse
0c30: 63 75 74 69 76 65 0a 2a 2a 20 20 20 20 20 20 69  cutive.**      i
0c40: 6e 20 74 68 65 20 64 6f 63 75 6d 65 6e 74 0a 2a  n the document.*
0c50: 2f 0a 69 6e 74 20 73 65 61 72 63 68 5f 73 63 6f  /.int search_sco
0c60: 72 65 28 53 65 61 72 63 68 20 2a 70 2c 20 63 6f  re(Search *p, co
0c70: 6e 73 74 20 63 68 61 72 20 2a 7a 44 6f 63 29 7b  nst char *zDoc){
0c80: 0a 20 20 69 6e 74 20 69 50 72 65 76 20 3d 20 39  .  int iPrev = 9
0c90: 39 39 3b 0a 20 20 69 6e 74 20 73 63 6f 72 65 20  99;.  int score 
0ca0: 3d 20 31 30 3b 0a 20 20 69 6e 74 20 69 42 6f 6e  = 10;.  int iBon
0cb0: 75 73 20 3d 20 30 3b 0a 20 20 69 6e 74 20 69 2c  us = 0;.  int i,
0cc0: 20 6a 3b 0a 20 20 75 6e 73 69 67 6e 65 64 20 63   j;.  unsigned c
0cd0: 68 61 72 20 73 65 65 6e 5b 38 5d 3b 0a 0a 20 20  har seen[8];..  
0ce0: 6d 65 6d 73 65 74 28 73 65 65 6e 2c 20 30 2c 20  memset(seen, 0, 
0cf0: 73 69 7a 65 6f 66 28 73 65 65 6e 29 29 3b 0a 20  sizeof(seen));. 
0d00: 20 66 6f 72 28 69 3d 30 3b 20 7a 44 6f 63 5b 69   for(i=0; zDoc[i
0d10: 5d 3b 20 69 2b 2b 29 7b 0a 20 20 20 20 63 68 61  ]; i++){.    cha
0d20: 72 20 63 20 3d 20 7a 44 6f 63 5b 69 5d 3b 0a 20  r c = zDoc[i];. 
0d30: 20 20 20 69 66 28 20 69 73 42 6f 75 6e 64 61 72     if( isBoundar
0d40: 79 5b 63 26 30 78 66 66 5d 20 29 20 63 6f 6e 74  y[c&0xff] ) cont
0d50: 69 6e 75 65 3b 0a 20 20 20 20 66 6f 72 28 6a 3d  inue;.    for(j=
0d60: 30 3b 20 6a 3c 70 2d 3e 6e 54 65 72 6d 3b 20 6a  0; j<p->nTerm; j
0d70: 2b 2b 29 7b 0a 20 20 20 20 20 20 69 6e 74 20 6e  ++){.      int n
0d80: 20 3d 20 70 2d 3e 61 5b 6a 5d 2e 6e 3b 0a 20 20   = p->a[j].n;.  
0d90: 20 20 20 20 69 66 28 20 73 71 6c 69 74 65 33 5f      if( sqlite3_
0da0: 73 74 72 6e 69 63 6d 70 28 70 2d 3e 61 5b 6a 5d  strnicmp(p->a[j]
0db0: 2e 7a 2c 20 26 7a 44 6f 63 5b 69 5d 2c 20 6e 29  .z, &zDoc[i], n)
0dc0: 3d 3d 30 20 29 7b 0a 20 20 20 20 20 20 20 20 73  ==0 ){.        s
0dd0: 63 6f 72 65 20 2b 3d 20 31 3b 0a 20 20 20 20 20  core += 1;.     
0de0: 20 20 20 69 66 28 20 21 73 65 65 6e 5b 6a 5d 20     if( !seen[j] 
0df0: 29 7b 0a 20 20 20 20 20 20 20 20 20 20 69 66 28  ){.          if(
0e00: 20 69 73 42 6f 75 6e 64 61 72 79 5b 7a 44 6f 63   isBoundary[zDoc
0e10: 5b 69 2b 6e 5d 26 30 78 66 66 5d 20 29 20 73 63  [i+n]&0xff] ) sc
0e20: 6f 72 65 20 2b 3d 20 31 30 3b 0a 20 20 20 20 20  ore += 10;.     
0e30: 20 20 20 20 20 73 65 65 6e 5b 6a 5d 20 3d 20 31       seen[j] = 1
0e40: 3b 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20  ;.        }.    
0e50: 20 20 20 20 69 66 28 20 6a 3d 3d 69 50 72 65 76      if( j==iPrev
0e60: 2b 31 20 29 7b 0a 20 20 20 20 20 20 20 20 20 20  +1 ){.          
0e70: 73 63 6f 72 65 20 2b 3d 20 69 42 6f 6e 75 73 3b  score += iBonus;
0e80: 0a 20 20 20 20 20 20 20 20 7d 0a 20 20 20 20 20  .        }.     
0e90: 20 20 20 69 20 2b 3d 20 6e 2d 31 3b 0a 20 20 20     i += n-1;.   
0ea0: 20 20 20 20 20 69 50 72 65 76 20 3d 20 6a 3b 0a       iPrev = j;.
0eb0: 20 20 20 20 20 20 20 20 69 42 6f 6e 75 73 20 3d          iBonus =
0ec0: 20 35 30 3b 0a 20 20 20 20 20 20 20 20 62 72 65   50;.        bre
0ed0: 61 6b 3b 0a 20 20 20 20 20 20 7d 0a 20 20 20 20  ak;.      }.    
0ee0: 7d 0a 20 20 20 20 69 42 6f 6e 75 73 20 2f 3d 20  }.    iBonus /= 
0ef0: 32 3b 0a 20 20 20 20 77 68 69 6c 65 28 20 21 69  2;.    while( !i
0f00: 73 42 6f 75 6e 64 61 72 79 5b 7a 44 6f 63 5b 69  sBoundary[zDoc[i
0f10: 5d 26 30 78 66 66 5d 20 29 7b 20 69 2b 2b 3b 20  ]&0xff] ){ i++; 
0f20: 7d 0a 20 20 7d 0a 0a 20 20 2f 2a 20 45 76 65 72  }.  }..  /* Ever
0f30: 79 20 74 65 72 6d 20 6d 75 73 74 20 62 65 20 73  y term must be s
0f40: 65 65 6e 20 6f 72 20 65 6c 73 65 20 74 68 65 20  een or else the 
0f50: 73 63 6f 72 65 20 69 73 20 7a 65 72 6f 20 2a 2f  score is zero */
0f60: 0a 20 20 66 6f 72 28 6a 3d 30 3b 20 6a 3c 70 2d  .  for(j=0; j<p-
0f70: 3e 6e 54 65 72 6d 3b 20 6a 2b 2b 29 7b 0a 20 20  >nTerm; j++){.  
0f80: 20 20 69 66 28 20 21 73 65 65 6e 5b 6a 5d 20 29    if( !seen[j] )
0f90: 20 72 65 74 75 72 6e 20 30 3b 0a 20 20 7d 0a 20   return 0;.  }. 
0fa0: 20 20 20 20 20 0a 20 20 72 65 74 75 72 6e 20 73       .  return s
0fb0: 63 6f 72 65 3b 0a 7d 0a 0a 2f 2a 0a 2a 2a 20 54  core;.}../*.** T
0fc0: 68 69 73 20 69 73 20 61 6e 20 53 51 4c 69 74 65  his is an SQLite
0fd0: 20 66 75 6e 63 74 69 6f 6e 20 74 68 61 74 20 73   function that s
0fe0: 63 6f 72 65 73 20 69 74 73 20 69 6e 70 75 74 20  cores its input 
0ff0: 75 73 69 6e 67 0a 2a 2a 20 61 20 70 72 65 2d 63  using.** a pre-c
1000: 6f 6d 70 75 74 65 64 20 70 61 74 74 65 72 6e 2e  omputed pattern.
1010: 0a 2a 2f 0a 73 74 61 74 69 63 20 76 6f 69 64 20  .*/.static void 
1020: 73 65 61 72 63 68 5f 73 63 6f 72 65 5f 73 71 6c  search_score_sql
1030: 66 75 6e 63 28 0a 20 20 73 71 6c 69 74 65 33 5f  func(.  sqlite3_
1040: 63 6f 6e 74 65 78 74 20 2a 63 6f 6e 74 65 78 74  context *context
1050: 2c 0a 20 20 69 6e 74 20 61 72 67 63 2c 0a 20 20  ,.  int argc,.  
1060: 73 71 6c 69 74 65 33 5f 76 61 6c 75 65 20 2a 2a  sqlite3_value **
1070: 61 72 67 76 0a 29 7b 0a 20 20 53 65 61 72 63 68  argv.){.  Search
1080: 20 2a 70 20 3d 20 28 53 65 61 72 63 68 2a 29 73   *p = (Search*)s
1090: 71 6c 69 74 65 33 5f 75 73 65 72 5f 64 61 74 61  qlite3_user_data
10a0: 28 63 6f 6e 74 65 78 74 29 3b 0a 20 20 69 6e 74  (context);.  int
10b0: 20 73 63 6f 72 65 20 3d 20 73 65 61 72 63 68 5f   score = search_
10c0: 73 63 6f 72 65 28 70 2c 20 28 63 6f 6e 73 74 20  score(p, (const 
10d0: 63 68 61 72 2a 29 73 71 6c 69 74 65 33 5f 76 61  char*)sqlite3_va
10e0: 6c 75 65 5f 74 65 78 74 28 61 72 67 76 5b 30 5d  lue_text(argv[0]
10f0: 29 29 3b 0a 20 20 73 71 6c 69 74 65 33 5f 72 65  ));.  sqlite3_re
1100: 73 75 6c 74 5f 69 6e 74 28 63 6f 6e 74 65 78 74  sult_int(context
1110: 2c 20 73 63 6f 72 65 29 3b 0a 7d 0a 0a 2f 2a 0a  , score);.}../*.
1120: 2a 2a 20 52 65 67 69 73 74 65 72 20 74 68 65 20  ** Register the 
1130: 22 73 63 6f 72 65 28 29 22 20 53 51 4c 20 66 75  "score()" SQL fu
1140: 6e 63 74 69 6f 6e 20 74 6f 20 73 63 6f 72 65 20  nction to score 
1150: 69 74 73 20 69 6e 70 75 74 20 74 65 78 74 0a 2a  its input text.*
1160: 2a 20 75 73 69 6e 67 20 74 68 65 20 67 69 76 65  * using the give
1170: 6e 20 53 65 61 72 63 68 20 6f 62 6a 65 63 74 2e  n Search object.
1180: 20 20 4f 6e 63 65 20 74 68 69 73 20 66 75 6e 63    Once this func
1190: 74 69 6f 6e 20 69 73 20 72 65 67 69 73 74 65 72  tion is register
11a0: 65 64 2c 0a 2a 2a 20 64 6f 20 6e 6f 74 20 64 65  ed,.** do not de
11b0: 6c 65 74 65 20 74 68 65 20 53 65 61 72 63 68 20  lete the Search 
11c0: 6f 62 6a 65 63 74 2e 0a 2a 2f 0a 76 6f 69 64 20  object..*/.void 
11d0: 73 65 61 72 63 68 5f 73 71 6c 5f 73 65 74 75 70  search_sql_setup
11e0: 28 53 65 61 72 63 68 20 2a 70 29 7b 0a 20 20 73  (Search *p){.  s
11f0: 71 6c 69 74 65 33 5f 63 72 65 61 74 65 5f 66 75  qlite3_create_fu
1200: 6e 63 74 69 6f 6e 28 67 2e 64 62 2c 20 22 73 63  nction(g.db, "sc
1210: 6f 72 65 22 2c 20 31 2c 20 53 51 4c 49 54 45 5f  ore", 1, SQLITE_
1220: 55 54 46 38 2c 20 70 2c 0a 20 20 20 20 20 73 65  UTF8, p,.     se
1230: 61 72 63 68 5f 73 63 6f 72 65 5f 73 71 6c 66 75  arch_score_sqlfu
1240: 6e 63 2c 20 30 2c 20 30 29 3b 0a 7d 0a 0a 2f 2a  nc, 0, 0);.}../*
1250: 0a 2a 2a 20 54 65 73 74 69 6e 67 20 74 68 65 20  .** Testing the 
1260: 73 65 61 72 63 68 20 66 75 6e 63 74 69 6f 6e 2e  search function.
1270: 0a 2a 2a 0a 2a 2a 20 43 4f 4d 4d 41 4e 44 3a 20  .**.** COMMAND: 
1280: 74 65 73 74 2d 73 65 61 72 63 68 0a 2a 2a 20 25  test-search.** %
1290: 66 6f 73 73 69 6c 20 74 65 73 74 2d 73 65 61 72  fossil test-sear
12a0: 63 68 20 70 61 74 74 65 72 6e 2e 2e 2e 0a 2a 2a  ch pattern....**
12b0: 0a 2a 2a 20 73 65 61 72 63 68 20 66 6f 72 20 63  .** search for c
12c0: 68 65 63 6b 2d 69 6e 73 20 6d 61 74 63 68 69 6e  heck-ins matchin
12d0: 67 20 74 68 65 20 70 61 74 74 65 72 6e 2e 0a 2a  g the pattern..*
12e0: 2f 0a 76 6f 69 64 20 73 65 61 72 63 68 5f 74 65  /.void search_te
12f0: 73 74 28 76 6f 69 64 29 7b 0a 20 20 53 65 61 72  st(void){.  Sear
1300: 63 68 20 2a 70 3b 0a 20 20 42 6c 6f 62 20 70 61  ch *p;.  Blob pa
1310: 74 74 65 72 6e 3b 0a 20 20 69 6e 74 20 69 3b 0a  ttern;.  int i;.
1320: 20 20 53 74 6d 74 20 71 3b 0a 0a 20 20 64 62 5f    Stmt q;..  db_
1330: 6d 75 73 74 5f 62 65 5f 77 69 74 68 69 6e 5f 74  must_be_within_t
1340: 72 65 65 28 29 3b 0a 20 20 69 66 28 20 67 2e 61  ree();.  if( g.a
1350: 72 67 63 3c 32 20 29 20 72 65 74 75 72 6e 3b 0a  rgc<2 ) return;.
1360: 20 20 62 6c 6f 62 5f 69 6e 69 74 28 26 70 61 74    blob_init(&pat
1370: 74 65 72 6e 2c 20 67 2e 61 72 67 76 5b 32 5d 2c  tern, g.argv[2],
1380: 20 2d 31 29 3b 0a 20 20 66 6f 72 28 69 3d 33 3b   -1);.  for(i=3;
1390: 20 69 3c 67 2e 61 72 67 63 3b 20 69 2b 2b 29 7b   i<g.argc; i++){
13a0: 0a 20 20 20 20 62 6c 6f 62 5f 61 70 70 65 6e 64  .    blob_append
13b0: 66 28 26 70 61 74 74 65 72 6e 2c 20 22 20 25 73  f(&pattern, " %s
13c0: 22 2c 20 67 2e 61 72 67 76 5b 69 5d 29 3b 0a 20  ", g.argv[i]);. 
13d0: 20 7d 0a 20 20 70 20 3d 20 73 65 61 72 63 68 5f   }.  p = search_
13e0: 69 6e 69 74 28 62 6c 6f 62 5f 73 74 72 28 26 70  init(blob_str(&p
13f0: 61 74 74 65 72 6e 29 29 3b 0a 20 20 62 6c 6f 62  attern));.  blob
1400: 5f 72 65 73 65 74 28 26 70 61 74 74 65 72 6e 29  _reset(&pattern)
1410: 3b 0a 20 20 73 65 61 72 63 68 5f 73 71 6c 5f 73  ;.  search_sql_s
1420: 65 74 75 70 28 70 29 3b 0a 0a 20 20 64 62 5f 6d  etup(p);..  db_m
1430: 75 6c 74 69 5f 65 78 65 63 28 0a 20 20 20 20 20  ulti_exec(.     
1440: 22 43 52 45 41 54 45 20 54 45 4d 50 20 54 41 42  "CREATE TEMP TAB
1450: 4c 45 20 73 72 63 68 28 78 2c 74 65 78 74 29 3b  LE srch(x,text);
1460: 22 0a 20 20 20 20 20 22 49 4e 53 45 52 54 20 49  ".     "INSERT I
1470: 4e 54 4f 20 73 72 63 68 28 74 65 78 74 29 20 53  NTO srch(text) S
1480: 45 4c 45 43 54 20 63 6f 61 6c 65 73 63 65 28 65  ELECT coalesce(e
1490: 63 6f 6d 6d 65 6e 74 2c 63 6f 6d 6d 65 6e 74 29  comment,comment)
14a0: 20 46 52 4f 4d 20 65 76 65 6e 74 3b 22 0a 20 20   FROM event;".  
14b0: 20 20 20 22 55 50 44 41 54 45 20 73 72 63 68 20     "UPDATE srch 
14c0: 53 45 54 20 78 3d 73 63 6f 72 65 28 74 65 78 74  SET x=score(text
14d0: 29 3b 22 0a 20 20 29 3b 0a 20 20 64 62 5f 70 72  );".  );.  db_pr
14e0: 65 70 61 72 65 28 26 71 2c 20 22 53 45 4c 45 43  epare(&q, "SELEC
14f0: 54 20 78 2c 20 74 65 78 74 20 46 52 4f 4d 20 73  T x, text FROM s
1500: 72 63 68 20 57 48 45 52 45 20 78 3e 30 20 4f 52  rch WHERE x>0 OR
1510: 44 45 52 20 42 59 20 78 20 44 45 53 43 22 29 3b  DER BY x DESC");
1520: 0a 20 20 77 68 69 6c 65 28 20 64 62 5f 73 74 65  .  while( db_ste
1530: 70 28 26 71 29 3d 3d 53 51 4c 49 54 45 5f 52 4f  p(&q)==SQLITE_RO
1540: 57 20 29 7b 0a 20 20 20 20 69 6e 74 20 73 63 6f  W ){.    int sco
1550: 72 65 20 3d 20 64 62 5f 63 6f 6c 75 6d 6e 5f 69  re = db_column_i
1560: 6e 74 28 26 71 2c 20 30 29 3b 0a 20 20 20 20 63  nt(&q, 0);.    c
1570: 6f 6e 73 74 20 63 68 61 72 20 2a 7a 20 3d 20 64  onst char *z = d
1580: 62 5f 63 6f 6c 75 6d 6e 5f 74 65 78 74 28 26 71  b_column_text(&q
1590: 2c 20 31 29 3b 0a 0a 20 20 20 20 73 63 6f 72 65  , 1);..    score
15a0: 20 3d 20 73 65 61 72 63 68 5f 73 63 6f 72 65 28   = search_score(
15b0: 70 2c 20 7a 29 3b 0a 20 20 20 20 69 66 28 20 73  p, z);.    if( s
15c0: 63 6f 72 65 20 29 7b 0a 20 20 20 20 20 20 70 72  core ){.      pr
15d0: 69 6e 74 66 28 22 25 35 64 3a 20 25 73 5c 6e 22  intf("%5d: %s\n"
15e0: 2c 20 73 63 6f 72 65 2c 20 7a 29 3b 0a 20 20 20  , score, z);.   
15f0: 20 7d 0a 20 20 7d 0a 20 20 64 62 5f 66 69 6e 61   }.  }.  db_fina
1600: 6c 69 7a 65 28 26 71 29 3b 0a 7d 0a              lize(&q);.}.