medfall

A super great game engine
Log | Files | Refs

patterns.h (2609B)


      1 /*	$OpenBSD: patterns.h,v 1.3 2015/12/12 19:59:43 mmcc Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2015 Reyk Floeter <reyk@openbsd.org>
      5  *
      6  * Permission to use, copy, modify, and distribute this software for any
      7  * purpose with or without fee is hereby granted, provided that the above
      8  * copyright notice and this permission notice appear in all copies.
      9  *
     10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
     11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
     12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
     13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
     14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
     15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
     16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
     17  */
     18 
     19 #ifndef PATTERNS_H
     20 #define PATTERNS_H
     21 
     22 #include <stddef.h>
     23 
     24 #include "array.h"
     25 
     26 #define MAXCAPTURES	32	/* Max no. of allowed captures in pattern */
     27 #define MAXCCALLS	200	/* Max recusion depth in pattern matching */
     28 #define MAXREPETITION	0xfffff	/* Max for repetition items */
     29 
     30 struct match_state {
     31 	int matchdepth;		/* control for recursive depth (to avoid C
     32 				 * stack overflow) */
     33 	int repetitioncounter;	/* control the repetition items */
     34 	int maxcaptures;	/* configured capture limit */
     35 	const char *src_init;	/* init of source string */
     36 	const char *src_end;	/* end ('\0') of source string */
     37 	const char *p_end;	/* end ('\0') of pattern */
     38 	const char *error;	/* should be NULL */
     39 	int level;		/* total number of captures (finished or
     40 				 * unfinished) */
     41 	struct {
     42 		const char * init;
     43 		ssize_t len;
     44 	} capture[MAXCAPTURES];
     45 };
     46 
     47 struct gmatch_state {
     48 	const char *src;
     49 	const char *p;
     50 	const char *lastmatch;
     51 	match_state ms;
     52 };
     53 
     54 struct Matches {
     55 	const array< const char > operator[]( size_t i ) {
     56 		return matches[ i ];
     57 	}
     58 
     59 	StaticArray< array< const char >, MAXCAPTURES > matches_data;
     60 	array< array< const char > > matches;
     61 };
     62 
     63 bool match( Matches * matches, const char * str, const char * pattern );
     64 bool match( Matches * matches, array< const char > str, const char * pattern );
     65 
     66 class gmatch {
     67 	gmatch_state gm;
     68 
     69 	struct Iterator {
     70 		Iterator( gmatch_state * gm );
     71 		array< array< const char > > operator*();
     72 		void operator++();
     73 		bool operator!=( const Iterator & other );
     74 		void next();
     75 
     76 		bool done;
     77 		gmatch_state * gm;
     78 		Matches matches;
     79 	};
     80 
     81 public:
     82 	gmatch( const char * str, const char * pattern );
     83 
     84 	Iterator begin() {
     85 		return Iterator( &gm );
     86 	}
     87 
     88 	Iterator end() {
     89 		return Iterator( NULL );
     90 	}
     91 };
     92 
     93 #endif /* PATTERNS_H */