implement "a like b" operator (and alias "a matches b"). pattern matching code taken from NetBSD src/lib/libc/gen/fnmatch.c

This commit is contained in:
River Tarnell 2008-08-08 12:13:03 +00:00
parent 0042e5d4f5
commit 90afa30066

View file

@ -85,6 +85,8 @@ struct parser_closure : boost::spirit::closure<parser_closure, datum>
namespace {
int match(char const *, char const *);
datum
f_in(datum const &a, datum const &b)
{
@ -92,6 +94,12 @@ f_in(datum const &a, datum const &b)
return datum(std::search(sb.begin(), sb.end(), sa.begin(), sa.end()) != sb.end());
}
datum
f_like(datum const &str, datum const &pattern)
{
return datum::from_int(match(str.toString().c_str(), pattern.toString().c_str()));
}
datum
f_ternary(datum const &v, datum const &iftrue, datum const &iffalse)
{
@ -280,6 +288,8 @@ struct parser_grammar : public grammar<parser_grammar, parser_closure::context_t
>> *(
"in" >> basic[in_expr.val = bind(&f_in)(in_expr.val, arg1)]
| "contains" >> basic[in_expr.val = bind(&f_in)(arg1, in_expr.val)]
| "like" >> basic[in_expr.val = bind(&f_like)(arg1, in_expr.val)]
| "matches" >> basic[in_expr.val = bind(&f_like)(arg1, in_expr.val)]
)
;
@ -448,6 +458,148 @@ expressor::add_function(std::string const &name, func_t value)
grammar_->add_function(name, value);
}
namespace {
/* $NetBSD: fnmatch.c,v 1.21 2005/12/24 21:11:16 perry Exp $ */
/*
* Copyright (c) 1989, 1993, 1994
* The Regents of the University of California. All rights reserved.
*
* This code is derived from software contributed to Berkeley by
* Guido van Rossum.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Function fnmatch() as specified in POSIX 1003.2-1992, section B.6.
* Compares a filename or pathname to a pattern.
*/
#include <ctype.h>
#include <string.h>
#define EOS '\0'
const char *rangematch (const char *, int);
int
match(char const *pattern, char const *string)
{
const char *stringstart;
char c, test;
for (stringstart = string;;)
switch (c = *pattern++) {
case EOS:
return (*string == EOS ? 1 : 0);
case '?':
if (*string == EOS)
return (0);
++string;
break;
case '*':
c = *pattern;
/* Collapse multiple stars. */
while (c == '*')
c = *++pattern;
/* Optimize for pattern with * at end or before /. */
if (c == EOS) {
return (1);
}
/* General case, use recursion. */
while ((test = *string) != EOS) {
if (match(pattern, string))
return (1);
++string;
}
return (0);
case '[':
if (*string == EOS)
return (0);
if ((pattern =
rangematch(pattern, *string)) == NULL)
return (0);
++string;
break;
case '\\':
if ((c = *pattern++) == EOS) {
c = '\\';
--pattern;
}
/* FALLTHROUGH */
default:
if (c != *string++)
return (0);
break;
}
/* NOTREACHED */
}
const char *
rangematch(char const *pattern, int test)
{
int negate, ok;
char c, c2;
/*
* A bracket expression starting with an unquoted circumflex
* character produces unspecified results (IEEE 1003.2-1992,
* 3.13.2). This implementation treats it like '!', for
* consistency with the regular expression syntax.
* J.T. Conklin (conklin@ngai.kaleida.com)
*/
if ((negate = (*pattern == '!' || *pattern == '^')) != 0)
++pattern;
for (ok = 0; (c = *pattern++) != ']';) {
if (c == '\\')
c = *pattern++;
if (c == EOS)
return (NULL);
if (*pattern == '-'
&& (c2 = (*(pattern+1))) != EOS &&
c2 != ']') {
pattern += 2;
if (c2 == '\\')
c2 = *pattern++;
if (c2 == EOS)
return (NULL);
if (c <= test && test <= c2)
ok = 1;
} else if (c == test)
ok = 1;
}
return (ok == negate ? NULL : pattern);
}
} // anonymous namespace
} // namespace afp
#ifdef TEST_PARSER