Another memory simplification.

This commit is contained in:
2025-04-07 22:37:33 -04:00
parent 40e2306664
commit 885a5ba629
2 changed files with 16 additions and 27 deletions

View File

@ -31,7 +31,6 @@ FindtextState::FindtextState ()
{
DBG_OBJ_CREATE ("dw::core::FindtextState");
key = NULL;
nexttab = NULL;
widget = NULL;
iterator = NULL;
@ -40,8 +39,6 @@ FindtextState::FindtextState ()
FindtextState::~FindtextState ()
{
if (key)
free(key);
if (nexttab)
delete[] nexttab;
if (iterator)
@ -57,9 +54,7 @@ void FindtextState::setWidget (Widget *widget)
this->widget = widget;
// A widget change will restart the search.
if (key)
free(key);
key = NULL;
if (key.has_value()) key.reset();
if (nexttab)
delete[] nexttab;
nexttab = NULL;
@ -83,12 +78,10 @@ FindtextState::Result FindtextState::search (const char *key, bool caseSens,
// If the key (or the widget) changes (including case sensitivity),
// the search is started from the beginning.
if (this->key == NULL || this->caseSens != caseSens ||
strcmp (this->key, key) != 0) {
if (not this->key.has_value() || this->caseSens != caseSens ||
this->key.value() != key ) {
newKey = true;
if (this->key)
free(this->key);
this->key = dStrdup (key);
this->key = key;
this->caseSens = caseSens;
if (nexttab)
@ -152,9 +145,7 @@ void FindtextState::resetSearch ()
{
unhighlight ();
if (key)
free(key);
key = NULL;
key.reset();
}
/*
@ -177,12 +168,12 @@ const char* FindtextState::rev(const char *str)
int *FindtextState::createNexttab (const char *needle, bool caseSens,
bool backwards)
{
const char* key;
std::string key= needle;
if( backwards ) std::reverse( begin( key ), end( key ) );
key = (backwards) ? rev(needle) : needle;
int i = 0;
int j = -1;
int l = strlen (key);
int l = key.size();
int *nexttab = new int[l + 1]; // + 1 is necessary for l == 1 case
nexttab[0] = -1;
@ -196,9 +187,6 @@ int *FindtextState::createNexttab (const char *needle, bool caseSens,
j = nexttab[j];
} while (i < l - 1);
if (backwards)
delete [] key;
return nexttab;
}
@ -209,7 +197,7 @@ bool FindtextState::unhighlight ()
{
if (hlIterator) {
CharIterator *start = hlIterator->cloneCharIterator ();
for (int i = 0; key[i]; i++)
for (std::size_t i = 0; key.value().size(); ++i)
start->prev ();
CharIterator::unhighlight (start, hlIterator, HIGHLIGHT_FINDTEXT);
@ -228,10 +216,11 @@ bool FindtextState::search0 (bool backwards, bool firstTrial)
return false;
bool ret = false;
const char* searchKey = (backwards) ? rev(key) : key;
std::string searchKey= key.has_value() ? key.value() : "";
if( backwards ) std::reverse( begin( searchKey ), end( searchKey ) );
int j = 0;
bool nextit = true;
int l = strlen (key);
int l = searchKey.size();
if (backwards && !firstTrial) {
_MSG("Having to do.");
@ -288,9 +277,6 @@ bool FindtextState::search0 (bool backwards, bool firstTrial)
ret = true;
}
if (backwards)
delete [] searchKey;
return ret;
}

View File

@ -7,6 +7,9 @@
#include <ctype.h>
#include <optional>
#include <string>
namespace dw {
namespace core {
@ -34,7 +37,7 @@ private:
* If dw::core::Findtext::search is called with the same key, the search
* is continued, otherwise it is restarted.
*/
char *key;
std::optional< std::string > key;
/** \brief Whether the last search was case sensitive. */
bool caseSens;