Skip to content

Commit cf719bf

Browse files
committed
refactor
1 parent 3f166fc commit cf719bf

1 file changed

Lines changed: 28 additions & 24 deletions

File tree

lib/suppressions.cpp

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -153,18 +153,35 @@ std::string SuppressionList::parseXmlFile(const char *filename)
153153
return "";
154154
}
155155

156-
static std::string::size_type findExtraCommentStart(const std::string &comment, std::string::size_type startPos)
156+
static std::string getExtraComment(const std::string &comment, std::string::size_type startPos, std::string::size_type *delimPos = nullptr)
157157
{
158158
const std::string::size_type semiPos = comment.find(';', startPos);
159159
const std::string::size_type slashPos = comment.find("//", startPos);
160+
std::string::size_type pos;
161+
162+
if (semiPos != std::string::npos && semiPos < slashPos) {
163+
pos = semiPos + 1;
164+
if (delimPos)
165+
*delimPos = semiPos;
166+
} else if (slashPos != std::string::npos) {
167+
pos = slashPos + 2;
168+
if (delimPos)
169+
*delimPos = slashPos;
170+
} else {
171+
return "";
172+
}
173+
174+
std::string extra = comment.substr(pos);
160175

161-
if (semiPos != std::string::npos && semiPos < slashPos)
162-
return semiPos + 1;
176+
if (extra.size() >= 2 && extra.compare(extra.size() - 2, 2, "*/") == 0)
177+
extra.erase(extra.size() - 2, 2);
163178

164-
if (slashPos != std::string::npos)
165-
return slashPos + 2;
179+
extra = trim(extra);
166180

167-
return std::string::npos;
181+
for (auto it = extra.begin(); it != extra.end();)
182+
it = *it & 0x80 ? extra.erase(it) : it + 1;
183+
184+
return extra;
168185
}
169186

170187
std::vector<SuppressionList::Suppression> SuppressionList::parseMultiSuppressComment(const std::string &comment, std::string *errorMessage)
@@ -221,21 +238,11 @@ std::vector<SuppressionList::Suppression> SuppressionList::parseMultiSuppressCom
221238
suppressions.push_back(std::move(s));
222239
}
223240

224-
const std::string::size_type extraPos = findExtraCommentStart(comment, end_position);
241+
const std::string extraComment = getExtraComment(comment, end_position);
225242

226-
if (extraPos == std::string::npos)
243+
if (extraComment.empty())
227244
return suppressions;
228245

229-
std::string extraComment = comment.substr(extraPos);
230-
231-
if (extraComment.size() >= 2 && extraComment.compare(extraComment.size() - 2, 2, "*/") == 0)
232-
extraComment.erase(extraComment.size() - 2, 2);
233-
234-
extraComment = trim(extraComment);
235-
236-
for (auto it = extraComment.begin(); it != extraComment.end();)
237-
it = *it & 0x80 ? extraComment.erase(it) : it + 1;
238-
239246
for (auto &suppression : suppressions)
240247
suppression.extraComment = extraComment;
241248

@@ -392,14 +399,11 @@ bool SuppressionList::Suppression::parseComment(std::string comment, std::string
392399
if (comment.compare(comment.size() - 2, 2, "*/") == 0)
393400
comment.erase(comment.size() - 2, 2);
394401

395-
const std::string::size_type extraPos = findExtraCommentStart(comment, 2);
402+
std::string::size_type extraPos;
403+
extraComment = getExtraComment(comment, 2, &extraPos);
396404

397-
if (extraPos != std::string::npos) {
398-
extraComment = trim(comment.substr(extraPos));
399-
for (auto it = extraComment.begin(); it != extraComment.end();)
400-
it = *it & 0x80 ? extraComment.erase(it) : it + 1;
405+
if (!extraComment.empty())
401406
comment.erase(extraPos);
402-
}
403407

404408
const std::set<std::string> cppchecksuppress{
405409
"cppcheck-suppress",

0 commit comments

Comments
 (0)