Skip to content

Commit eecaf74

Browse files
committed
Smarter stripping (make sure we're not in a string)
1 parent 43b0be1 commit eecaf74

File tree

1 file changed

+24
-32
lines changed

1 file changed

+24
-32
lines changed

src/attributes.cpp

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,6 @@ namespace attributes {
7575
// Trim a string
7676
void trimWhitespace(std::string* pStr);
7777

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-
8278
// Strip trailing line comments
8379
void stripTrailingLineComments(std::string* pStr);
8480

@@ -2353,43 +2349,39 @@ namespace attributes {
23532349
return std::strchr(kWhitespaceChars, ch) != NULL;
23542350
}
23552351

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-
23682352
// Remove trailing line comments -- ie, find comments that don't begin
23692353
// a line, and remove them. We avoid stripping attributes.
23702354
void stripTrailingLineComments(std::string* pStr) {
23712355

23722356
if (pStr->empty()) return;
23732357

2374-
size_t firstComment = findComment(*pStr, 0);
2375-
size_t firstNonWhitespaceChar = pStr->find_first_not_of(kWhitespaceChars);
2358+
size_t len = pStr->length();
2359+
bool inString = false;
2360+
size_t idx = 0;
23762361

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;
2362+
// skip over initial whitespace
2363+
idx = pStr->find_first_not_of(kWhitespaceChars);
2364+
if (idx == std::string::npos) return;
2365+
2366+
// skip over a first comment
2367+
if (idx + 1 < len && pStr->at(idx) == '/' && pStr->at(idx + 1) == '/') {
2368+
idx = idx + 2;
23872369
}
23882370

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);
2371+
// since we are searching for "//", we iterate up to 2nd last character
2372+
while (idx < len - 1) {
2373+
2374+
if (pStr->at(idx) == '"') {
2375+
inString = !inString;
2376+
}
2377+
2378+
if (!inString &&
2379+
pStr->at(idx) == '/' &&
2380+
pStr->at(idx + 1) == '/') {
2381+
pStr->erase(idx);
2382+
return;
2383+
}
2384+
++idx;
23932385
}
23942386
}
23952387

0 commit comments

Comments
 (0)