Changeset - r26745:baeac55e5810
[Not reviewed]
master
0 6 0
Rubidium - 23 months ago 2023-01-13 16:55:43
rubidium@openttd.org
Codechange: use smart pointers when creating StringIterators
6 files changed with 15 insertions and 18 deletions:
0 comments (0 inline, 0 general)
src/os/macosx/string_osx.cpp
Show inline comments
 
@@ -440,12 +440,12 @@ int MacOSStringCompare(const char *s1, c
 
		this->cur_pos--;
 
	} while (this->cur_pos > 0 && (what == ITER_WORD ? !this->str_info[this->cur_pos].word_stop : !this->str_info[this->cur_pos].char_stop));
 

	
 
	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;
 

	
 
	return new OSXStringIterator();
 
	return std::make_unique<OSXStringIterator>();
 
}
src/os/macosx/string_osx.h
Show inline comments
 
@@ -30,13 +30,13 @@ class OSXStringIterator : public StringI
 
public:
 
	void SetString(const char *s) override;
 
	size_t SetCurPosition(size_t pos) override;
 
	size_t Next(IterType what) override;
 
	size_t Prev(IterType what) override;
 

	
 
	static StringIterator *Create();
 
	static std::unique_ptr<StringIterator> Create();
 
};
 

	
 
/**
 
 * Helper class to construct a new #CoreTextParagraphLayout.
 
 */
 
class CoreTextParagraphLayoutFactory {
src/string.cpp
Show inline comments
 
@@ -762,15 +762,15 @@ int strnatcmp(const char *s1, const char
 
	/* Do a normal comparison if ICU is missing or if we cannot create a collator. */
 
	return strcasecmp(s1, s2);
 
}
 

	
 
#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)
 

	
 
#include <unicode/utext.h>
 
#include <unicode/brkiter.h>
 
@@ -918,15 +918,15 @@ public:
 
		}
 

	
 
		return pos == icu::BreakIterator::DONE ? END : this->utf16_to_utf8[pos];
 
	}
 
};
 

	
 
/* static */ StringIterator *StringIterator::Create()
 
/* static */ std::unique_ptr<StringIterator> StringIterator::Create()
 
{
 
	return new IcuStringIterator();
 
	return std::make_unique<IcuStringIterator>();
 
}
 

	
 
#else
 

	
 
/** Fallback simple string iterator. */
 
class DefaultStringIterator : public StringIterator
 
@@ -1029,21 +1029,21 @@ public:
 

	
 
		return END;
 
	}
 
};
 

	
 
#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;
 

	
 
	return new DefaultStringIterator();
 
	return std::make_unique<DefaultStringIterator>();
 
}
 
#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
src/string_base.h
Show inline comments
 
@@ -23,13 +23,13 @@ public:
 
	static const size_t END = SIZE_MAX;
 

	
 
	/**
 
	 * Create a new iterator instance.
 
	 * @return New iterator instance.
 
	 */
 
	static StringIterator *Create();
 
	static std::unique_ptr<StringIterator> Create();
 

	
 
	virtual ~StringIterator() {}
 

	
 
	/**
 
	 * Set a new iteration string. Must also be called if the string contents
 
	 * changed. The cursor is reset to the start of the string.
src/textbuf.cpp
Show inline comments
 
@@ -366,29 +366,26 @@ bool Textbuf::MovePos(uint16 keycode)
 
 * Initialize the textbuffer by supplying it the buffer to write into
 
 * and the maximum length of this buffer
 
 * @param max_bytes maximum size in bytes, including terminating '\0'
 
 * @param max_chars maximum size in chars, including terminating '\0'
 
 */
 
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_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;
 
	this->caret      = true;
 
	this->DeleteAll();
 
}
 

	
 
Textbuf::~Textbuf()
 
{
 
	delete this->char_iter;
 
	free(this->buf);
 
}
 

	
 
/**
 
 * Render a string into the textbuffer.
 
 * @param string String
src/textbuf_type.h
Show inline comments
 
@@ -64,13 +64,13 @@ struct Textbuf {
 
	bool HandleCaret();
 
	void UpdateSize();
 

	
 
	void DiscardMarkedText(bool update = true);
 

	
 
private:
 
	StringIterator *char_iter;
 
	std::unique_ptr<StringIterator> char_iter;
 

	
 
	bool CanDelChar(bool backspace);
 

	
 
	void DeleteText(uint16 from, uint16 to, bool update);
 

	
 
	void UpdateStringIter();
0 comments (0 inline, 0 general)