[llvm-commits] [llvm] r129582 - in /llvm/trunk: include/llvm/ADT/StringRef.h lib/Support/StringRef.cpp unittests/ADT/StringRefTest.cpp
Lenny Maiorani
lenny at colorado.edu
Fri Apr 15 10:56:50 PDT 2011
Author: lenny
Date: Fri Apr 15 12:56:50 2011
New Revision: 129582
URL: http://llvm.org/viewvc/llvm-project?rev=129582&view=rev
Log:
Implements StringRef::compare with bounds. It is behaves similarly to strncmp(). Unit tests also included.
Modified:
llvm/trunk/include/llvm/ADT/StringRef.h
llvm/trunk/lib/Support/StringRef.cpp
llvm/trunk/unittests/ADT/StringRefTest.cpp
Modified: llvm/trunk/include/llvm/ADT/StringRef.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/StringRef.h?rev=129582&r1=129581&r2=129582&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/StringRef.h (original)
+++ llvm/trunk/include/llvm/ADT/StringRef.h Fri Apr 15 12:56:50 2011
@@ -125,6 +125,12 @@
return Length < RHS.Length ? -1 : 1;
}
+ /// compare - Compare two strings; the result is -1, 0, or 1 if this string
+ /// is lexicographically less than, equal to, or greater than the \arg RHS.
+ /// This is different than compare with no size specified as it only
+ /// compares at most the first n bytes.
+ int compare(StringRef RHS, size_t n) const;
+
/// compare_lower - Compare two strings, ignoring case.
int compare_lower(StringRef RHS) const;
Modified: llvm/trunk/lib/Support/StringRef.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/StringRef.cpp?rev=129582&r1=129581&r2=129582&view=diff
==============================================================================
--- llvm/trunk/lib/Support/StringRef.cpp (original)
+++ llvm/trunk/lib/Support/StringRef.cpp Fri Apr 15 12:56:50 2011
@@ -29,6 +29,27 @@
return x >= '0' && x <= '9';
}
+/// compare - Compare two strings; the result is -1, 0, or 1 if this string
+/// is lexicographically less than, equal to, or greater than the \arg RHS.
+/// This is different than compare with no size specified as it only
+/// compares at most the first n bytes.
+int StringRef::compare(StringRef RHS, size_t n) const {
+ // Check the prefix for a mismatch.
+ size_t maxToCmp = min(Length, RHS.Length);
+ maxToCmp = min(maxToCmp, n);
+ if (int Res = memcmp(Data, RHS.Data, maxToCmp))
+ return Res < 0 ? -1 : 1;
+
+ // Otherwise the prefixes match, so we only need to check the lengths.
+ // Be mindful that if the n is less than or equal to the length of either
+ // string, that is the same as the strings matching because in that case
+ // we only care about the prefix.
+ if (((n <= Length) && (n <= RHS.Length)) ||
+ (Length == RHS.Length))
+ return 0;
+ return Length < RHS.Length ? -1 : 1;
+}
+
/// compare_lower - Compare strings, ignoring case.
int StringRef::compare_lower(StringRef RHS) const {
for (size_t I = 0, E = min(Length, RHS.Length); I != E; ++I) {
Modified: llvm/trunk/unittests/ADT/StringRefTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/StringRefTest.cpp?rev=129582&r1=129581&r2=129582&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/StringRefTest.cpp (original)
+++ llvm/trunk/unittests/ADT/StringRefTest.cpp Fri Apr 15 12:56:50 2011
@@ -55,6 +55,19 @@
EXPECT_EQ( 1, StringRef("aab").compare("aa"));
EXPECT_EQ( 1, StringRef("\xFF").compare("\1"));
+ EXPECT_EQ(-1, StringRef("aab").compare("aad", 3));
+ EXPECT_EQ( 0, StringRef("aab").compare("aab", 3));
+ EXPECT_EQ( 1, StringRef("aab").compare("aaa", 3));
+ EXPECT_EQ(-1, StringRef("aab").compare("aabb", 4));
+ EXPECT_EQ( 1, StringRef("aab").compare("aa", 3));
+ EXPECT_EQ( 1, StringRef("\xFF").compare("\1", 3));
+ EXPECT_EQ( 0, StringRef("aab").compare("aad", 2));
+ EXPECT_EQ( 0, StringRef("aab").compare("aab", 2));
+ EXPECT_EQ( 0, StringRef("aab").compare("aab", 4));
+ EXPECT_EQ( 0, StringRef("aab").compare("aaa", 2));
+ EXPECT_EQ( 0, StringRef("aab").compare("aabb", 3));
+ EXPECT_EQ( 0, StringRef("aab").compare("aa", 2));
+
EXPECT_EQ(-1, StringRef("AaB").compare_lower("aAd"));
EXPECT_EQ( 0, StringRef("AaB").compare_lower("aab"));
EXPECT_EQ( 1, StringRef("AaB").compare_lower("AAA"));
More information about the llvm-commits
mailing list