File indexing completed on 2026-05-10 08:43:10
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef LLVM_ADT_STRING_TABLE_H
0010 #define LLVM_ADT_STRING_TABLE_H
0011
0012 #include "llvm/ADT/StringRef.h"
0013 #include "llvm/ADT/iterator.h"
0014 #include <iterator>
0015 #include <limits>
0016
0017 namespace llvm {
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033 class StringTable {
0034 StringRef Table;
0035
0036 public:
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048 class Offset {
0049
0050 unsigned Value = 0;
0051
0052 public:
0053 constexpr Offset() = default;
0054 constexpr Offset(unsigned Value) : Value(Value) {}
0055
0056 friend constexpr bool operator==(const Offset &LHS, const Offset &RHS) {
0057 return LHS.Value == RHS.Value;
0058 }
0059
0060 friend constexpr bool operator!=(const Offset &LHS, const Offset &RHS) {
0061 return LHS.Value != RHS.Value;
0062 }
0063
0064 constexpr unsigned value() const { return Value; }
0065 };
0066
0067
0068
0069
0070
0071 template <size_t N>
0072 constexpr StringTable(const char (&RawTable)[N]) : Table(RawTable, N) {
0073 static_assert(N <= std::numeric_limits<unsigned>::max(),
0074 "We only support table sizes that can be indexed by an "
0075 "`unsigned` offset.");
0076
0077
0078
0079 assert(!Table.empty() && "Requires at least a valid empty string.");
0080 assert(Table.data()[0] == '\0' && "Offset zero must be the empty string.");
0081
0082
0083
0084 assert(Table.data()[Table.size() - 1] == '\0' &&
0085 "Last byte must be a null byte.");
0086 }
0087
0088
0089
0090
0091 constexpr StringRef operator[](Offset O) const {
0092 assert(O.value() < Table.size() && "Out of bounds offset!");
0093 return Table.data() + O.value();
0094 }
0095
0096
0097 constexpr size_t size() const { return Table.size(); }
0098
0099 class Iterator
0100 : public iterator_facade_base<Iterator, std::forward_iterator_tag,
0101 const StringRef> {
0102 friend StringTable;
0103
0104 const StringTable *Table;
0105 Offset O;
0106
0107
0108 mutable StringRef S;
0109
0110 explicit constexpr Iterator(const StringTable &Table, Offset O)
0111 : Table(&Table), O(O) {}
0112
0113 public:
0114 constexpr Iterator(const Iterator &RHS) = default;
0115 constexpr Iterator(Iterator &&RHS) = default;
0116
0117 bool operator==(const Iterator &RHS) const {
0118 assert(Table == RHS.Table && "Compared iterators for unrelated tables!");
0119 return O == RHS.O;
0120 }
0121
0122 const StringRef &operator*() const {
0123 S = (*Table)[O];
0124 return S;
0125 }
0126
0127 Iterator &operator++() {
0128 O = O.value() + (*Table)[O].size() + 1;
0129 return *this;
0130 }
0131 };
0132
0133 constexpr Iterator begin() const { return Iterator(*this, 0); }
0134 constexpr Iterator end() const { return Iterator(*this, size() - 1); }
0135 };
0136
0137 }
0138
0139 #endif