Skip to content

Commit a3c86ef

Browse files
committed
#10 - Implement method CppStringT::center()
Comlpeted.
1 parent 60782bb commit a3c86ef

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

cpp-strings/cppstrings.h

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class CppStringT : public std::basic_string<CharT>
109109
//=== Methods =========================================
110110

111111
//--- capitalize() ------------------------------------
112-
// /** \brief In-place modifies the string with its first character capitalized and the rest lowercased. Returns a reference to the string*/
112+
/** \brief In-place modifies the string with its first character capitalized and the rest lowercased. Returns a reference to the string*/
113113
inline CppStringT& capitalize() noexcept
114114
{
115115
this->lower();
@@ -118,6 +118,23 @@ class CppStringT : public std::basic_string<CharT>
118118
}
119119

120120

121+
//--- center() ----------------------------------------
122+
/** Returns the string centered in a string of length width.
123+
* *
124+
* Padding is done using the specified fillchar (default is an ASCII space).
125+
* A copy of the original string is returned if width is less than or equal
126+
* to the length of the string. The original string remains unchanged.
127+
*/
128+
inline CppStringT center(const size_type width, const value_type fillch = value_type(' ')) const noexcept
129+
{
130+
const size_type l = this->length();
131+
if (l <= width)
132+
return CppStringT(*this);
133+
const size_type half = (width - l) / 2;
134+
return CppStringT(fillch, half) + *this + CppStringT(fillch, width - half - l);
135+
}
136+
137+
121138
//--- is_punctuation() --------------------------------
122139
/** \brief Returns true if the string contains only one character and if this character belongs to the ASCII punctuation set. */
123140
inline const bool is_punctuation() const noexcept

0 commit comments

Comments
 (0)