1 //===-- StringTableBuilder.h - String table building utility ------*- C++ -*-=//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_MC_STRINGTABLE_BUILDER_H
11 #define LLVM_MC_STRINGTABLE_BUILDER_H
13 #include "llvm/ADT/SmallString.h"
14 #include "llvm/ADT/StringMap.h"
19 /// \brief Utility for building string tables with deduplicated suffixes.
20 class StringTableBuilder {
21 SmallString<256> StringTable;
22 StringMap<size_t> StringIndexMap;
25 /// \brief Add a string to the builder. Returns a StringRef to the internal
26 /// copy of s. Can only be used before the table is finalized.
27 StringRef add(StringRef s) {
28 assert(!isFinalized());
29 return StringIndexMap.GetOrCreateValue(s, 0).getKey();
32 /// \brief Analyze the strings and build the final table. No more strings can
33 /// be added after this point.
36 /// \brief Retrieve the string table data. Can only be used after the table
39 assert(isFinalized());
43 /// \brief Get the offest of a string in the string table. Can only be used
44 /// after the table is finalized.
45 size_t getOffset(StringRef s) {
46 assert(isFinalized());
47 assert(StringIndexMap.count(s) && "String is not in table!");
48 return StringIndexMap[s];
53 return !StringTable.empty();
57 } // end llvm namespace