From 521b60d5da2a23923daa1939f41651a0cfbc9b2ab69f79c3480a8495fdfc1d69 Mon Sep 17 00:00:00 2001 From: ADAM David Alan Martin Date: Mon, 11 Aug 2025 03:47:43 -0400 Subject: [PATCH] Trie and hyphenator RAII. --- dw/hyphenator.cc | 19 ++++++------------- dw/hyphenator.hh | 5 ++--- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/dw/hyphenator.cc b/dw/hyphenator.cc index bdaad71..ee4eeca 100644 --- a/dw/hyphenator.cc +++ b/dw/hyphenator.cc @@ -49,21 +49,18 @@ std::map< std::string, std::unique_ptr< Hyphenator > > Hyphenator::hyphenators; Hyphenator::Hyphenator (const char *patFile, const char *excFile, int pack) { - trie = NULL; // As long we are not sure whether a pattern file can be read. - using namespace std::literals::string_literals; FILE *trieF = fopen ( (patFile + ".trie"s).c_str(), "r"); if (trieF) { - trie = new Trie (); + trie = std::make_unique< Trie >(); if (trie->load (trieF) != 0) { - delete trie; - trie = NULL; + trie= nullptr; } fclose (trieF); } - if (trie == NULL) { + if (trie == nullptr) { TrieBuilder trieBuilder(pack); FILE *patF = fopen (patFile, "r"); if (patF) { @@ -103,11 +100,6 @@ Hyphenator::Hyphenator (const char *patFile, const char *excFile, int pack) } } -Hyphenator::~Hyphenator () -{ - delete trie; -} - Hyphenator *Hyphenator::getHyphenator (const char *lang) { auto &hyphenator= hyphenators[ lang ]; @@ -468,7 +460,8 @@ int TrieBuilder::stateStackPop () return next; } -Trie *TrieBuilder::createTrie () +std::unique_ptr< Trie > +TrieBuilder::createTrie () { // we need to sort the patterns as byte strings not as unicode qsort (dataList->getArray (), dataList->size (), @@ -483,7 +476,7 @@ Trie *TrieBuilder::createTrie () stateStackPop (); int size = tree->size (); - Trie *trie = new Trie(tree->detachArray(), size, true, dataZone); + auto trie = std::make_unique< Trie >( tree->detachArray(), size, true, dataZone ); dataZone = NULL; return trie; } diff --git a/dw/hyphenator.hh b/dw/hyphenator.hh index ebcd4dc..f294500 100644 --- a/dw/hyphenator.hh +++ b/dw/hyphenator.hh @@ -80,13 +80,13 @@ class TrieBuilder { ~TrieBuilder (); void insert (const char *key, const char *value); - Trie *createTrie(); + std::unique_ptr< Trie > createTrie(); }; class Hyphenator: public lout::object::Object { static std::map< std::string, std::unique_ptr< Hyphenator > > hyphenators; - Trie *trie; + std::unique_ptr< Trie > trie; // Only instantiated when needed. std::optional< std::map< std::string, std::vector< int > > > exceptions; @@ -101,7 +101,6 @@ class Hyphenator: public lout::object::Object public: Hyphenator (const char *patFile, const char *excFile, int pack = 256); - ~Hyphenator(); static Hyphenator *getHyphenator (const char *language); static bool isHyphenationCandidate (const char *word);