Trie and hyphenator RAII.

This commit is contained in:
2025-08-11 03:47:43 -04:00
parent 91da2ff34b
commit 521b60d5da
2 changed files with 8 additions and 16 deletions

View File

@ -49,21 +49,18 @@ std::map< std::string, std::unique_ptr< Hyphenator > > Hyphenator::hyphenators;
Hyphenator::Hyphenator (const char *patFile, const char *excFile, int pack) 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; using namespace std::literals::string_literals;
FILE *trieF = fopen ( (patFile + ".trie"s).c_str(), "r"); FILE *trieF = fopen ( (patFile + ".trie"s).c_str(), "r");
if (trieF) { if (trieF) {
trie = new Trie (); trie = std::make_unique< Trie >();
if (trie->load (trieF) != 0) { if (trie->load (trieF) != 0) {
delete trie; trie= nullptr;
trie = NULL;
} }
fclose (trieF); fclose (trieF);
} }
if (trie == NULL) { if (trie == nullptr) {
TrieBuilder trieBuilder(pack); TrieBuilder trieBuilder(pack);
FILE *patF = fopen (patFile, "r"); FILE *patF = fopen (patFile, "r");
if (patF) { 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) Hyphenator *Hyphenator::getHyphenator (const char *lang)
{ {
auto &hyphenator= hyphenators[ lang ]; auto &hyphenator= hyphenators[ lang ];
@ -468,7 +460,8 @@ int TrieBuilder::stateStackPop ()
return next; return next;
} }
Trie *TrieBuilder::createTrie () std::unique_ptr< Trie >
TrieBuilder::createTrie ()
{ {
// we need to sort the patterns as byte strings not as unicode // we need to sort the patterns as byte strings not as unicode
qsort (dataList->getArray (), dataList->size (), qsort (dataList->getArray (), dataList->size (),
@ -483,7 +476,7 @@ Trie *TrieBuilder::createTrie ()
stateStackPop (); stateStackPop ();
int size = tree->size (); 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; dataZone = NULL;
return trie; return trie;
} }

View File

@ -80,13 +80,13 @@ class TrieBuilder {
~TrieBuilder (); ~TrieBuilder ();
void insert (const char *key, const char *value); void insert (const char *key, const char *value);
Trie *createTrie(); std::unique_ptr< Trie > createTrie();
}; };
class Hyphenator: public lout::object::Object class Hyphenator: public lout::object::Object
{ {
static std::map< std::string, std::unique_ptr< Hyphenator > > hyphenators; static std::map< std::string, std::unique_ptr< Hyphenator > > hyphenators;
Trie *trie; std::unique_ptr< Trie > trie;
// Only instantiated when needed. // Only instantiated when needed.
std::optional< std::map< std::string, std::vector< int > > > exceptions; std::optional< std::map< std::string, std::vector< int > > > exceptions;
@ -101,7 +101,6 @@ class Hyphenator: public lout::object::Object
public: public:
Hyphenator (const char *patFile, const char *excFile, int pack = 256); Hyphenator (const char *patFile, const char *excFile, int pack = 256);
~Hyphenator();
static Hyphenator *getHyphenator (const char *language); static Hyphenator *getHyphenator (const char *language);
static bool isHyphenationCandidate (const char *word); static bool isHyphenationCandidate (const char *word);