Update Scintilla to version 3.10.4
[geany-mirror.git] / scintilla / src / UniqueString.cxx
blobaadc2ae7e6f50b1b02109d5577ca0b78a24ab2b9
1 // Scintilla source code edit control
2 /** @file UniqueString.cxx
3 ** Define an allocator for UniqueString.
4 **/
5 // Copyright 2017 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
8 #include <cstring>
9 #include <vector>
10 #include <algorithm>
11 #include <memory>
13 #include "UniqueString.h"
15 namespace Scintilla {
17 /// Equivalent to strdup but produces a std::unique_ptr<const char[]> allocation to go
18 /// into collections.
19 UniqueString UniqueStringCopy(const char *text) {
20 if (!text) {
21 return UniqueString();
23 const size_t len = strlen(text);
24 std::unique_ptr<char[]> upcNew(new char[len + 1]);
25 memcpy(&upcNew[0], text, len + 1);
26 return UniqueString(upcNew.release());
29 // A set of strings that always returns the same pointer for each string.
31 UniqueStringSet::UniqueStringSet() noexcept = default;
33 UniqueStringSet::~UniqueStringSet() {
34 strings.clear();
37 void UniqueStringSet::Clear() noexcept {
38 strings.clear();
41 const char *UniqueStringSet::Save(const char *text) {
42 if (!text)
43 return nullptr;
45 for (const UniqueString &us : strings) {
46 if (text == us.get()) {
47 return us.get();
51 strings.push_back(UniqueStringCopy(text));
52 return strings.back().get();