clearly check itor at end

cppcheck (via Codacy) notes that it's not clear that the iterator (j) cannot exceed end() because most don't expect pointer math.

Rewrote to make it more clear what is going on.
This commit is contained in:
Gregory A Lundberg 2018-01-24 23:04:26 -06:00
parent 59554ca0cd
commit b74df558e0
No known key found for this signature in database
GPG Key ID: 149484078AE8AE9E

View File

@ -290,20 +290,18 @@ std::string encode_binary(const std::string& str)
std::string unencode_binary(const std::string& str)
{
std::string res;
res.resize(str.size());
std::string res(str.size(), '\0');
size_t n = 0;
for(std::string::const_iterator j = str.begin(); j != str.end(); ++j) {
if(*j == escape_char && j+1 != str.end()) {
++j;
res[n++] = *j - 1;
res.resize(res.size()-1);
} else {
res[n++] = *j;
for(std::string::const_iterator j = str.begin(); j != str.end(); ) {
char c = *j++;
if((c == escape_char) && (j != str.end())) {
c = (*j++) - 1;
}
res[n++] = c;
}
res.resize(n);
return res;
}