File: pattern.cpp

package info (click to toggle)
fcitx-unikey 0.2.7%2Bgit20220410-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 684 kB
  • sloc: cpp: 6,904; makefile: 8
file content (100 lines) | stat: -rw-r--r-- 2,921 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
// -*- 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<count; i++)
		m_patterns[i].init(patterns[i]);
}

//-----------------------------------------------------
// return the order number of the pattern that is found.
// If more than 1 pattern is found, returns any pattern
// Returns -1 if no pattern is found
//-----------------------------------------------------
int PatternList::foundAtNextChar(char ch)
{
	int patternFound = -1;
	for (int i=0; i<m_count; i++) {
		if (m_patterns[i].foundAtNextChar(ch))
			patternFound = i;
	}
	return patternFound;
}

//-----------------------------------------------------
void PatternList::reset()
{
	for (int i=0; i<m_count; i++)
		m_patterns[i].reset();
}