From e595f53e5bb77d05cb5d981a6fbd613cda5f108f Mon Sep 17 00:00:00 2001 From: Gregory Nutt Date: Thu, 12 Dec 2013 08:40:54 -0600 Subject: Pattern matching logic extended by Ken Pettit --- nuttx/ChangeLog | 4 ++- nuttx/libc/misc/lib_match.c | 73 +++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 71 insertions(+), 6 deletions(-) diff --git a/nuttx/ChangeLog b/nuttx/ChangeLog index 47c8a65e2..792230a0a 100644 --- a/nuttx/ChangeLog +++ b/nuttx/ChangeLog @@ -6197,4 +6197,6 @@ defintion header files (2013-12-10). * arch/arm/src/a1x/a1x_pio.c and .h: Support for PIO configuration (2013-12-11). - + * libc/misc/lib_match.c: Pattern matching logic extended to handle + matches to sets of characters and ranges of character values. From + Ken Pettit (2013-12-12). diff --git a/nuttx/libc/misc/lib_match.c b/nuttx/libc/misc/lib_match.c index a8cfad329..4f96542f6 100644 --- a/nuttx/libc/misc/lib_match.c +++ b/nuttx/libc/misc/lib_match.c @@ -5,9 +5,11 @@ * This pattern matcher only handles '?', '*' and '**', and multiple * patterns separated by '|'. * - * Copyright © 1995,2000 by Jef Poskanzer . + * Copyright © 1995, 2000 by Jef Poskanzer . * All rights reserved. * + * With extensions by Ken Pettit. + * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: @@ -57,6 +59,8 @@ static int match_one(const char *pattern, int patlen, const char *string) { const char *p; + char first; + char last; int pl; int i; @@ -67,6 +71,63 @@ static int match_one(const char *pattern, int patlen, const char *string) continue; } + /* Match single character from a set: "[a-zA-Z]" for instance */ + + if (*p == '[' && *string != '\0') + { + i = 0; + while (*p != ']' && *p != '\0') + { + p++; + + if (*string == *p) + { + /* Match found. Advance to the ']' */ + + i = 1; + while (*p != ']' && *p != '\0') + { + p++; + } + + break; + } + + /* Prepare to test for range */ + + if (*p != '\0') + { + first = *p++; + if (*p == '-') + { + p++; + last = *p++; + if (*string >= first && *string <= *p) + { + /* Match found. Advance to the ']' */ + + i = 1; + while (*p != ']' && *p != '\0') + { + p++; + } + + break; + } + } + } + } + + /* We reuse 'i' above to indicate match found */ + + if (i) + { + continue; + } + + return 0; + } + if (*p == '*') { p++; @@ -92,6 +153,7 @@ static int match_one(const char *pattern, int patlen, const char *string) return 1; } } + return 0; } @@ -105,6 +167,7 @@ static int match_one(const char *pattern, int patlen, const char *string) { return 1; } + return 0; } @@ -116,9 +179,10 @@ static int match_one(const char *pattern, int patlen, const char *string) * Name: match * * Description: - * Simple shell-style filename pattern matcher written by Jef Poskanzer - * This pattern matcher only handles '?', '*' and '**', and multiple - * patterns separated by '|'. + * Simple shell-style filename pattern matcher originally written by + * Jef Poskanzer and extended by Ken Pettit. This pattern matcher handles + * '?', '*', '**', sets like [a-zA-z], and multiple patterns separated + * by '|'. * * Returned Value: * Returns 1 (match) or 0 (no-match). @@ -145,4 +209,3 @@ int match(const char *pattern, const char *string) pattern = or + 1; } } - -- cgit v1.2.3