1 // -*- coding:unix; mode:c++; tab-width:4; c-basic-offset:4; indent-tabs-mode:nil -*-
2 /*------------------------------------------------------------------------------
3 VnConv: Vietnamese Encoding Converter Library
4 UniKey Project: http://unikey.sourceforge.net
5 Copyleft (C) 1998-2002 Pham Kim Long
6 Contact: longp@cslab.felk.cvut.cz
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 --------------------------------------------------------------------------------*/
25 //////////////////////////////////////////////////
26 // Pattern matching (based on KPM algorithm)
27 //////////////////////////////////////////////////
29 //----------------------------
30 void PatternState::reset()
36 //----------------------------
37 void PatternState::init(char *pattern)
47 while (j>=0 && m_pattern[i]!=m_pattern[j]) j=m_border[j];
53 //-----------------------------------------------------
54 //get next input char, returns 1 if pattern is found.
55 //-----------------------------------------------------
56 int PatternState::foundAtNextChar(char ch)
60 while (m_pos>=0 && ch!=m_pattern[m_pos]) m_pos=m_border[m_pos];
62 if (m_pattern[m_pos]==0) {
64 m_pos = m_border[m_pos];
70 //-----------------------------------------------------
71 void PatternList::init(char **patterns, int count)
75 m_patterns = new PatternState[count];
76 for (int i=0; i<count; i++)
77 m_patterns[i].init(patterns[i]);
80 //-----------------------------------------------------
81 // return the order number of the pattern that is found.
82 // If more than 1 pattern is found, returns any pattern
83 // Returns -1 if no pattern is found
84 //-----------------------------------------------------
85 int PatternList::foundAtNextChar(char ch)
87 int patternFound = -1;
88 for (int i=0; i<m_count; i++) {
89 if (m_patterns[i].foundAtNextChar(ch))
95 //-----------------------------------------------------
96 void PatternList::reset()
98 for (int i=0; i<m_count; i++)
99 m_patterns[i].reset();