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)
{
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;
}

View File

@ -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);