From 51338ac5bb627655403aa5924c18e0bbaaf78b86 Mon Sep 17 00:00:00 2001 From: Aleksa Sarai Date: Tue, 28 Sep 2021 17:49:27 +1000 Subject: [PATCH] lookup: do not bail on first failed lookup with a word list Due to the lack of deinflection support in StarDict, users might want to be able to create a list of possible deinflections and search each one to see if there is a dictionary entry for that deinflection. Being able to do this in one sdcv invocation is far more preferable to calling sdcv once for each candidate due to the performance cost of doing so. The most obvious language that would benefit from this is Japanese, but I'm sure other folks would prefer this. In order to make this use-case better supported -- try to look up every word in the provided list of words before existing with an error if any one of the words failed to be looked up. Signed-off-by: Aleksa Sarai --- src/sdcv.cpp | 11 +++++++---- tests/t_json | 3 +++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/src/sdcv.cpp b/src/sdcv.cpp index 29635d4..170ac2f 100644 --- a/src/sdcv.cpp +++ b/src/sdcv.cpp @@ -219,12 +219,15 @@ try { search_result rval = SEARCH_SUCCESS; gchar **p = get_impl(word_list); while (*p) { - if ((rval = lib.process_phrase(*p++, *io, non_interactive)) != SEARCH_SUCCESS) { - return rval; - } + search_result this_rval = lib.process_phrase(*p++, *io, non_interactive); + // If we encounter any error, save it but continue through the word + // list to check all requested words. + if (rval == SEARCH_SUCCESS) + rval = this_rval; } + if (rval != SEARCH_SUCCESS) + return rval; } else if (!non_interactive) { - std::string phrase; while (io->read(_("Enter word or phrase: "), phrase)) { if (lib.process_phrase(phrase.c_str(), *io) == SEARCH_FAILURE) diff --git a/tests/t_json b/tests/t_json index 1887b0b..79db5f6 100755 --- a/tests/t_json +++ b/tests/t_json @@ -22,4 +22,7 @@ test_json '[{"name": "Test synonyms", "wordcount": "2"},{"name": "Sample 1 test test_json '[{"dict": "Test synonyms","word":"test","definition":"\u000aresult of test"}]' -x -j -n --data-dir "$TEST_DIR" foo test_json '[]' -x -j -n --data-dir "$TEST_DIR" foobarbaaz +# Test multiple searches, with the first failing. +test_json '[][{"dict": "Test synonyms","word":"test","definition":"\u000aresult of test"}]' -x -j -n --data-dir "$TEST_DIR" foobarbaaz foo + exit 0