From d5fd9ce0880aebb9cc149017b1c2a7829d2006ca Mon Sep 17 00:00:00 2001 From: Shlomi Fish Date: Mon, 30 Mar 2020 11:51:25 +0300 Subject: [PATCH] avoid double traversal. --- fortune-mod/ChangeLog | 4 ++++ fortune-mod/fortune/fortune.c | 26 +++++++++++++---------- fortune-mod/tests/t/lib/FortTestInst.pm | 1 + fortune-mod/tests/t/test-fortune-m.t | 28 ++++++++++++++++++++++++- fortune-mod/ver.txt | 2 +- 5 files changed, 48 insertions(+), 13 deletions(-) diff --git a/fortune-mod/ChangeLog b/fortune-mod/ChangeLog index 2a7b0f3..4d54a27 100644 --- a/fortune-mod/ChangeLog +++ b/fortune-mod/ChangeLog @@ -1,6 +1,10 @@ (Note: this file has been re-arranged to be in reverse chronological order, which is The Right Thing for ChangeLogs - DLC) +March 30, 2020 (fortune-mod 2.18.0) + + Avoid double traversal if FORTDIR == LOCFORTDIR. + February 26, 2020 (fortune-mod 2.16.0) Better portability to Microsoft Windows and other OSes diff --git a/fortune-mod/fortune/fortune.c b/fortune-mod/fortune/fortune.c index 86dbaa3..8834d2d 100644 --- a/fortune-mod/fortune/fortune.c +++ b/fortune-mod/fortune/fortune.c @@ -868,6 +868,16 @@ int add_dir(register FILEDESC * fp) * form_file_list: * Form the file list from the file specifications. */ + +static int cond_top_level__add_file(const char *dirpath, const char*possible_dup) +{ + if (!strcmp(dirpath, possible_dup)) + { + return 0; + } + return add_file(NO_PROB, dirpath, NULL, &File_list, &File_tail, NULL); +} + static int form_file_list(register char **files, register int file_cnt) { register int i, percent; @@ -882,15 +892,12 @@ static int form_file_list(register char **files, register int file_cnt) &File_tail, NULL) | add_file(NO_PROB, LOCOFFDIR, NULL, &File_list, &File_tail, NULL) - | add_file(NO_PROB, FORTDIR, NULL, &File_list, - &File_tail, NULL) - | add_file(NO_PROB, OFFDIR, NULL, &File_list, - &File_tail, NULL)); + | cond_top_level__add_file(FORTDIR, LOCFORTDIR) + | cond_top_level__add_file(OFFDIR, LOCOFFDIR)); else if (Offend) return (add_file(NO_PROB, LOCOFFDIR, NULL, &File_list, &File_tail, NULL) - | add_file(NO_PROB, OFFDIR, NULL, &File_list, - &File_tail, NULL)); + | cond_top_level__add_file(OFFDIR, LOCOFFDIR)); else { if (env_lang) { char *lang; @@ -929,17 +936,14 @@ static int form_file_list(register char **files, register int file_cnt) /* default */ return (add_file(NO_PROB, LOCFORTDIR, NULL, &File_list, &File_tail, NULL) - | add_file(NO_PROB, FORTDIR, NULL, &File_list, - &File_tail, NULL)); + | cond_top_level__add_file(FORTDIR, LOCFORTDIR)); } else /* no locales available, use default */ return (add_file(NO_PROB, LOCFORTDIR, NULL, &File_list, &File_tail, NULL) - | add_file(NO_PROB, FORTDIR, NULL, &File_list, - &File_tail, NULL)); - + | cond_top_level__add_file(FORTDIR, LOCFORTDIR)); } } diff --git a/fortune-mod/tests/t/lib/FortTestInst.pm b/fortune-mod/tests/t/lib/FortTestInst.pm index 45fddab..697f04a 100644 --- a/fortune-mod/tests/t/lib/FortTestInst.pm +++ b/fortune-mod/tests/t/lib/FortTestInst.pm @@ -44,6 +44,7 @@ sub install : () ), "-DCMAKE_INSTALL_PREFIX=$inst_dir", + "-DLOCALDIR=$inst_dir/share/games/fortunes", ( $IS_WIN ? ("-DCMAKE_BUILD_TYPE=Debug") : () ), $ENV{SRC_DIR} ] diff --git a/fortune-mod/tests/t/test-fortune-m.t b/fortune-mod/tests/t/test-fortune-m.t index 2a40a1a..a4a8bca 100644 --- a/fortune-mod/tests/t/test-fortune-m.t +++ b/fortune-mod/tests/t/test-fortune-m.t @@ -7,7 +7,7 @@ use 5.014; use FindBin; use lib "$FindBin::Bin/lib"; use FortTestInst (); -use Test::More tests => 1; +use Test::More tests => 3; { my $inst_dir = FortTestInst::install("fortune-m"); @@ -31,3 +31,29 @@ use Test::More tests => 1; # TEST like( $text, qr/Newton/, 'fortune -m matched' ); } + +{ + my $inst_dir = FortTestInst::install("fortune-m"); + my $IS_WIN = ( $^O eq "MSWin32" ); + my @cmd = ( $inst_dir->child( 'games', 'fortune' ), '-m', '"wet paint"' ); + + # Does not help: + # if ($IS_WIN) + if (0) + { + print "IS_WIN=1\n"; + $cmd[0] = ( "$cmd[0]" =~ s#/#\\\\#gr ); + $cmd[0] .= ".exe"; + print "TransformedRun [@cmd]\n"; + } + print "Running [@cmd]\n"; + my $text = `@cmd`; + my $rc = $?; + print "AfterRun rc=$rc [@cmd]\n"; + + # TEST + like( $text, qr/wet paint/, 'fortune -m matched' ); + + # TEST + unlike( $text, qr/wet paint.*?wet paint/ms, 'no duplicate fortunes' ); +} diff --git a/fortune-mod/ver.txt b/fortune-mod/ver.txt index 7524906..cf86907 100644 --- a/fortune-mod/ver.txt +++ b/fortune-mod/ver.txt @@ -1 +1 @@ -2.16.0 +2.18.0 -- 2.40.0