llvm.org GIT mirror llvm / 7bee327
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 Chris Lattner 10 years ago
1 changed file(s) with 29 addition(s) and 21 deletion(s). Raw diff Collapse all Expand all
5050 };
5151
5252
53 /// FindStringInBuffer - This is basically just a strstr wrapper that differs in
54 /// two ways: first it handles 'nul' characters in memory buffers, second, it
55 /// returns the end of the memory buffer on match failure.
56 static const char *FindStringInBuffer(const char *Str, const char *CurPtr,
57 const MemoryBuffer &MB) {
58 // Check to see if we have a match. If so, just return it.
59 if (const char *Res = strstr(CurPtr, Str))
60 return Res;
61
62 // If not, check to make sure we didn't just find an embedded nul in the
63 // memory buffer.
64 const char *Ptr = CurPtr + strlen(CurPtr);
65
66 // If we really reached the end of the file, return it.
67 if (Ptr == MB.getBufferEnd())
68 return Ptr;
69
70 // Otherwise, just skip this section of the file, including the nul.
71 return FindStringInBuffer(Str, Ptr+1, MB);
53 /// FindFixedStringInBuffer - This works like strstr, except for two things:
54 /// 1) it handles 'nul' characters in memory buffers. 2) it returns the end of
55 /// the memory buffer on match failure instead of null.
56 static const char *FindFixedStringInBuffer(StringRef Str, const char *CurPtr,
57 const MemoryBuffer &MB) {
58 assert(!Str.empty() && "Can't find an empty string");
59 const char *BufEnd = MB.getBufferEnd();
60
61 while (1) {
62 // Scan for the first character in the match string.
63 CurPtr = (char*)memchr(CurPtr, Str[0], BufEnd-CurPtr);
64
65 // If we didn't find the first character of the string, then we failed to
66 // match.
67 if (CurPtr == 0) return BufEnd;
68
69 // If the match string is one character, then we win.
70 if (Str.size() == 1) return CurPtr;
71
72 // Otherwise, verify that the rest of the string matches.
73 if (Str.size() <= unsigned(BufEnd-CurPtr) &&
74 memcmp(CurPtr+1, Str.data()+1, Str.size()-1) == 0)
75 return CurPtr;
76
77 // If not, advance past this character and try again.
78 ++CurPtr;
79 }
7280 }
7381
7482 /// ReadCheckFile - Read the check file, which specifies the sequence of
94102
95103 while (1) {
96104 // See if Prefix occurs in the memory buffer.
97 const char *Ptr = FindStringInBuffer(Prefix.c_str(), CurPtr, *F);
105 const char *Ptr = FindFixedStringInBuffer(Prefix, CurPtr, *F);
98106
99107 // If we didn't find a match, we're done.
100108 if (Ptr == BufferEnd)
230238 const CheckString &CheckStr = CheckStrings[StrNo];
231239
232240 // Find StrNo in the file.
233 const char *Ptr = FindStringInBuffer(CheckStr.Str.c_str(), CurPtr, *F);
241 const char *Ptr = FindFixedStringInBuffer(CheckStr.Str, CurPtr, *F);
234242
235243 // If we found a match, we're done, move on.
236244 if (Ptr != BufferEnd) {