summaryrefslogtreecommitdiff
path: root/lib/Support
diff options
context:
space:
mode:
authorLenny Maiorani <lenny@colorado.edu>2011-04-15 17:56:50 +0000
committerLenny Maiorani <lenny@colorado.edu>2011-04-15 17:56:50 +0000
commit437ef0cfb74102208d6e96ff7e7f1204835e66f3 (patch)
tree54e728995aa94004e9168115304357cd2b0ed00c /lib/Support
parent44b7ae2355a32035ea286555736d173755a1c5e2 (diff)
downloadllvm-437ef0cfb74102208d6e96ff7e7f1204835e66f3.tar.gz
llvm-437ef0cfb74102208d6e96ff7e7f1204835e66f3.tar.bz2
llvm-437ef0cfb74102208d6e96ff7e7f1204835e66f3.tar.xz
Implements StringRef::compare with bounds. It is behaves similarly to strncmp(). Unit tests also included.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@129582 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Support')
-rw-r--r--lib/Support/StringRef.cpp21
1 files changed, 21 insertions, 0 deletions
diff --git a/lib/Support/StringRef.cpp b/lib/Support/StringRef.cpp
index 8c3fc094cd..066e7743ae 100644
--- a/lib/Support/StringRef.cpp
+++ b/lib/Support/StringRef.cpp
@@ -29,6 +29,27 @@ static bool ascii_isdigit(char x) {
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) {