mirror of
git://git.psyc.eu/libpsyc
synced 2024-08-15 03:19:02 +00:00
clean version of PSYC_matches()
This commit is contained in:
parent
52d672318c
commit
d1ebbd7b3b
4 changed files with 27 additions and 26 deletions
|
@ -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=cc -I../include
|
||||||
# CC=clang
|
# CC=clang
|
||||||
|
|
||||||
|
@ -17,7 +17,7 @@ lib: $S
|
||||||
ar rcs libpsyc.a $O
|
ar rcs libpsyc.a $O
|
||||||
|
|
||||||
match: match.c
|
match: match.c
|
||||||
${CC} -o $@ -DDEBUG=2 -DCMDTOOL -DTEST $<
|
${CC} -o $@ -DDEBUG=4 -DCMDTOOL -DTEST $<
|
||||||
|
|
||||||
it: match
|
it: match
|
||||||
|
|
||||||
|
|
45
src/match.c
45
src/match.c
|
@ -11,53 +11,52 @@ int PSYC_matches(char* sho, size_t slen,
|
||||||
|
|
||||||
if (slen == 0 || *sho != '_' ||
|
if (slen == 0 || *sho != '_' ||
|
||||||
llen == 0 || *lon != '_') {
|
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;
|
return -2;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (slen > llen) {
|
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;
|
return -3;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (slen == llen) {
|
if (slen == llen) {
|
||||||
if (!strcmp(sho, lon)) {
|
if (!strncmp(sho, lon, slen)) {
|
||||||
PT(("Identical arguments.\n"))
|
P1(("Identical arguments.\n"))
|
||||||
return 0;
|
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;
|
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;
|
se = sho+slen;
|
||||||
le = lon+llen;
|
le = lon+llen;
|
||||||
/* doesn't always work this way.. FIXME */
|
sho++; lon++; slen--; llen--;
|
||||||
*se = *le = '_';
|
while(*sho && sho < se) {
|
||||||
sho++; lon++;
|
P3(("# comparing short '%*s' (%d)\n", slen, sho, slen))
|
||||||
while((s = strchr(sho, '_'))) {
|
unless (s = memchr(sho, '_', slen)) s = se;
|
||||||
*s = 0;
|
P4(("# sho goes '%c' and lon goes '%c'\n", *sho, *lon))
|
||||||
PT(("sho goes '%c' and lon goes '%c'\n", *sho, *lon))
|
while(*lon && lon < le) {
|
||||||
while((l = strchr(lon, '_'))) {
|
P3(("# against long '%*s' (%d)\n", llen, lon, llen))
|
||||||
*l = 0;
|
unless (l = memchr(lon, '_', llen)) l = le;
|
||||||
PT(("strcmp short '%s' long '%s'\n", sho, lon))
|
P3(("# %d == %d && !strncmp '%*s', '%*s'\n", s-sho, l-lon, s-sho, sho, s-sho, lon))
|
||||||
if (!strcmp(sho, lon)) goto foundone;
|
if (l-lon == s-sho && !strncmp(sho, lon, s-sho)) goto foundone;
|
||||||
if (l == le) goto failed;
|
P4(("# failed\n"))
|
||||||
*l = '_';
|
llen -= l-lon + 1;
|
||||||
lon = ++l;
|
lon = ++l;
|
||||||
}
|
}
|
||||||
goto failed;
|
goto failed;
|
||||||
foundone:
|
foundone:
|
||||||
PT(("found: short '%s' long '%s'\n", sho, lon))
|
P3(("# found %d of short '%*s' and long '%*s'\n", s-sho, s-sho, sho, s-sho, lon))
|
||||||
if (s == se) goto success;
|
llen -= l-lon;
|
||||||
*l = *s = '_';
|
slen -= s-sho;
|
||||||
sho = ++s;
|
sho = ++s;
|
||||||
lon = ++l;
|
lon = ++l;
|
||||||
}
|
}
|
||||||
success:
|
|
||||||
return 0;
|
return 0;
|
||||||
failed:
|
failed:
|
||||||
PT(("No, they don't match.\n"))
|
P4(("No, they don't match.\n"))
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -5,8 +5,9 @@ int main() {
|
||||||
if (PSYC_matches("_failure_delivery", 0, "_failure_unsuccessful_delivery_death", 0)) return -1;
|
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("_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("_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;
|
unless (PSYC_matches("_truthahn", 0, "_failure_unsuccessful_delivery_death", 0)) return -5;
|
||||||
|
|
||||||
|
puts("PSYC_matches passed all tests.");
|
||||||
return 0; // passed all tests
|
return 0; // passed all tests
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,6 +32,7 @@ int main(int argc, char** argv)
|
||||||
case PSYC_ENTITY:
|
case PSYC_ENTITY:
|
||||||
write(1, &modifier, 1);
|
write(1, &modifier, 1);
|
||||||
case PSYC_BODY:
|
case PSYC_BODY:
|
||||||
|
// printf("the string is '%.*s'\n", name);
|
||||||
write(1, name.ptr, name.length);
|
write(1, name.ptr, name.length);
|
||||||
write(1, " = ", 3);
|
write(1, " = ", 3);
|
||||||
write(1, value.ptr, value.length);
|
write(1, value.ptr, value.length);
|
||||||
|
|
Loading…
Reference in a new issue