Skip to content

Commit 5e28b0e

Browse files
committed
Merge pull request #140 from RcppCore/feature/stripTrailingComments
Strip trailing line comments when reading a file for sourceCpp
2 parents 7817c48 + 17a530e commit 5e28b0e

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

src/attributes.cpp

Lines changed: 50 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,46 @@ 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 (firstSlash != std::string::npos) {
2361+
if (firstSlash + 1 < str.length() && str[firstSlash + 1] == '/') {
2362+
return firstSlash;
2363+
}
2364+
}
2365+
return std::string::npos;
2366+
}
2367+
2368+
// Remove trailing line comments -- ie, find comments that don't begin
2369+
// a line, and remove them. We avoid stripping attributes.
2370+
void stripTrailingLineComments(std::string* pStr) {
2371+
2372+
if (pStr->empty()) return;
2373+
2374+
size_t firstComment = findComment(*pStr, 0);
2375+
size_t firstNonWhitespaceChar = pStr->find_first_not_of(kWhitespaceChars);
2376+
2377+
// if the first comment also begins the line, then we look for a later
2378+
// comment to strip
2379+
if (firstComment != std::string::npos &&
2380+
firstComment == firstNonWhitespaceChar) {
2381+
2382+
size_t secondComment = findComment(*pStr, firstComment + 1);
2383+
if (secondComment != std::string::npos) {
2384+
pStr->erase(secondComment);
2385+
}
2386+
return;
2387+
}
2388+
2389+
// if we get here, then the first comment is not the entire string --
2390+
// so we can strip it
2391+
if (firstComment != std::string::npos) {
2392+
pStr->erase(firstComment);
2393+
}
2394+
}
23462395

23472396
// Trim a string
23482397
void trimWhitespace(std::string* pStr) {

0 commit comments

Comments
 (0)