blob: c4d616a108003d1d95c7e0b08874f3fb059811d8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
|
// Scintilla source code edit control
/** @file UniqueString.cxx
** Define an allocator for UniqueString.
**/
// Copyright 2017 by Neil Hodgson <neilh@scintilla.org>
// The License.txt file describes the conditions under which this software may be distributed.
#include <cstring>
#include <vector>
#include <algorithm>
#include <memory>
#include "UniqueString.h"
namespace Scintilla {
/// Equivalent to strdup but produces a std::unique_ptr<const char[]> allocation to go
/// into collections.
UniqueString UniqueStringCopy(const char *text) {
if (!text) {
return UniqueString();
}
const size_t len = strlen(text);
std::unique_ptr<char[]> upcNew = Sci::make_unique<char[]>(len + 1);
memcpy(upcNew.get(), text, len + 1);
return UniqueString(upcNew.release());
}
// A set of strings that always returns the same pointer for each string.
UniqueStringSet::UniqueStringSet() = 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();
}
}
|