Mercurial > ~dholland > hg > ag > index.cgi
comparison anagram/agcore/search.cpp @ 0:13d2b8934445
Import AnaGram (near-)release tree into Mercurial.
author | David A. Holland |
---|---|
date | Sat, 22 Dec 2007 17:52:45 -0500 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:13d2b8934445 |
---|---|
1 /* | |
2 * AnaGram, A System for Syntax Directed Programming | |
3 * Copyright 1993-2002 Parsifal Software. All Rights Reserved. | |
4 * See the file COPYING for license and usage terms. | |
5 * | |
6 * search.cpp | |
7 */ | |
8 | |
9 #include <ctype.h> | |
10 #include "port.h" | |
11 | |
12 #include "arrays.h" | |
13 #include "search.h" | |
14 | |
15 //#define INCLUDE_LOGGING | |
16 #include "log.h" | |
17 | |
18 | |
19 SearchProcess::SearchProcess() | |
20 : keyLength(0) | |
21 , ptr(0) | |
22 , currentMode(notSet) | |
23 { | |
24 LOGSECTION("SearchProcess::SearchProcess"); | |
25 } | |
26 | |
27 void SearchProcess::setKey(AgString k) { | |
28 LOGSECTION("SearchProcess::setKey"); | |
29 key = k; | |
30 ptr = key.pointer(); | |
31 keyLength = key.size(); | |
32 LOGV(key) LCV(ptr) LCV(keyLength); | |
33 memset(offset, 0, 256); | |
34 currentMode = notSet; | |
35 int i; | |
36 for (i = 0; i < keyLength; i++) { | |
37 offset[(unsigned) key[i]] = (unsigned char)(i+1); | |
38 if (isalpha(key[i])) { | |
39 offset[(unsigned)key[i]^0x20] = (unsigned char) (i+1); | |
40 } | |
41 LOGV(i) LV(key[i]); | |
42 LOGV(key[i]) LCV(offset[(unsigned) key[i]]); | |
43 LOGV(key[i]^0x20) LCV(offset[(unsigned)key[i]^0x20]); | |
44 } | |
45 } | |
46 | |
47 char *SearchProcess::scanForward(char *line, int n) { | |
48 LOGSECTION("SearchProcess::scanForward"); | |
49 if (currentMode != forward) { | |
50 int i; | |
51 for (i = 0; i < keyLength; i++) { | |
52 offset[(unsigned)key[i]] = (unsigned char) (i+1); | |
53 if (isalpha((unsigned) key[i])) { | |
54 offset[(unsigned)key[i]^0x20] = (unsigned char) (i+1); | |
55 } | |
56 } | |
57 currentMode = forward; | |
58 } | |
59 //char *logBuf = new char[keyLength + 1]; | |
60 //logBuf[keyLength] = 0; | |
61 char *p = line + keyLength - 1; | |
62 char *end = line + n; | |
63 LOGV((int) p) LCV((int) end); | |
64 while (p < end) { | |
65 int k = offset[*(unsigned char *) p]; | |
66 if (k) { | |
67 p -= k - 1; | |
68 //strncpy(logBuf, p, keyLength); | |
69 //LOGV((int) p) LCV(*(unsigned char *)p) LCV(k) LCV(logBuf); | |
70 k = offset[*(unsigned char *) p]; | |
71 if (k && strnicmp(p, ptr, keyLength) == 0) { | |
72 return p; | |
73 } | |
74 } | |
75 p += keyLength; | |
76 } | |
77 //delete [] logBuf; | |
78 return 0; | |
79 } | |
80 | |
81 char *SearchProcess::scanReverse(char *line, int n) { | |
82 LOGSECTION("SearchProcess::scanReverse"); | |
83 if (currentMode != reverse) { | |
84 int i = keyLength; | |
85 while (i--) { | |
86 offset[(unsigned) key[i]] = (unsigned char) (i+1); | |
87 if (isalpha((unsigned) key[i])) { | |
88 offset[(unsigned)key[i]^0x20] = (unsigned char) (i+1); | |
89 } | |
90 } | |
91 currentMode = reverse; | |
92 } | |
93 //char *logBuf = new char[keyLength + 1]; | |
94 //char *logBuf = local_array(keyLength + 1, char); | |
95 LocalArray<char> logBuf(keyLength + 1); | |
96 logBuf[keyLength] = 0; | |
97 char *p = line + n - keyLength; | |
98 LOGV((int) line) LCV((int) p); | |
99 while (line <= p) { | |
100 int k = offset[*(unsigned char *)p]; | |
101 if (k) { | |
102 p -= k - 1; | |
103 strncpy(logBuf, p, keyLength); | |
104 LOGV((int) p) LCV(*(unsigned char *)p) LCV(k) LCV(logBuf); | |
105 k = offset[*(unsigned char *) p]; | |
106 if (k && strnicmp(p, ptr, keyLength) == 0) { | |
107 return p; | |
108 } | |
109 p--; | |
110 } | |
111 else { | |
112 p -= keyLength; | |
113 } | |
114 } | |
115 return 0; | |
116 } | |
117 | |
118 | |
119 | |
120 |