// -*- coding:unix; mode:c++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*- /*------------------------------------------------------------------------------ VnConv: Vietnamese Encoding Converter Library UniKey Project: http://unikey.sourceforge.net Copyleft (C) 1998-2002 Pham Kim Long Contact: longp@cslab.felk.cvut.cz This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA. --------------------------------------------------------------------------------*/ #include "pattern.h" ////////////////////////////////////////////////// // Pattern matching (based on KPM algorithm) ////////////////////////////////////////////////// //---------------------------- void PatternState::reset() { m_pos = 0; m_found = 0; } //---------------------------- void PatternState::init(char *pattern) { m_pos = 0; m_found = 0; m_pattern = pattern; int i=0, j=-1; m_border[i]=j; while (m_pattern[i]) { while (j>=0 && m_pattern[i]!=m_pattern[j]) j=m_border[j]; i++; j++; m_border[i]=j; } } //----------------------------------------------------- //get next input char, returns 1 if pattern is found. //----------------------------------------------------- int PatternState::foundAtNextChar(char ch) { int ret = 0; //int j = m_pos; while (m_pos>=0 && ch!=m_pattern[m_pos]) m_pos=m_border[m_pos]; m_pos++; if (m_pattern[m_pos]==0) { m_found++; m_pos = m_border[m_pos]; ret = 1; } return ret; } //----------------------------------------------------- void PatternList::init(char **patterns, int count) { m_count = count; delete [] m_patterns; m_patterns = new PatternState[count]; for (int i=0; i