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.



git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@79120 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2009-08-15 17:53:12 +00:00
parent 207e1bcf89
commit 7bee3271e8

View File

@ -51,25 +51,33 @@ struct CheckString {
}; };
/// FindStringInBuffer - This is basically just a strstr wrapper that differs in /// FindFixedStringInBuffer - This works like strstr, except for two things:
/// two ways: first it handles 'nul' characters in memory buffers, second, it /// 1) it handles 'nul' characters in memory buffers. 2) it returns the end of
/// returns the end of the memory buffer on match failure. /// the memory buffer on match failure instead of null.
static const char *FindStringInBuffer(const char *Str, const char *CurPtr, static const char *FindFixedStringInBuffer(StringRef Str, const char *CurPtr,
const MemoryBuffer &MB) { const MemoryBuffer &MB) {
// Check to see if we have a match. If so, just return it. assert(!Str.empty() && "Can't find an empty string");
if (const char *Res = strstr(CurPtr, Str)) const char *BufEnd = MB.getBufferEnd();
return Res;
// If not, check to make sure we didn't just find an embedded nul in the while (1) {
// memory buffer. // Scan for the first character in the match string.
const char *Ptr = CurPtr + strlen(CurPtr); CurPtr = (char*)memchr(CurPtr, Str[0], BufEnd-CurPtr);
// If we really reached the end of the file, return it. // If we didn't find the first character of the string, then we failed to
if (Ptr == MB.getBufferEnd()) // match.
return Ptr; if (CurPtr == 0) return BufEnd;
// Otherwise, just skip this section of the file, including the nul. // If the match string is one character, then we win.
return FindStringInBuffer(Str, Ptr+1, MB); 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 /// ReadCheckFile - Read the check file, which specifies the sequence of
@ -95,7 +103,7 @@ static bool ReadCheckFile(SourceMgr &SM,
while (1) { while (1) {
// See if Prefix occurs in the memory buffer. // 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 we didn't find a match, we're done.
if (Ptr == BufferEnd) if (Ptr == BufferEnd)
@ -231,7 +239,7 @@ int main(int argc, char **argv) {
const CheckString &CheckStr = CheckStrings[StrNo]; const CheckString &CheckStr = CheckStrings[StrNo];
// Find StrNo in the file. // 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 we found a match, we're done, move on.
if (Ptr != BufferEnd) { if (Ptr != BufferEnd) {