Skip to content

Commit

Permalink
Merge pull request #43 from fbudin69500/is_between_single_quotes
Browse files Browse the repository at this point in the history
BUG: KWStyle was not checking if string was between single quotes
  • Loading branch information
thewtex committed Nov 8, 2016
2 parents 0fc7fd8 + 3a8b321 commit 0620fdd
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 9 deletions.
34 changes: 26 additions & 8 deletions kwsParser.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1475,8 +1475,26 @@ bool Parser::IsValidQuote(std::string & stream,size_t pos) const
return false;
}

/** return true if the position pos is between ' ' */
bool Parser::IsBetweenSingleQuote(size_t pos,bool withComments,std::string buffer) const
{
return this->IsBetweenQuoteChar(pos,withComments,buffer,'\'');
}

/** return true if the position pos is between " " */
bool Parser::IsBetweenDoubleQuote(size_t pos,bool withComments,std::string buffer) const
{
return this->IsBetweenQuoteChar(pos,withComments,buffer,'"');
}

/** return true if the position pos is between " " or ' ' */
bool Parser::IsBetweenQuote(size_t pos,bool withComments,std::string buffer) const
{
return (this->IsBetweenQuoteChar(pos,withComments,buffer,'\'') || this->IsBetweenQuoteChar(pos,withComments,buffer,'"'));
}

/** return true if the position pos is between the selected quoteChar (should be ' or ") */
bool Parser::IsBetweenQuoteChar(size_t pos,bool withComments,std::string buffer, char quoteChar) const
{
std::string stream = buffer;

Expand All @@ -1495,16 +1513,16 @@ bool Parser::IsBetweenQuote(size_t pos,bool withComments,std::string buffer) con
}

// We don't want to check for \" otherwise it fails
size_t b0 = stream.find('"',0);
size_t b0 = stream.find(quoteChar,0);
while((b0!=std::string::npos) && !this->IsValidQuote(stream,b0))
{
b0 = stream.find('"',b0+1);
b0 = stream.find(quoteChar,b0+1);
}

size_t b1 = stream.find('"',b0+1);
size_t b1 = stream.find(quoteChar,b0+1);
while((b1!=std::string::npos) && !this->IsValidQuote(stream,b1))
{
b1 = stream.find('"',b1+1);
b1 = stream.find(quoteChar,b1+1);
}

while(b0 != std::string::npos && b1 != std::string::npos && b1>b0)
Expand All @@ -1513,16 +1531,16 @@ bool Parser::IsBetweenQuote(size_t pos,bool withComments,std::string buffer) con
{
return true;
}
b0 = stream.find('"',b1+1);
b0 = stream.find(quoteChar,b1+1);
while((b0!=std::string::npos) && !this->IsValidQuote(stream,b0))
{
b0 = stream.find('"',b0+1);
b0 = stream.find(quoteChar,b0+1);
}

b1 = stream.find('"',b0+1);
b1 = stream.find(quoteChar,b0+1);
while((b1!=std::string::npos) && !this->IsValidQuote(stream,b1))
{
b1 = stream.find('"',b1+1);
b1 = stream.find(quoteChar,b1+1);
}
}
return false;
Expand Down
11 changes: 10 additions & 1 deletion kwsParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -333,9 +333,15 @@ class Parser
void SetFixFile(bool fix) {m_FixFile = fix;}
void GenerateFixedFile();

/** return true if the position pos is between " " */
/** return true if the position pos is between " " or ' ' */
bool IsBetweenQuote(size_t pos,bool withComments=false,std::string buffer="") const;

/** return true if the position pos is between ' ' */
bool IsBetweenSingleQuote(size_t pos,bool withComments=false,std::string buffer="") const;

/** return true if the position pos is between " " */
bool IsBetweenDoubleQuote(size_t pos,bool withComments=false,std::string buffer="") const;

protected:

/** Convert the file with CR+LF instead of LF */
Expand Down Expand Up @@ -400,6 +406,9 @@ class Parser
bool IsBetweenCharsFast(const char begin, const char end, size_t pos,bool withComments=false,std::string buffer="") const;
bool IsBetweenChars(const char begin, const char end, size_t pos,bool withComments=false,std::string buffer="") const;

/** Return true if the position pos is between the selected quote character (' or ") */
bool IsBetweenQuoteChar(size_t pos,bool withComments,std::string buffer, char quoteChar) const;

bool IsValidQuote(std::string & stream,size_t pos) const;

/** Removes ass CtrlN characters from the buffer. */
Expand Down

0 comments on commit 0620fdd

Please sign in to comment.