From 885a5ba629f8ca124bb3f2bff47a27522287370da635c204725696b2592c2d16 Mon Sep 17 00:00:00 2001 From: ADAM David Alan Martin Date: Mon, 7 Apr 2025 22:37:33 -0400 Subject: [PATCH] Another memory simplification. --- dw/findtext.cc | 38 ++++++++++++-------------------------- dw/findtext.hh | 5 ++++- 2 files changed, 16 insertions(+), 27 deletions(-) diff --git a/dw/findtext.cc b/dw/findtext.cc index 8b2cedc..601fc35 100644 --- a/dw/findtext.cc +++ b/dw/findtext.cc @@ -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; } diff --git a/dw/findtext.hh b/dw/findtext.hh index 5323556..43f4290 100644 --- a/dw/findtext.hh +++ b/dw/findtext.hh @@ -7,6 +7,9 @@ #include +#include +#include + 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;