1 // Scintilla source code edit control
2 /** @file UniqueString.cxx
3 ** Define an allocator for UniqueString.
5 // Copyright 2017 by Neil Hodgson <neilh@scintilla.org>
6 // The License.txt file describes the conditions under which this software may be distributed.
13 #include "UniqueString.h"
17 /// Equivalent to strdup but produces a std::unique_ptr<const char[]> allocation to go
19 UniqueString
UniqueStringCopy(const char *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() {
37 void UniqueStringSet::Clear() noexcept
{
41 const char *UniqueStringSet::Save(const char *text
) {
45 for (const UniqueString
&us
: strings
) {
46 if (text
== us
.get()) {
51 strings
.push_back(UniqueStringCopy(text
));
52 return strings
.back().get();