# HG changeset patch # User Rubidium # Date 2023-01-13 16:55:43 # Node ID baeac55e5810d90cb6089a476daeb027db706efc # Parent 108afc18327b281f2ca02aaaca69932be3134d54 Codechange: use smart pointers when creating StringIterators diff --git a/src/os/macosx/string_osx.cpp b/src/os/macosx/string_osx.cpp --- a/src/os/macosx/string_osx.cpp +++ b/src/os/macosx/string_osx.cpp @@ -443,9 +443,9 @@ int MacOSStringCompare(const char *s1, c return this->utf16_to_utf8[this->cur_pos]; } -/* static */ StringIterator *OSXStringIterator::Create() +/* static */ std::unique_ptr OSXStringIterator::Create() { if (!MacOSVersionIsAtLeast(10, 5, 0)) return nullptr; - return new OSXStringIterator(); + return std::make_unique(); } diff --git a/src/os/macosx/string_osx.h b/src/os/macosx/string_osx.h --- a/src/os/macosx/string_osx.h +++ b/src/os/macosx/string_osx.h @@ -33,7 +33,7 @@ public: size_t Next(IterType what) override; size_t Prev(IterType what) override; - static StringIterator *Create(); + static std::unique_ptr Create(); }; /** diff --git a/src/string.cpp b/src/string.cpp --- a/src/string.cpp +++ b/src/string.cpp @@ -765,9 +765,9 @@ int strnatcmp(const char *s1, const char #ifdef WITH_UNISCRIBE -/* static */ StringIterator *StringIterator::Create() +/* static */ std::unique_ptr StringIterator::Create() { - return new UniscribeStringIterator(); + return std::make_unique(); } #elif defined(WITH_ICU_I18N) @@ -921,9 +921,9 @@ public: } }; -/* static */ StringIterator *StringIterator::Create() +/* static */ std::unique_ptr StringIterator::Create() { - return new IcuStringIterator(); + return std::make_unique(); } #else @@ -1032,17 +1032,17 @@ public: }; #if defined(WITH_COCOA) && !defined(STRGEN) && !defined(SETTINGSGEN) -/* static */ StringIterator *StringIterator::Create() +/* static */ std::unique_ptr StringIterator::Create() { - StringIterator *i = OSXStringIterator::Create(); + std::unique_ptr i = OSXStringIterator::Create(); if (i != nullptr) return i; - return new DefaultStringIterator(); + return std::make_unique(); } #else -/* static */ StringIterator *StringIterator::Create() +/* static */ std::unique_ptr StringIterator::Create() { - return new DefaultStringIterator(); + return std::make_unique(); } #endif /* defined(WITH_COCOA) && !defined(STRGEN) && !defined(SETTINGSGEN) */ diff --git a/src/string_base.h b/src/string_base.h --- a/src/string_base.h +++ b/src/string_base.h @@ -26,7 +26,7 @@ public: * Create a new iterator instance. * @return New iterator instance. */ - static StringIterator *Create(); + static std::unique_ptr Create(); virtual ~StringIterator() {} diff --git a/src/textbuf.cpp b/src/textbuf.cpp --- a/src/textbuf.cpp +++ b/src/textbuf.cpp @@ -369,13 +369,11 @@ bool Textbuf::MovePos(uint16 keycode) * @param max_chars maximum size in chars, including terminating '\0' */ Textbuf::Textbuf(uint16 max_bytes, uint16 max_chars) - : buf(MallocT(max_bytes)) + : buf(MallocT(max_bytes)), char_iter(StringIterator::Create()) { assert(max_bytes != 0); assert(max_chars != 0); - this->char_iter = StringIterator::Create(); - this->afilter = CS_ALPHANUMERAL; this->max_bytes = max_bytes; this->max_chars = max_chars == UINT16_MAX ? max_bytes : max_chars; @@ -385,7 +383,6 @@ Textbuf::Textbuf(uint16 max_bytes, uint1 Textbuf::~Textbuf() { - delete this->char_iter; free(this->buf); } diff --git a/src/textbuf_type.h b/src/textbuf_type.h --- a/src/textbuf_type.h +++ b/src/textbuf_type.h @@ -67,7 +67,7 @@ struct Textbuf { void DiscardMarkedText(bool update = true); private: - StringIterator *char_iter; + std::unique_ptr char_iter; bool CanDelChar(bool backspace);