diff options
| author | Joseph Hunkeler <jhunkeler@gmail.com> | 2021-10-15 09:29:01 -0400 | 
|---|---|---|
| committer | Joseph Hunkeler <jhunkeler@gmail.com> | 2021-10-15 09:29:01 -0400 | 
| commit | 49d392f261391f1c5b238df80c937d652ebbea87 (patch) | |
| tree | 77c6604bc3d77fe24dbfd9fb231b9fe9824379a6 /lib/jdtalk | |
| parent | 248b0ccc87b7fcb4a260790d38f3e59a971abf31 (diff) | |
| download | jdtalk-49d392f261391f1c5b238df80c937d652ebbea87.tar.gz | |
Optimize dictionary search for speed
Diffstat (limited to 'lib/jdtalk')
| -rw-r--r-- | lib/jdtalk/core.d | 23 | 
1 files 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;  } | 
