diff options
author | Neil <nyamatongwe@gmail.com> | 2019-04-08 08:34:52 +1000 |
---|---|---|
committer | Neil <nyamatongwe@gmail.com> | 2019-04-08 08:34:52 +1000 |
commit | 4a44fd852450d9e97032633524f92d1fad5f7187 (patch) | |
tree | e7e99b2532a0766190cba557131431b05ec4ceb2 /src | |
parent | bdfbc5325157c037fdaa70d6c7fe757046f08906 (diff) | |
download | scintilla-mirror-4a44fd852450d9e97032633524f92d1fad5f7187.tar.gz |
Backport: Rename FontNames to UniqueStringSet and move into UniqueString.
It may be useful in more situations than just font names.
Backport of changeset 7409:a70a4ee51448.
Diffstat (limited to 'src')
-rw-r--r-- | src/UniqueString.cxx | 27 | ||||
-rw-r--r-- | src/UniqueString.h | 20 | ||||
-rw-r--r-- | src/ViewStyle.cxx | 26 | ||||
-rw-r--r-- | src/ViewStyle.h | 17 |
4 files changed, 49 insertions, 41 deletions
diff --git a/src/UniqueString.cxx b/src/UniqueString.cxx index dcbdc441c..aadc2ae7e 100644 --- a/src/UniqueString.cxx +++ b/src/UniqueString.cxx @@ -6,6 +6,7 @@ // The License.txt file describes the conditions under which this software may be distributed. #include <cstring> +#include <vector> #include <algorithm> #include <memory> @@ -25,4 +26,30 @@ UniqueString UniqueStringCopy(const char *text) { return UniqueString(upcNew.release()); } +// A set of strings that always returns the same pointer for each string. + +UniqueStringSet::UniqueStringSet() noexcept = default; + +UniqueStringSet::~UniqueStringSet() { + strings.clear(); +} + +void UniqueStringSet::Clear() noexcept { + strings.clear(); +} + +const char *UniqueStringSet::Save(const char *text) { + if (!text) + return nullptr; + + for (const UniqueString &us : strings) { + if (text == us.get()) { + return us.get(); + } + } + + strings.push_back(UniqueStringCopy(text)); + return strings.back().get(); +} + } diff --git a/src/UniqueString.h b/src/UniqueString.h index 44ba26652..f7f7ebbdc 100644 --- a/src/UniqueString.h +++ b/src/UniqueString.h @@ -2,6 +2,8 @@ /** @file UniqueString.h ** Define UniqueString, a unique_ptr based string type for storage in containers ** and an allocator for UniqueString. + ** Define UniqueStringSet which holds a set of strings, used to avoid holding many copies + ** of font names. **/ // Copyright 2017 by Neil Hodgson <neilh@scintilla.org> // The License.txt file describes the conditions under which this software may be distributed. @@ -21,6 +23,24 @@ using UniqueString = std::unique_ptr<const char[]>; /// into collections. UniqueString UniqueStringCopy(const char *text); +// A set of strings that always returns the same pointer for each string. + +class UniqueStringSet { +private: + std::vector<UniqueString> strings; +public: + UniqueStringSet() noexcept; + // UniqueStringSet objects can not be copied. + UniqueStringSet(const UniqueStringSet &) = delete; + UniqueStringSet &operator=(const UniqueStringSet &) = delete; + // UniqueStringSet objects can be moved. + UniqueStringSet(UniqueStringSet &&) = default; + UniqueStringSet &operator=(UniqueStringSet &&) = default; + ~UniqueStringSet(); + void Clear() noexcept; + const char *Save(const char *text); +}; + } #endif diff --git a/src/ViewStyle.cxx b/src/ViewStyle.cxx index 54f20de4b..d3f225b98 100644 --- a/src/ViewStyle.cxx +++ b/src/ViewStyle.cxx @@ -32,32 +32,6 @@ MarginStyle::MarginStyle(int style_, int width_, int mask_) : style(style_), width(width_), mask(mask_), sensitive(false), cursor(SC_CURSORREVERSEARROW) { } -// A list of the fontnames - avoids wasting space in each style -FontNames::FontNames() { -} - -FontNames::~FontNames() { - Clear(); -} - -void FontNames::Clear() { - names.clear(); -} - -const char *FontNames::Save(const char *name) { - if (!name) - return nullptr; - - for (const UniqueString &nm : names) { - if (strcmp(nm.get(), name) == 0) { - return nm.get(); - } - } - - names.push_back(UniqueStringCopy(name)); - return names.back().get(); -} - FontRealised::FontRealised() { } diff --git a/src/ViewStyle.h b/src/ViewStyle.h index ba0524d86..0fb3db093 100644 --- a/src/ViewStyle.h +++ b/src/ViewStyle.h @@ -25,20 +25,7 @@ public: /** */ -class FontNames { -private: - std::vector<UniqueString> names; -public: - FontNames(); - // FontNames objects can not be copied. - FontNames(const FontNames &) = delete; - FontNames(FontNames &&) = delete; - FontNames &operator=(const FontNames &) = delete; - FontNames &operator=(FontNames &&) = delete; - ~FontNames(); - void Clear(); - const char *Save(const char *name); -}; + class FontRealised : public FontMeasurements { public: @@ -91,7 +78,7 @@ struct EdgeProperties { /** */ class ViewStyle { - FontNames fontNames; + UniqueStringSet fontNames; FontMap fonts; public: std::vector<Style> styles; |