From 7a272317801e46efd858b9ed9daf9aa7d99eae8c Mon Sep 17 00:00:00 2001 From: Benjamin Kramer Date: Wed, 24 Sep 2014 13:19:28 +0000 Subject: [PATCH] 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 --- lib/MC/StringTableBuilder.cpp | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/lib/MC/StringTableBuilder.cpp b/lib/MC/StringTableBuilder.cpp index db58ece5c86..2343845ee32 100644 --- a/lib/MC/StringTableBuilder.cpp +++ b/lib/MC/StringTableBuilder.cpp @@ -12,25 +12,17 @@ using namespace llvm; -static bool compareBySuffix(StringRef a, StringRef b) { - size_t sizeA = a.size(); - size_t sizeB = b.size(); - size_t len = std::min(sizeA, sizeB); - for (size_t i = 0; i < len; ++i) { - char ca = a[sizeA - i - 1]; - char cb = b[sizeB - i - 1]; - if (ca != cb) - return ca > cb; - } - return sizeA > sizeB; -} - void StringTableBuilder::finalize() { SmallVector Strings; for (auto i = StringIndexMap.begin(), e = StringIndexMap.end(); i != e; ++i) Strings.push_back(i->getKey()); - std::sort(Strings.begin(), Strings.end(), compareBySuffix); + // 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 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.