Another memory simplification.
This commit is contained in:
@ -31,7 +31,6 @@ FindtextState::FindtextState ()
|
|||||||
{
|
{
|
||||||
DBG_OBJ_CREATE ("dw::core::FindtextState");
|
DBG_OBJ_CREATE ("dw::core::FindtextState");
|
||||||
|
|
||||||
key = NULL;
|
|
||||||
nexttab = NULL;
|
nexttab = NULL;
|
||||||
widget = NULL;
|
widget = NULL;
|
||||||
iterator = NULL;
|
iterator = NULL;
|
||||||
@ -40,8 +39,6 @@ FindtextState::FindtextState ()
|
|||||||
|
|
||||||
FindtextState::~FindtextState ()
|
FindtextState::~FindtextState ()
|
||||||
{
|
{
|
||||||
if (key)
|
|
||||||
free(key);
|
|
||||||
if (nexttab)
|
if (nexttab)
|
||||||
delete[] nexttab;
|
delete[] nexttab;
|
||||||
if (iterator)
|
if (iterator)
|
||||||
@ -57,9 +54,7 @@ void FindtextState::setWidget (Widget *widget)
|
|||||||
this->widget = widget;
|
this->widget = widget;
|
||||||
|
|
||||||
// A widget change will restart the search.
|
// A widget change will restart the search.
|
||||||
if (key)
|
if (key.has_value()) key.reset();
|
||||||
free(key);
|
|
||||||
key = NULL;
|
|
||||||
if (nexttab)
|
if (nexttab)
|
||||||
delete[] nexttab;
|
delete[] nexttab;
|
||||||
nexttab = NULL;
|
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),
|
// If the key (or the widget) changes (including case sensitivity),
|
||||||
// the search is started from the beginning.
|
// the search is started from the beginning.
|
||||||
if (this->key == NULL || this->caseSens != caseSens ||
|
if (not this->key.has_value() || this->caseSens != caseSens ||
|
||||||
strcmp (this->key, key) != 0) {
|
this->key.value() != key ) {
|
||||||
newKey = true;
|
newKey = true;
|
||||||
if (this->key)
|
this->key = key;
|
||||||
free(this->key);
|
|
||||||
this->key = dStrdup (key);
|
|
||||||
this->caseSens = caseSens;
|
this->caseSens = caseSens;
|
||||||
|
|
||||||
if (nexttab)
|
if (nexttab)
|
||||||
@ -152,9 +145,7 @@ void FindtextState::resetSearch ()
|
|||||||
{
|
{
|
||||||
unhighlight ();
|
unhighlight ();
|
||||||
|
|
||||||
if (key)
|
key.reset();
|
||||||
free(key);
|
|
||||||
key = NULL;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -177,12 +168,12 @@ const char* FindtextState::rev(const char *str)
|
|||||||
int *FindtextState::createNexttab (const char *needle, bool caseSens,
|
int *FindtextState::createNexttab (const char *needle, bool caseSens,
|
||||||
bool backwards)
|
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 i = 0;
|
||||||
int j = -1;
|
int j = -1;
|
||||||
int l = strlen (key);
|
int l = key.size();
|
||||||
int *nexttab = new int[l + 1]; // + 1 is necessary for l == 1 case
|
int *nexttab = new int[l + 1]; // + 1 is necessary for l == 1 case
|
||||||
nexttab[0] = -1;
|
nexttab[0] = -1;
|
||||||
|
|
||||||
@ -196,9 +187,6 @@ int *FindtextState::createNexttab (const char *needle, bool caseSens,
|
|||||||
j = nexttab[j];
|
j = nexttab[j];
|
||||||
} while (i < l - 1);
|
} while (i < l - 1);
|
||||||
|
|
||||||
if (backwards)
|
|
||||||
delete [] key;
|
|
||||||
|
|
||||||
return nexttab;
|
return nexttab;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -209,7 +197,7 @@ bool FindtextState::unhighlight ()
|
|||||||
{
|
{
|
||||||
if (hlIterator) {
|
if (hlIterator) {
|
||||||
CharIterator *start = hlIterator->cloneCharIterator ();
|
CharIterator *start = hlIterator->cloneCharIterator ();
|
||||||
for (int i = 0; key[i]; i++)
|
for (std::size_t i = 0; key.value().size(); ++i)
|
||||||
start->prev ();
|
start->prev ();
|
||||||
|
|
||||||
CharIterator::unhighlight (start, hlIterator, HIGHLIGHT_FINDTEXT);
|
CharIterator::unhighlight (start, hlIterator, HIGHLIGHT_FINDTEXT);
|
||||||
@ -228,10 +216,11 @@ bool FindtextState::search0 (bool backwards, bool firstTrial)
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
bool ret = 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;
|
int j = 0;
|
||||||
bool nextit = true;
|
bool nextit = true;
|
||||||
int l = strlen (key);
|
int l = searchKey.size();
|
||||||
|
|
||||||
if (backwards && !firstTrial) {
|
if (backwards && !firstTrial) {
|
||||||
_MSG("Having to do.");
|
_MSG("Having to do.");
|
||||||
@ -288,9 +277,6 @@ bool FindtextState::search0 (bool backwards, bool firstTrial)
|
|||||||
ret = true;
|
ret = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (backwards)
|
|
||||||
delete [] searchKey;
|
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -7,6 +7,9 @@
|
|||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
|
|
||||||
|
#include <optional>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
namespace dw {
|
namespace dw {
|
||||||
namespace core {
|
namespace core {
|
||||||
|
|
||||||
@ -34,7 +37,7 @@ private:
|
|||||||
* If dw::core::Findtext::search is called with the same key, the search
|
* If dw::core::Findtext::search is called with the same key, the search
|
||||||
* is continued, otherwise it is restarted.
|
* is continued, otherwise it is restarted.
|
||||||
*/
|
*/
|
||||||
char *key;
|
std::optional< std::string > key;
|
||||||
|
|
||||||
/** \brief Whether the last search was case sensitive. */
|
/** \brief Whether the last search was case sensitive. */
|
||||||
bool caseSens;
|
bool caseSens;
|
||||||
|
|||||||
Reference in New Issue
Block a user