1
0
Fork 0

Codechange: use smart pointers when creating StringIterators

pull/10342/head
Rubidium 2023-01-13 17:55:43 +01:00 committed by rubidium42
parent b35c791d05
commit b951332def
6 changed files with 15 additions and 18 deletions

View File

@ -443,9 +443,9 @@ int MacOSStringCompare(const char *s1, const char *s2)
return this->utf16_to_utf8[this->cur_pos]; return this->utf16_to_utf8[this->cur_pos];
} }
/* static */ StringIterator *OSXStringIterator::Create() /* static */ std::unique_ptr<StringIterator> OSXStringIterator::Create()
{ {
if (!MacOSVersionIsAtLeast(10, 5, 0)) return nullptr; if (!MacOSVersionIsAtLeast(10, 5, 0)) return nullptr;
return new OSXStringIterator(); return std::make_unique<OSXStringIterator>();
} }

View File

@ -33,7 +33,7 @@ public:
size_t Next(IterType what) override; size_t Next(IterType what) override;
size_t Prev(IterType what) override; size_t Prev(IterType what) override;
static StringIterator *Create(); static std::unique_ptr<StringIterator> Create();
}; };
/** /**

View File

@ -765,9 +765,9 @@ int strnatcmp(const char *s1, const char *s2, bool ignore_garbage_at_front)
#ifdef WITH_UNISCRIBE #ifdef WITH_UNISCRIBE
/* static */ StringIterator *StringIterator::Create() /* static */ std::unique_ptr<StringIterator> StringIterator::Create()
{ {
return new UniscribeStringIterator(); return std::make_unique<UniscribeStringIterator>();
} }
#elif defined(WITH_ICU_I18N) #elif defined(WITH_ICU_I18N)
@ -921,9 +921,9 @@ public:
} }
}; };
/* static */ StringIterator *StringIterator::Create() /* static */ std::unique_ptr<StringIterator> StringIterator::Create()
{ {
return new IcuStringIterator(); return std::make_unique<IcuStringIterator>();
} }
#else #else
@ -1032,17 +1032,17 @@ public:
}; };
#if defined(WITH_COCOA) && !defined(STRGEN) && !defined(SETTINGSGEN) #if defined(WITH_COCOA) && !defined(STRGEN) && !defined(SETTINGSGEN)
/* static */ StringIterator *StringIterator::Create() /* static */ std::unique_ptr<StringIterator> StringIterator::Create()
{ {
StringIterator *i = OSXStringIterator::Create(); std::unique_ptr<StringIterator> i = OSXStringIterator::Create();
if (i != nullptr) return i; if (i != nullptr) return i;
return new DefaultStringIterator(); return std::make_unique<DefaultStringIterator>();
} }
#else #else
/* static */ StringIterator *StringIterator::Create() /* static */ std::unique_ptr<StringIterator> StringIterator::Create()
{ {
return new DefaultStringIterator(); return std::make_unique<DefaultStringIterator>();
} }
#endif /* defined(WITH_COCOA) && !defined(STRGEN) && !defined(SETTINGSGEN) */ #endif /* defined(WITH_COCOA) && !defined(STRGEN) && !defined(SETTINGSGEN) */

View File

@ -26,7 +26,7 @@ public:
* Create a new iterator instance. * Create a new iterator instance.
* @return New iterator instance. * @return New iterator instance.
*/ */
static StringIterator *Create(); static std::unique_ptr<StringIterator> Create();
virtual ~StringIterator() {} virtual ~StringIterator() {}

View File

@ -369,13 +369,11 @@ bool Textbuf::MovePos(uint16 keycode)
* @param max_chars maximum size in chars, including terminating '\0' * @param max_chars maximum size in chars, including terminating '\0'
*/ */
Textbuf::Textbuf(uint16 max_bytes, uint16 max_chars) Textbuf::Textbuf(uint16 max_bytes, uint16 max_chars)
: buf(MallocT<char>(max_bytes)) : buf(MallocT<char>(max_bytes)), char_iter(StringIterator::Create())
{ {
assert(max_bytes != 0); assert(max_bytes != 0);
assert(max_chars != 0); assert(max_chars != 0);
this->char_iter = StringIterator::Create();
this->afilter = CS_ALPHANUMERAL; this->afilter = CS_ALPHANUMERAL;
this->max_bytes = max_bytes; this->max_bytes = max_bytes;
this->max_chars = max_chars == UINT16_MAX ? max_bytes : max_chars; this->max_chars = max_chars == UINT16_MAX ? max_bytes : max_chars;
@ -385,7 +383,6 @@ Textbuf::Textbuf(uint16 max_bytes, uint16 max_chars)
Textbuf::~Textbuf() Textbuf::~Textbuf()
{ {
delete this->char_iter;
free(this->buf); free(this->buf);
} }

View File

@ -67,7 +67,7 @@ struct Textbuf {
void DiscardMarkedText(bool update = true); void DiscardMarkedText(bool update = true);
private: private:
StringIterator *char_iter; std::unique_ptr<StringIterator> char_iter;
bool CanDelChar(bool backspace); bool CanDelChar(bool backspace);