[llvm-commits] [llvm] r79120 - /llvm/trunk/utils/FileCheck/FileCheck.cpp
Chris Lattner
sabre at nondot.org
Sat Aug 15 10:53:12 PDT 2009
Author: lattner
Date: Sat Aug 15 12:53:12 2009
New Revision: 79120
URL: http://llvm.org/viewvc/llvm-project?rev=79120&view=rev
Log:
rewrite FindStringInBuffer to use an explicit loop instead of
trying to wrap strstr which is just too inconvenient. Make it
use a StringRef to avoid ".c_str()" calls.
Modified:
llvm/trunk/utils/FileCheck/FileCheck.cpp
Modified: llvm/trunk/utils/FileCheck/FileCheck.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/utils/FileCheck/FileCheck.cpp?rev=79120&r1=79119&r2=79120&view=diff
==============================================================================
--- llvm/trunk/utils/FileCheck/FileCheck.cpp (original)
+++ llvm/trunk/utils/FileCheck/FileCheck.cpp Sat Aug 15 12:53:12 2009
@@ -51,25 +51,33 @@
};
-/// FindStringInBuffer - This is basically just a strstr wrapper that differs in
-/// two ways: first it handles 'nul' characters in memory buffers, second, it
-/// returns the end of the memory buffer on match failure.
-static const char *FindStringInBuffer(const char *Str, const char *CurPtr,
- const MemoryBuffer &MB) {
- // Check to see if we have a match. If so, just return it.
- if (const char *Res = strstr(CurPtr, Str))
- return Res;
-
- // If not, check to make sure we didn't just find an embedded nul in the
- // memory buffer.
- const char *Ptr = CurPtr + strlen(CurPtr);
-
- // If we really reached the end of the file, return it.
- if (Ptr == MB.getBufferEnd())
- return Ptr;
+/// FindFixedStringInBuffer - This works like strstr, except for two things:
+/// 1) it handles 'nul' characters in memory buffers. 2) it returns the end of
+/// the memory buffer on match failure instead of null.
+static const char *FindFixedStringInBuffer(StringRef Str, const char *CurPtr,
+ const MemoryBuffer &MB) {
+ assert(!Str.empty() && "Can't find an empty string");
+ const char *BufEnd = MB.getBufferEnd();
+
+ while (1) {
+ // Scan for the first character in the match string.
+ CurPtr = (char*)memchr(CurPtr, Str[0], BufEnd-CurPtr);
- // Otherwise, just skip this section of the file, including the nul.
- return FindStringInBuffer(Str, Ptr+1, MB);
+ // If we didn't find the first character of the string, then we failed to
+ // match.
+ if (CurPtr == 0) return BufEnd;
+
+ // If the match string is one character, then we win.
+ if (Str.size() == 1) return CurPtr;
+
+ // Otherwise, verify that the rest of the string matches.
+ if (Str.size() <= unsigned(BufEnd-CurPtr) &&
+ memcmp(CurPtr+1, Str.data()+1, Str.size()-1) == 0)
+ return CurPtr;
+
+ // If not, advance past this character and try again.
+ ++CurPtr;
+ }
}
/// ReadCheckFile - Read the check file, which specifies the sequence of
@@ -95,7 +103,7 @@
while (1) {
// See if Prefix occurs in the memory buffer.
- const char *Ptr = FindStringInBuffer(Prefix.c_str(), CurPtr, *F);
+ const char *Ptr = FindFixedStringInBuffer(Prefix, CurPtr, *F);
// If we didn't find a match, we're done.
if (Ptr == BufferEnd)
@@ -231,7 +239,7 @@
const CheckString &CheckStr = CheckStrings[StrNo];
// Find StrNo in the file.
- const char *Ptr = FindStringInBuffer(CheckStr.Str.c_str(), CurPtr, *F);
+ const char *Ptr = FindFixedStringInBuffer(CheckStr.Str, CurPtr, *F);
// If we found a match, we're done, move on.
if (Ptr != BufferEnd) {
More information about the llvm-commits
mailing list