From 49d392f261391f1c5b238df80c937d652ebbea87 Mon Sep 17 00:00:00 2001 From: Joseph Hunkeler Date: Fri, 15 Oct 2021 09:29:01 -0400 Subject: Optimize dictionary search for speed --- lib/jdtalk/core.d | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/jdtalk/core.d b/lib/jdtalk/core.d index c6a2c6b..d8f5386 100644 --- a/lib/jdtalk/core.d +++ b/lib/jdtalk/core.d @@ -153,24 +153,33 @@ bool wordStartsWith(ref string[] words, char ch) { bool searchDict(dict_t dict, string pattern) { bool has_pattern = false; foreach (wordList; [dict.noun, dict.verb, dict.adverb, dict.adjective]) { - foreach (word; wordList) { - has_pattern = wordList.canFind(pattern); - if (has_pattern) break; + if ((has_pattern = wordList.canFind(pattern)) == true) { + break; } - if (has_pattern) break; } return has_pattern; } -char acronymSafe(ref dict_t dict, string acronym) { +char acronymSafe(ref dict_t dict, string acronym, string pattern=null) { string[] words; + char last = 0; + words = dict.noun ~ dict.verb ~ dict.adverb ~ dict.adjective; for (int i = 0; i < acronym.length; i++) { if (!wordStartsWith(words, acronym[i])) { - return acronym[i]; + last = acronym[i]; + } + } + + bool has_pattern = false; + if (pattern) { + char check = pattern[0]; + has_pattern = acronym.canFind(check.to!string); + if (!has_pattern) { + last = check; } } - return 0; + return last; } -- cgit