|
10 | 10 | #ifndef LLVM_SUPPORT_STRINGSAVER_H
|
11 | 11 | #define LLVM_SUPPORT_STRINGSAVER_H
|
12 | 12 |
|
| 13 | +#include "llvm/ADT/DenseSet.h" |
13 | 14 | #include "llvm/ADT/StringRef.h"
|
14 | 15 | #include "llvm/ADT/Twine.h"
|
15 | 16 | #include "llvm/Support/Allocator.h"
|
16 | 17 |
|
17 | 18 | namespace llvm {
|
18 | 19 |
|
19 |
| -/// Saves strings in the inheritor's stable storage and returns a |
| 20 | +/// Saves strings in the provided stable storage and returns a |
20 | 21 | /// StringRef with a stable character pointer.
|
21 | 22 | class StringSaver final {
|
22 | 23 | BumpPtrAllocator &Alloc;
|
23 | 24 |
|
24 | 25 | public:
|
25 | 26 | StringSaver(BumpPtrAllocator &Alloc) : Alloc(Alloc) {}
|
| 27 | + |
| 28 | + // All returned strings are null-terminated: *save(S).end() == 0. |
26 | 29 | StringRef save(const char *S) { return save(StringRef(S)); }
|
27 | 30 | StringRef save(StringRef S);
|
28 | 31 | StringRef save(const Twine &S) { return save(StringRef(S.str())); }
|
29 | 32 | StringRef save(const std::string &S) { return save(StringRef(S)); }
|
30 | 33 | };
|
| 34 | + |
| 35 | +/// Saves strings in the provided stable storage and returns a StringRef with a |
| 36 | +/// stable character pointer. Saving the same string yields the same StringRef. |
| 37 | +/// |
| 38 | +/// Compared to StringSaver, it does more work but avoids saving the same string |
| 39 | +/// multiple times. |
| 40 | +/// |
| 41 | +/// Compared to StringPool, it performs fewer allocations but doesn't support |
| 42 | +/// refcounting/deletion. |
| 43 | +class UniqueStringSaver final { |
| 44 | + StringSaver Strings; |
| 45 | + llvm::DenseSet<llvm::StringRef> Unique; |
| 46 | + |
| 47 | +public: |
| 48 | + UniqueStringSaver(BumpPtrAllocator &Alloc) : Strings(Alloc) {} |
| 49 | + |
| 50 | + // All returned strings are null-terminated: *save(S).end() == 0. |
| 51 | + StringRef save(const char *S) { return save(StringRef(S)); } |
| 52 | + StringRef save(StringRef S); |
| 53 | + StringRef save(const Twine &S) { return save(StringRef(S.str())); } |
| 54 | + StringRef save(const std::string &S) { return save(StringRef(S)); } |
| 55 | +}; |
| 56 | + |
31 | 57 | }
|
32 | 58 | #endif
|
0 commit comments