MCPcopy Create free account
hub / github.com/RcppCore/Rcpp / stripTrailingLineComments

Function stripTrailingLineComments

src/attributes.cpp:3029–3070  ·  view source on GitHub ↗

Remove trailing line comments -- ie, find comments that don't begin a line, and remove them. We avoid stripping attributes.

Source from the content-addressed store, hash-verified

3027 // Remove trailing line comments -- ie, find comments that don't begin
3028 // a line, and remove them. We avoid stripping attributes.
3029 void stripTrailingLineComments(std::string* pStr) {
3030
3031 if (pStr->empty()) return;
3032
3033 size_t len = pStr->length();
3034 bool inString = false;
3035 size_t idx = 0;
3036
3037 // if this is an roxygen comment, then bail
3038 if (isRoxygenCpp(*pStr)) return;
3039
3040 // skip over initial whitespace
3041 idx = pStr->find_first_not_of(kWhitespaceChars);
3042 if (idx == std::string::npos) return;
3043
3044 // skip over a first comment
3045 if (idx + 1 < len && pStr->at(idx) == '/' && pStr->at(idx + 1) == '/') {
3046 idx = idx + 2;
3047 }
3048
3049 // since we are searching for "//", we iterate up to 2nd last character
3050 while (idx < len - 1) {
3051
3052 if (inString) {
3053 if (pStr->at(idx) == '"' && pStr->at(idx - 1) != '\\') {
3054 inString = false;
3055 }
3056 } else {
3057 if (pStr->at(idx) == '"') {
3058 inString = true;
3059 }
3060 }
3061
3062 if (!inString &&
3063 pStr->at(idx) == '/' &&
3064 pStr->at(idx + 1) == '/') {
3065 pStr->erase(idx);
3066 return;
3067 }
3068 ++idx;
3069 }
3070 }
3071
3072 // Trim a string
3073 void trimWhitespace(std::string* pStr) {

Callers 1

readLinesFunction · 0.85

Calls 5

isRoxygenCppFunction · 0.85
eraseMethod · 0.80
emptyMethod · 0.45
lengthMethod · 0.45
atMethod · 0.45

Tested by

no test coverage detected