aboutsummaryrefslogtreecommitdiff
path: root/src/match.c
blob: f299724dbf7952d6728a8bb48eba802c638c6599 (plain)
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
/****************************************************************************
 * Expression matching.
 * Copyright (C) 2004 Joe Wingbermuehle
 ****************************************************************************/

#include "jwm.h"
#include "match.h"

typedef struct MatchStateType {
	const char *pattern;
	const char *expression;
	int patternOffset;
	int expressionOffset;
	int expressionLength;
} MatchStateType;

static int DoMatch(MatchStateType state);

/****************************************************************************
 ****************************************************************************/
int
Match(const char *pattern, const char *expression)
{

	MatchStateType state;

	if(!pattern && !expression) {
		return 1;
	} else if(!pattern || !expression) {
		return 0;
	}

	state.pattern = pattern;
	state.expression = expression;
	state.patternOffset = 0;
	state.expressionOffset = 0;
	state.expressionLength = strlen(expression);

	return DoMatch(state);

}

/****************************************************************************
 ****************************************************************************/
int
DoMatch(MatchStateType state)
{

	char p, e;

	for(;;) {
		p = state.pattern[state.patternOffset];
		e = state.expression[state.expressionOffset];

		if(p == 0 && e == 0) {
			return 1;
		} else if(p == 0 || e == 0) {
			return 0;
		}

		switch(p) {
		case '*':
			++state.patternOffset;
			while(state.expressionOffset < state.expressionLength) {
				if(DoMatch(state)) {
					return 1;
				}
				++state.expressionOffset;
			}
			return 0;
		default:
			if(p == e) {
				++state.patternOffset;
				++state.expressionOffset;
				break;
			} else {
				return 0;
			}
		}
	}

}