This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
deb-goldedplus/goldlib/gall/gbmh.cpp

135 lines
3.4 KiB
C++
Raw Normal View History

2000-02-25 10:15:17 +00:00
// This may look like C code, but it is really -*- C++ -*-
// ------------------------------------------------------------------
// The Goldware Library
// Copyright (C) 1990-1999 Odinn Sorensen
// Copyright (C) 2000 Alexander S. Aganichev
// ------------------------------------------------------------------
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Library General Public
// License as published by the Free Software Foundation; either
// version 2 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Library General Public License for more details.
//
// You should have received a copy of the GNU Library General Public
// License along with this program; if not, write to the Free
// Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
// MA 02111-1307, USA
// ------------------------------------------------------------------
// $Id$
// ------------------------------------------------------------------
// Boyer-Moore-Horspool pattern match.
// ------------------------------------------------------------------
// Based on Public Domain version by Thad Smith 7/21/1992, based on a
// 7/92 public domain BMH version by Raymond Gardner.
// With fixes found by Jeff Dunlop 10/21/93.
// ------------------------------------------------------------------
#include <climits>
#include <gstrall.h>
#include <gmemdbg.h>
#include <gbmh.h>
// ------------------------------------------------------------------
gbmh::gbmh() {
pat = NULL;
}
// ------------------------------------------------------------------
gbmh::~gbmh() {
throw_deletearray(pat);
}
// ------------------------------------------------------------------
void gbmh::init(const char* pattern, bool ignorecase) {
ignore_case = ignorecase;
patlen = strlen(pattern);
pat = new char [patlen+1];
throw_new(pat);
// Copy pattern
strcpy(pat, pattern);
if(ignore_case)
strupr(pat);
// initialize skip array
int i;
for(i=0; i<256; i++)
skip[i] = patlen;
for(i=0; i<patlen; i++) {
2004-01-20 14:58:37 +00:00
skip[pat[i] & 0xff] = patlen - i - 1;
2000-02-25 10:15:17 +00:00
if(ignore_case)
2004-01-20 14:58:37 +00:00
skip[tolower((unsigned char)pat[i])] = patlen - i - 1;
2000-02-25 10:15:17 +00:00
}
char lastpatchar = pat[patlen - 1];
2004-01-20 14:58:37 +00:00
skip[lastpatchar & 0xff] = INT_MAX;
2000-02-25 10:15:17 +00:00
if(ignore_case)
2004-01-20 14:58:37 +00:00
skip[tolower((unsigned char)lastpatchar)] = INT_MAX;
2000-02-25 10:15:17 +00:00
// Horspool's fixed second shift
skip2 = patlen;
for(i=0; i<patlen-1; ++i)
if(pat[i] == lastpatchar)
skip2 = patlen - i - 1;
}
// ------------------------------------------------------------------
bool gbmh::find(const char* buffer) {
int buflen = strlen(buffer);
int i = patlen - 1 - buflen;
if(i >= 0)
return false;
buffer += buflen;
while(1) {
2004-01-20 14:58:37 +00:00
while((i += skip[buffer[i] & 0xff]) < 0)
2000-02-25 10:15:17 +00:00
;
if(i < (INT_MAX - buflen))
return false;
i -= INT_MAX;
int j = patlen - 1;
const char* s = buffer + (i - j);
if(ignore_case) {
2004-01-20 14:58:37 +00:00
while(--j >= 0 and toupper((unsigned char)s[j]) == pat[j])
2000-02-25 10:15:17 +00:00
;
}
else {
while(--j >= 0 and s[j] == pat[j])
;
}
if(j < 0)
return true;
if((i += skip2) >= 0)
return false;
}
}
// ------------------------------------------------------------------