From d1ebbd7b3be1b04d99cd7112dcef6074643da005 Mon Sep 17 00:00:00 2001 From: "psyc://psyced.org/~lynX" <@> Date: Wed, 20 Apr 2011 18:59:27 +0200 Subject: [PATCH] clean version of PSYC_matches() --- src/Makefile | 4 ++-- src/match.c | 45 ++++++++++++++++++++++----------------------- tests/testMatch.c | 3 ++- tests/testParser.c | 1 + 4 files changed, 27 insertions(+), 26 deletions(-) diff --git a/src/Makefile b/src/Makefile index ae8bad9..837a9ce 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,4 +1,4 @@ -CFLAGS=-I../../include -DDEBUG -DPSYC_COMPILE_LIBRARY -g -O0 -Wall +CFLAGS=-I../../include -DDEBUG=2 -DPSYC_COMPILE_LIBRARY -g -O0 -Wall CC=cc -I../include # CC=clang @@ -17,7 +17,7 @@ lib: $S ar rcs libpsyc.a $O match: match.c - ${CC} -o $@ -DDEBUG=2 -DCMDTOOL -DTEST $< + ${CC} -o $@ -DDEBUG=4 -DCMDTOOL -DTEST $< it: match diff --git a/src/match.c b/src/match.c index 1292c25..a35a6e7 100644 --- a/src/match.c +++ b/src/match.c @@ -11,53 +11,52 @@ int PSYC_matches(char* sho, size_t slen, if (slen == 0 || *sho != '_' || llen == 0 || *lon != '_') { - PT(("Please use long format keywords (compact ones would be faster, I know..)\n")) + P1(("Please use long format keywords (compact ones would be faster, I know..)\n")) return -2; } if (slen > llen) { - PT(("The long string is shorter than the short one.\n")) + P1(("The long string is shorter than the short one.\n")) return -3; } if (slen == llen) { - if (!strcmp(sho, lon)) { - PT(("Identical arguments.\n")) + if (!strncmp(sho, lon, slen)) { + P1(("Identical arguments.\n")) return 0; } - PT(("Same length but different.\nNo match, but they could be related or have a common type.\n")) + P1(("Same length but different.\nNo match, but they could be related or have a common type.\n")) return -4; } - PT(("*start short '%s' long '%s'\n", sho, lon)) + P3(("# PSYC_matches short '%*s' in long '%*s' ?\n", slen, sho, llen, lon)) se = sho+slen; le = lon+llen; - /* doesn't always work this way.. FIXME */ - *se = *le = '_'; - sho++; lon++; - while((s = strchr(sho, '_'))) { - *s = 0; - PT(("sho goes '%c' and lon goes '%c'\n", *sho, *lon)) - while((l = strchr(lon, '_'))) { - *l = 0; - PT(("strcmp short '%s' long '%s'\n", sho, lon)) - if (!strcmp(sho, lon)) goto foundone; - if (l == le) goto failed; - *l = '_'; + sho++; lon++; slen--; llen--; + while(*sho && sho < se) { + P3(("# comparing short '%*s' (%d)\n", slen, sho, slen)) + unless (s = memchr(sho, '_', slen)) s = se; + P4(("# sho goes '%c' and lon goes '%c'\n", *sho, *lon)) + while(*lon && lon < le) { + P3(("# against long '%*s' (%d)\n", llen, lon, llen)) + unless (l = memchr(lon, '_', llen)) l = le; + P3(("# %d == %d && !strncmp '%*s', '%*s'\n", s-sho, l-lon, s-sho, sho, s-sho, lon)) + if (l-lon == s-sho && !strncmp(sho, lon, s-sho)) goto foundone; + P4(("# failed\n")) + llen -= l-lon + 1; lon = ++l; } goto failed; foundone: - PT(("found: short '%s' long '%s'\n", sho, lon)) - if (s == se) goto success; - *l = *s = '_'; + P3(("# found %d of short '%*s' and long '%*s'\n", s-sho, s-sho, sho, s-sho, lon)) + llen -= l-lon; + slen -= s-sho; sho = ++s; lon = ++l; } -success: return 0; failed: - PT(("No, they don't match.\n")) + P4(("No, they don't match.\n")) return 1; } diff --git a/tests/testMatch.c b/tests/testMatch.c index 933d217..a674aaa 100644 --- a/tests/testMatch.c +++ b/tests/testMatch.c @@ -5,8 +5,9 @@ int main() { if (PSYC_matches("_failure_delivery", 0, "_failure_unsuccessful_delivery_death", 0)) return -1; if (PSYC_matches("_failure_trash", 8, "_failure_unsuccessful_delivery_death", 0)) return -2; if (PSYC_matches("_unsuccessful", 0, "_failure_unsuccessful_delivery_death", 0)) return -3; - if (PSYC_matches("_fail", 0, "_failure_unsuccessful_delivery_death", 0)) return -4; + unless (PSYC_matches("_fail", 0, "_failure_unsuccessful_delivery_death", 0)) return -4; unless (PSYC_matches("_truthahn", 0, "_failure_unsuccessful_delivery_death", 0)) return -5; + puts("PSYC_matches passed all tests."); return 0; // passed all tests } diff --git a/tests/testParser.c b/tests/testParser.c index 7020354..107ec09 100644 --- a/tests/testParser.c +++ b/tests/testParser.c @@ -32,6 +32,7 @@ int main(int argc, char** argv) case PSYC_ENTITY: write(1, &modifier, 1); case PSYC_BODY: + // printf("the string is '%.*s'\n", name); write(1, name.ptr, name.length); write(1, " = ", 3); write(1, value.ptr, value.length);