Skip to content

Commit 65b40a7

Browse files
committed
#56 - Implement method CppStringT::zfill()
Completed.
1 parent 6996af3 commit 65b40a7

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

cpp-strings/cppstrings.h

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,10 +1393,24 @@ namespace pcs // i.e. "pythonic c++ strings"
13931393
}
13941394

13951395

1396-
protected:
1397-
1396+
//--- zfill() -----------------------------------------
1397+
/** \brief Returns a copy of the string left filled with ASCII '0' digits to make a string of length width.
1398+
*
1399+
* A leading sign prefix ('+'/'-') is handled by inserting the padding
1400+
* after the sign character rather than before. The original string is
1401+
* returned if width is less than or equal to len(s).
1402+
*/
1403+
inline CppStringT zfill(const size_type width) const noexcept
1404+
{
1405+
if (this->size() >= width)
1406+
return *this;
13981407

1399-
private:
1408+
const size_type padding_width = width - this->size();
1409+
if ((*this)[0] == '+' || (*this)[0] == '-')
1410+
return (*this)[0] + this->substr(1, this->size() - 1).ljust(padding_width, value_type('0'));
1411+
else
1412+
return this->ljust(padding_width, value_type('0'));
1413+
}
14001414

14011415
};
14021416

0 commit comments

Comments
 (0)