Skip to content

Commit 0e68568

Browse files
committed
abstract-parser: skip leading spaces in the input
... when detecting the input data format. None of the supported formats treats leading spaces as significant.
1 parent ace155d commit 0e68568

File tree

7 files changed

+2137
-6
lines changed

7 files changed

+2137
-6
lines changed

src/abstract-parser.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626

2727
AbstractParser* createParser(InStream &input)
2828
{
29-
// sniff the first two chars from the input
30-
InStreamLookAhead head(input, 2U);
29+
// skip all white-spaces and sniff the first two chars from the input
30+
InStreamLookAhead head(input, 2U, /* skipWhiteSpaces */ true);
3131

3232
switch (head[0]) {
3333
case '{':

src/instream.cc

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,13 +62,25 @@ void InStream::handleError(const std::string msg, const long line)
6262
std::cerr << ": error: " << msg << "\n";
6363
}
6464

65-
InStreamLookAhead::InStreamLookAhead(InStream &input, const unsigned size)
65+
InStreamLookAhead::InStreamLookAhead(
66+
InStream &input,
67+
const unsigned size,
68+
bool skipWhiteSpaces)
6669
{
6770
std::istream &inStr = input.str();
6871

6972
// read `size` chars from input
70-
while (buf_.size() < size)
71-
buf_.push_back(inStr.get());
73+
while (buf_.size() < size) {
74+
const int c = inStr.get();
75+
if (skipWhiteSpaces && isspace(c) && !!inStr)
76+
// skip a white-space
77+
continue;
78+
79+
// only the leading white-spaces are skipped
80+
skipWhiteSpaces = false;
81+
82+
buf_.push_back(c);
83+
}
7284

7385
// put the chars back to the input stream
7486
for (auto it = buf_.rbegin(); it != buf_.rend(); ++it)

src/instream.hh

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,10 @@ class InStream {
5959

6060
class InStreamLookAhead {
6161
public:
62-
InStreamLookAhead(InStream &, unsigned size);
62+
InStreamLookAhead(
63+
InStream &inStr,
64+
unsigned size,
65+
bool skipWhiteSpaces = false);
6366

6467
char operator[](const unsigned idx) const {
6568
return buf_.at(idx);

tests/csgrep/74-coverity-leading-space-args.txt

Whitespace-only changes.

tests/csgrep/74-coverity-leading-space-stdin.txt

Lines changed: 1068 additions & 0 deletions
Large diffs are not rendered by default.

tests/csgrep/74-coverity-leading-space-stdout.txt

Lines changed: 1047 additions & 0 deletions
Large diffs are not rendered by default.

tests/csgrep/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,4 @@ test_csgrep("70-csparser-included-from-note" )
117117
test_csgrep("71-csgrep-drop-scan-props" )
118118
test_csgrep("72-abstract-parser-gcc-builtin" )
119119
test_csgrep("73-coverity-misra-checkers" )
120+
test_csgrep("74-coverity-leading-space" )

0 commit comments

Comments
 (0)