Mercurial > ~dholland > hg > ag > index.cgi
diff 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 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/anagram/agcore/search.cpp Sat Dec 22 17:52:45 2007 -0500 @@ -0,0 +1,120 @@ +/* + * AnaGram, A System for Syntax Directed Programming + * Copyright 1993-2002 Parsifal Software. All Rights Reserved. + * See the file COPYING for license and usage terms. + * + * search.cpp + */ + +#include <ctype.h> +#include "port.h" + +#include "arrays.h" +#include "search.h" + +//#define INCLUDE_LOGGING +#include "log.h" + + +SearchProcess::SearchProcess() + : keyLength(0) + , ptr(0) + , currentMode(notSet) +{ + LOGSECTION("SearchProcess::SearchProcess"); +} + +void SearchProcess::setKey(AgString k) { + LOGSECTION("SearchProcess::setKey"); + key = k; + ptr = key.pointer(); + keyLength = key.size(); + LOGV(key) LCV(ptr) LCV(keyLength); + memset(offset, 0, 256); + currentMode = notSet; + int i; + for (i = 0; i < keyLength; i++) { + offset[(unsigned) key[i]] = (unsigned char)(i+1); + if (isalpha(key[i])) { + offset[(unsigned)key[i]^0x20] = (unsigned char) (i+1); + } + LOGV(i) LV(key[i]); + LOGV(key[i]) LCV(offset[(unsigned) key[i]]); + LOGV(key[i]^0x20) LCV(offset[(unsigned)key[i]^0x20]); + } +} + +char *SearchProcess::scanForward(char *line, int n) { + LOGSECTION("SearchProcess::scanForward"); + if (currentMode != forward) { + int i; + for (i = 0; i < keyLength; i++) { + offset[(unsigned)key[i]] = (unsigned char) (i+1); + if (isalpha((unsigned) key[i])) { + offset[(unsigned)key[i]^0x20] = (unsigned char) (i+1); + } + } + currentMode = forward; + } + //char *logBuf = new char[keyLength + 1]; + //logBuf[keyLength] = 0; + char *p = line + keyLength - 1; + char *end = line + n; + LOGV((int) p) LCV((int) end); + while (p < end) { + int k = offset[*(unsigned char *) p]; + if (k) { + p -= k - 1; + //strncpy(logBuf, p, keyLength); + //LOGV((int) p) LCV(*(unsigned char *)p) LCV(k) LCV(logBuf); + k = offset[*(unsigned char *) p]; + if (k && strnicmp(p, ptr, keyLength) == 0) { + return p; + } + } + p += keyLength; + } + //delete [] logBuf; + return 0; +} + +char *SearchProcess::scanReverse(char *line, int n) { + LOGSECTION("SearchProcess::scanReverse"); + if (currentMode != reverse) { + int i = keyLength; + while (i--) { + offset[(unsigned) key[i]] = (unsigned char) (i+1); + if (isalpha((unsigned) key[i])) { + offset[(unsigned)key[i]^0x20] = (unsigned char) (i+1); + } + } + currentMode = reverse; + } + //char *logBuf = new char[keyLength + 1]; + //char *logBuf = local_array(keyLength + 1, char); + LocalArray<char> logBuf(keyLength + 1); + logBuf[keyLength] = 0; + char *p = line + n - keyLength; + LOGV((int) line) LCV((int) p); + while (line <= p) { + int k = offset[*(unsigned char *)p]; + if (k) { + p -= k - 1; + strncpy(logBuf, p, keyLength); + LOGV((int) p) LCV(*(unsigned char *)p) LCV(k) LCV(logBuf); + k = offset[*(unsigned char *) p]; + if (k && strnicmp(p, ptr, keyLength) == 0) { + return p; + } + p--; + } + else { + p -= keyLength; + } + } + return 0; +} + + + +