llvm-6502/lib/MC/StringTableBuilder.cpp
Benjamin Kramer 7a27231780 Replace a hand-written suffix compare with std::lexicographical_compare.
No functionality change.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@218380 91177308-0d34-0410-b5e6-96231b3b80d8
2014-09-24 13:19:28 +00:00

44 lines
1.4 KiB
C++

//===-- StringTableBuilder.cpp - String table building utility ------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#include "llvm/MC/StringTableBuilder.h"
#include "llvm/ADT/SmallVector.h"
using namespace llvm;
void StringTableBuilder::finalize() {
SmallVector<StringRef, 8> Strings;
for (auto i = StringIndexMap.begin(), e = StringIndexMap.end(); i != e; ++i)
Strings.push_back(i->getKey());
// Sort the vector so a string is sorted above its suffixes.
std::sort(Strings.begin(), Strings.end(), [](StringRef A, StringRef B) {
typedef std::reverse_iterator<StringRef::iterator> Reverse;
return !std::lexicographical_compare(Reverse(A.end()), Reverse(A.begin()),
Reverse(B.end()), Reverse(B.begin()));
});
// FIXME: Starting with a null byte is ELF specific. Generalize this so we
// can use the class with other object formats.
StringTable += '\x00';
StringRef Previous;
for (StringRef s : Strings) {
if (Previous.endswith(s)) {
StringIndexMap[s] = StringTable.size() - 1 - s.size();
continue;
}
StringIndexMap[s] = StringTable.size();
StringTable += s;
StringTable += '\x00';
Previous = s;
}
}