[llvm] r218380 - Replace a hand-written suffix compare with std::lexicographical_compare.
Benjamin Kramer
benny.kra at googlemail.com
Wed Sep 24 06:19:29 PDT 2014
Author: d0k
Date: Wed Sep 24 08:19:28 2014
New Revision: 218380
URL: http://llvm.org/viewvc/llvm-project?rev=218380&view=rev
Log:
Replace a hand-written suffix compare with std::lexicographical_compare.
No functionality change.
Modified:
llvm/trunk/lib/MC/StringTableBuilder.cpp
Modified: llvm/trunk/lib/MC/StringTableBuilder.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/MC/StringTableBuilder.cpp?rev=218380&r1=218379&r2=218380&view=diff
==============================================================================
--- llvm/trunk/lib/MC/StringTableBuilder.cpp (original)
+++ llvm/trunk/lib/MC/StringTableBuilder.cpp Wed Sep 24 08:19:28 2014
@@ -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<StringRef, 8> 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<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.
More information about the llvm-commits
mailing list