The old code was trying to make the clearance decision without
normalizing parameters first and missed most cases as the result.
In theory, clear()ing SBuf during chop() is just an optimization
that should have no effect on correct code functionality.
SBuf&
SBuf::chop(size_type pos, size_type n)
{
- if (pos == npos || pos > length() || n == 0) {
+ if (pos == npos || pos > length())
+ pos = length();
+
+ if (n == npos || (pos+n) > length())
+ n = length() - pos;
+
+ // if there will be nothing left, reset the buffer while we can
+ if (pos == length() || n == 0) {
clear();
return *this;
}
- if (n == npos || (pos+n) > length())
- n = length()-pos;
+
++stats.chop;
off_ += pos;
len_ = n;