initial
[prop.git] / lib-src / strings / kr.cc
blob3114f9eae2f516f3bc2f22eae9d7ecabddfefb55
1 //////////////////////////////////////////////////////////////////////////////
2 // NOTICE:
3 //
4 // ADLib, Prop and their related set of tools and documentation are in the
5 // public domain. The author(s) of this software reserve no copyrights on
6 // the source code and any code generated using the tools. You are encouraged
7 // to use ADLib and Prop to develop software, in both academic and commercial
8 // settings, and are free to incorporate any part of ADLib and Prop into
9 // your programs.
11 // Although you are under no obligation to do so, we strongly recommend that
12 // you give away all software developed using our tools.
14 // We also ask that credit be given to us when ADLib and/or Prop are used in
15 // your programs, and that this notice be preserved intact in all the source
16 // code.
18 // This software is still under development and we welcome any suggestions
19 // and help from the users.
21 // Allen Leung
22 // 1994
23 //////////////////////////////////////////////////////////////////////////////
25 #include <stddef.h>
26 #include <string.h>
27 #include <AD/strings/kr.h>
29 void KR::compile (const char * pat, int len)
30 { if (len < 0) len = strlen(pat);
31 patternLength = len;
32 delete [] pattern;
33 pattern = new char [len];
34 memcpy(pattern,pat,len);
35 fingerPrint = 0;
36 for (register int i = len-1; i >= 0; i--) fingerPrint += pat[i];
39 int KR::Match(register const char * text, register int length) const
40 { if (length < 0) length = strlen(text);
41 if (length < patternLength) return -1;
42 register int i, j;
43 register unsigned int chi = 0;
44 for (i = 0; i < patternLength; i++) chi += text[i];
45 for (j = 0; ; i++, j++) {
46 if (chi == fingerPrint &&
47 memcmp(pattern,text+j,patternLength) == 0)
48 return j;
49 if (i >= length) break;
50 chi += text[i] - text[j];
52 return -1;