From 3711b7adccc766fdbafbc6b22c2bb6c45181a3b9 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sun, 20 Sep 2009 22:42:44 +0000 Subject: [PATCH] rewrite CountNumNewlinesBetween to be in terms of StringRef. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@82410 91177308-0d34-0410-b5e6-96231b3b80d8 --- utils/FileCheck/FileCheck.cpp | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/utils/FileCheck/FileCheck.cpp b/utils/FileCheck/FileCheck.cpp index b429f5ddf81..8f48c3a0cf3 100644 --- a/utils/FileCheck/FileCheck.cpp +++ b/utils/FileCheck/FileCheck.cpp @@ -241,23 +241,24 @@ static void PrintCheckFailed(const SourceMgr &SM, const CheckString &CheckStr, "note"); } -static unsigned CountNumNewlinesBetween(const char *Start, const char *End) { +/// CountNumNewlinesBetween - Count the number of newlines in the specified +/// range. +static unsigned CountNumNewlinesBetween(StringRef Range) { unsigned NumNewLines = 0; - for (; Start != End; ++Start) { + while (1) { // Scan for newline. - if (Start[0] != '\n' && Start[0] != '\r') - continue; + Range = Range.substr(Range.find_first_of("\n\r")); + if (Range.empty()) return NumNewLines; ++NumNewLines; // Handle \n\r and \r\n as a single newline. - if (Start+1 != End && - (Start[0] == '\n' || Start[0] == '\r') && - (Start[0] != Start[1])) - ++Start; + if (Range.size() > 1 && + (Range[1] == '\n' || Range[1] == '\r') && + (Range[0] != Range[1])) + Range = Range.substr(1); + Range = Range.substr(1); } - - return NumNewLines; } int main(int argc, char **argv) { @@ -311,7 +312,9 @@ int main(int argc, char **argv) { PrintCheckFailed(SM, CheckStr, SearchFrom); return 1; } - + + StringRef SkippedRegion(LastMatch, Buffer.data()-LastMatch); + // If this check is a "CHECK-NEXT", verify that the previous match was on // the previous line (i.e. that there is one newline between them). if (CheckStr.IsCheckNext) { @@ -319,7 +322,7 @@ int main(int argc, char **argv) { assert(LastMatch != F->getBufferStart() && "CHECK-NEXT can't be the first check in a file"); - unsigned NumNewLines = CountNumNewlinesBetween(LastMatch, Buffer.data()); + unsigned NumNewLines = CountNumNewlinesBetween(SkippedRegion); if (NumNewLines == 0) { SM.PrintMessage(CheckStr.Loc, CheckPrefix+"-NEXT: is on the same line as previous match", @@ -346,7 +349,6 @@ int main(int argc, char **argv) { // If this match had "not strings", verify that they don't exist in the // skipped region. - StringRef SkippedRegion(LastMatch, Buffer.data()-LastMatch); for (unsigned i = 0, e = CheckStr.NotStrings.size(); i != e; ++i) { size_t Pos = SkippedRegion.find(CheckStr.NotStrings[i].second); if (Pos == StringRef::npos) continue; -- 2.34.1