Skip to content

Commit 6e3b017

Browse files
committed
Strip trailing line comments when reading a file for attributes parsing
1 parent 7817c48 commit 6e3b017

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

src/attributes.cpp

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,13 @@ namespace attributes {
7474

7575
// Trim a string
7676
void trimWhitespace(std::string* pStr);
77+
78+
// Finds the first line comment (ie, comment started by "//");
79+
// returns std::string::npos if no match is found
80+
size_t findComment(std::string const& str, size_t idx);
81+
82+
// Strip trailing line comments
83+
void stripTrailingLineComments(std::string* pStr);
7784

7885
// Strip balanced quotes from around a string (assumes already trimmed)
7986
void stripQuotes(std::string* pStr);
@@ -854,6 +861,7 @@ namespace attributes {
854861
// strip \r (for the case of windows line terminators on posix)
855862
if (line.length() > 0 && *line.rbegin() == '\r')
856863
line.erase(line.length()-1, 1);
864+
stripTrailingLineComments(&line);
857865
lines.push_back(line);
858866
}
859867
lines_ = Rcpp::wrap(lines);
@@ -957,6 +965,7 @@ namespace attributes {
957965
// trim before we do this just in case someone updates the regex
958966
// to allow for whitespace around the call
959967
trimWhitespace(&paramsText);
968+
960969
paramsText = paramsText.substr(1, paramsText.size()-2);
961970

962971
// parse the parameters
@@ -1007,7 +1016,7 @@ namespace attributes {
10071016
const std::string& input) {
10081017

10091018
const std::string delimiters(" ,");
1010-
1019+
10111020
std::vector<Param> params;
10121021
std::string::size_type current;
10131022
std::string::size_type next = -1;
@@ -2343,6 +2352,42 @@ namespace attributes {
23432352
bool isWhitespace(char ch) {
23442353
return std::strchr(kWhitespaceChars, ch) != NULL;
23452354
}
2355+
2356+
// Finds the first line comment (ie, comment started by "//");
2357+
// returns std::string::npos if no match is found
2358+
size_t findComment(std::string const& str, size_t idx) {
2359+
size_t firstSlash = str.find("/", idx);
2360+
if (str[firstSlash + 1] == '/') return firstSlash;
2361+
else return std::string::npos;
2362+
}
2363+
2364+
// Remove trailing line comments -- ie, find comments that don't begin
2365+
// a line, and remove them. We avoid stripping attributes.
2366+
void stripTrailingLineComments(std::string* pStr) {
2367+
2368+
if (pStr->empty()) return;
2369+
2370+
size_t firstComment = findComment(*pStr, 0);
2371+
size_t firstNonWhitespaceChar = pStr->find_first_not_of(kWhitespaceChars);
2372+
2373+
// if the first comment also begins the line, then we look for a later
2374+
// comment to strip
2375+
if (firstComment != std::string::npos &&
2376+
firstComment == firstNonWhitespaceChar) {
2377+
2378+
size_t secondComment = findComment(*pStr, firstComment + 1);
2379+
if (secondComment != std::string::npos) {
2380+
pStr->erase(secondComment);
2381+
}
2382+
return;
2383+
}
2384+
2385+
// if we get here, then the first comment is not the entire string --
2386+
// so we can strip it
2387+
if (firstComment != std::string::npos) {
2388+
pStr->erase(firstComment);
2389+
}
2390+
}
23462391

23472392
// Trim a string
23482393
void trimWhitespace(std::string* pStr) {

0 commit comments

Comments
 (0)