-// There is similar code in yaml2coff, but with some slight COFF-specific
-// variations like different initial state. Might be able to deduplicate
-// some day, but also want to make sure that the Mach-O use case is served.
-//
-// This class has a deliberately small interface, since a lot of
-// implementation variation is possible.
-//
-// TODO: Use the StringTable builder from lib/Object instead, since it
-// will deduplicate suffixes.
-namespace {
-class StringTableBuilder {
- /// \brief Indices of strings currently present in `Buf`.
- StringMap<unsigned> StringIndices;
- /// \brief The contents of the string table as we build it.
- std::string Buf;
-public:
- StringTableBuilder() {
- Buf.push_back('\0');
- }
- /// \returns Index of string in string table.
- unsigned addString(StringRef S) {
- StringMapEntry<unsigned> &Entry = StringIndices.GetOrCreateValue(S);
- unsigned &I = Entry.getValue();
- if (I != 0)
- return I;
- I = Buf.size();
- Buf.append(S.begin(), S.end());
- Buf.push_back('\0');
- return I;
- }
- size_t size() const {
- return Buf.size();
- }
- void writeToStream(raw_ostream &OS) {
- OS.write(Buf.data(), Buf.size());
- }
-};
-} // end anonymous namespace
-