FreeBSD: add file descriptor tracking for _umtx_op
[valgrind.git] / coregrind / m_seqmatch.c
blob09abbca092adc2d049b514ec14eca24fb19d60f9
2 /*--------------------------------------------------------------------*/
3 /*--- A simple sequence matching facility. ---*/
4 /*--- m_seqmatch.c ---*/
5 /*--------------------------------------------------------------------*/
7 /*
8 This file is part of Valgrind, a dynamic binary instrumentation
9 framework.
11 Copyright (C) 2008-2017 OpenWorks Ltd
12 info@open-works.co.uk
14 This program is free software; you can redistribute it and/or
15 modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation; either version 2 of the
17 License, or (at your option) any later version.
19 This program is distributed in the hope that it will be useful, but
20 WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, see <http://www.gnu.org/licenses/>.
27 The GNU General Public License is contained in the file COPYING.
30 #include "pub_core_basics.h"
31 #include "pub_core_libcassert.h"
32 #include "pub_core_libcbase.h" // VG_(strlen)
33 #include "pub_core_seqmatch.h" // self
35 /* ---------------------------------------------------------------------
36 A simple sequence matching facility
37 ------------------------------------------------------------------ */
39 /* See detailed comment in include/pub_tool_seqmatch.h about this. */
40 Bool VG_(generic_match) (
41 Bool matchAll,
42 const void* patt, SizeT szbPatt, UWord nPatt, UWord ixPatt,
43 const void* input, SizeT szbInput, UWord nInput, UWord ixInput,
44 Bool (*pIsStar)(const void*),
45 Bool (*pIsQuery)(const void*),
46 Bool (*pattEQinp)(const void*,const void*,void*,UWord),
47 void* inputCompleter, Bool (*haveInputInpC)(void*,UWord)
50 /* This is the spec, written in my favourite formal specification
51 language. It specifies non-greedy matching of '*'s.
53 ma ('*':ps) (i:is) = ma ps (i:is) || ma ('*':ps) is
54 ma ('*':ps) [] = ma ps []
56 ma ('?':ps) (i:is) = ma ps is
57 ma ('?':ps) [] = False
59 ma (p:ps) (i:is) = p == i && ma ps is
61 ma (p:ps) [] = False
62 ma [] (i:is) = False -- m-all, True for m-prefix
63 ma [] [] = True
65 Bool havePatt, haveInput;
66 const HChar *currPatt, *currInput;
67 tailcall:
68 vg_assert(nPatt < 1000000); /* arbitrary */
69 vg_assert(inputCompleter || (nInput < 1000000)); /* arbitrary */
70 vg_assert(ixPatt <= nPatt);
71 vg_assert(inputCompleter || ixInput <= nInput);
73 havePatt = ixPatt < nPatt;
74 haveInput = inputCompleter ?
75 (*haveInputInpC)(inputCompleter, ixInput)
76 : ixInput < nInput;
78 /* No specific need to set NULL when !have{Patt,Input}, but guards
79 against inadvertantly dereferencing an out of range pointer to
80 the pattern or input arrays. */
81 currPatt = havePatt ? ((const HChar*)patt) + szbPatt * ixPatt : NULL;
82 currInput = haveInput && !inputCompleter ?
83 ((const HChar*)input) + szbInput * ixInput : NULL;
85 // Deal with the complex case first: wildcards. Do frugal
86 // matching. When encountering a '*', first skip no characters
87 // at all, and see if the rest of the match still works. Only if
88 // that fails do we then skip a character, and retry at the next
89 // position.
91 // ma ('*':ps) (i:is) = ma ps (i:is) || ma ('*':ps) is
93 // If we're out of input, check the rest of the pattern matches
94 // the empty input. This really means can only be be empty or
95 // composed entirely of '*'s.
97 // ma ('*':ps) [] = ma ps []
99 if (havePatt && pIsStar(currPatt)) {
100 if (haveInput) {
101 // ma ('*':ps) (i:is) = ma ps (i:is) || ma ('*':ps) is
102 // we unavoidably have to make a real recursive call for the
103 // first half of the OR, since this isn't straight tail-recursion.
104 if (VG_(generic_match)( matchAll,
105 patt, szbPatt, nPatt, ixPatt+1,
106 input,szbInput,nInput, ixInput+0,
107 pIsStar,pIsQuery,pattEQinp,
108 inputCompleter,haveInputInpC) ) {
109 return True;
111 // but we can tail-recurse for the second call
112 ixInput++; goto tailcall;
113 } else {
114 // ma ('*':ps) [] = ma ps []
115 ixPatt++; goto tailcall;
119 // simpler cases now. Deal with '?' wildcards.
121 // ma ('?':ps) (i:is) = ma ps is
122 // ma ('?':ps) [] = False
123 if (havePatt && pIsQuery(currPatt)) {
124 if (haveInput) {
125 ixPatt++; ixInput++; goto tailcall;
126 } else {
127 return False;
131 // obvious case with literal chars in the pattern
133 // ma (p:ps) (i:is) = p == i && ma ps is
134 if (havePatt && haveInput) {
135 if (!pattEQinp(currPatt,currInput,inputCompleter,ixInput)) return False;
136 ixPatt++; ixInput++; goto tailcall;
139 // if we run out of input before we run out of pattern, we must fail
140 // ma (p:ps) [] = False
141 if (havePatt && !haveInput) return False;
143 // if we run out of pattern before we run out of input, the
144 // verdict depends on the matching mode. If we are trying to
145 // match exactly (the pattern must consume the entire input)
146 // then the outcome is failure. However, if we're merely attempting
147 // to match some prefix of the input, then we have been successful.
149 // ma [] (i:is) = False -- m-all, True for m-prefix
150 if (!havePatt && haveInput) {
151 return matchAll ? False // match-all
152 : True; // match-prefix
155 // finally, if both sequence and input are both completely
156 // consumed, then we were successful, regardless of matching mode.
157 if (!havePatt && !haveInput) return True;
159 // end of cases
160 vg_assert(0);
164 /* And a parameterization of the above, to make it do
165 string matching.
167 static Bool charIsStar ( const void* pV ) { return *(const HChar*)pV == '*'; }
168 static Bool charIsQuery ( const void* pV ) { return *(const HChar*)pV == '?'; }
169 static Bool char_p_EQ_i ( const void* pV, const void* cV,
170 void* null_completer, UWord ixcV ) {
171 HChar p = *(const HChar*)pV;
172 HChar c = *(const HChar*)cV;
173 vg_assert(p != '*' && p != '?');
174 return p == c;
176 Bool VG_(string_match) ( const HChar* patt, const HChar* input )
178 return VG_(generic_match)(
179 True/* match-all */,
180 patt, sizeof(HChar), VG_(strlen)(patt), 0,
181 input, sizeof(HChar), VG_(strlen)(input), 0,
182 charIsStar, charIsQuery, char_p_EQ_i,
183 NULL, NULL
188 // test cases for the matcher (in match-all mode)
189 // typedef struct { char* patt; char* input; Bool xres; } Test;
191 //static Test tests[] =
192 // {
193 // { "" ,"" , True },
194 // { "a" ,"" , False },
195 // { "a" ,"b" , False },
196 // { "a" ,"a" , True },
197 // { "a" ,"aa" , False },
198 // { "*" ,"" , True },
199 // { "**" ,"" , True },
200 // { "*" ,"abc", True },
201 // { "*a" ,"abc", False },
202 // { "*b" ,"abc", False },
203 // { "*bc" ,"abc", True },
204 // { "a*b" ,"abc", False },
205 // { "a*c" ,"abc", True },
206 // { "*c" ,"abc", True },
207 // { "c*c" ,"abc", False },
208 // { "abc*" ,"abc", True },
209 // { "abc**" ,"abc", True },
210 // { "**abc" ,"abc", True },
211 // { "**a*b*c**" ,"abc", True },
212 // { "**a*b*d**" ,"abc", False },
213 // { "a?b" ,"abc", False },
214 // { "a?c" ,"abc", True },
215 // { "?" ,"" , False },
216 // { "?" ,"a" , True },
217 // { "?" ,"ab" , False },
218 // { "abcd" ,"abc", False },
219 // { "ab" ,"abc", False },
220 // { NULL ,NULL , False }
221 // };
223 //int main ( void )
225 // Test* t;
226 // for (t = tests; t->patt; t++) {
227 // printf("%-10s %-6s %s\n",
228 // t->patt, t->input,
229 // match_string_all((UChar*)t->patt,(UChar*)t->input,True)
230 // == t->xres
231 // ? "pass" : "FAIL" );
232 // }
233 // return 0;
236 /*--------------------------------------------------------------------*/
237 /*--- end m_seqmatch.c ---*/
238 /*--------------------------------------------------------------------*/