Skip to content

Commit 14054fb

Browse files
committed
#15 - Implement method CppStringT::expand_tabs()
Completed.
1 parent 11cae64 commit 14054fb

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

cpp-strings/cppstrings.h

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,35 @@ class CppStringT : public std::basic_string<CharT>
249249
}
250250

251251

252+
//--- expand_tabs() -----------------------------------
253+
/** Returns a copy of the string where all tab characters are replaced by one or more spaces, depending on the current column and the given tab size. */
254+
CppStringT expand_tabs(const size_type tabsize = 8) const noexcept
255+
{
256+
const size_type tabsize_{ tabsize == 0 ? 1 : tabsize };
257+
CppStringT ret{};
258+
259+
std::size_t current_pos{ 0 };
260+
for (const value_type ch : *this) {
261+
if (ch == value_type('\t')) {
262+
do {
263+
ret += value_type(' ');
264+
current_pos++;
265+
} while (current_pos % tabsize_ != 0);
266+
}
267+
else if (ch == value_type('\n') || ch == value_type('\r')) {
268+
ret += ch;
269+
current_pos = 0;
270+
}
271+
else {
272+
ret += ch;
273+
current_pos++;
274+
}
275+
}
276+
277+
return ret;
278+
}
279+
280+
252281
//--- find_n() ----------------------------------------
253282
/** Returns the lowest index in the string where substring sub is found within the slice str[start:start+count-1], or -1 (i.e. 'npos') if sub is not found.
254283
*

0 commit comments

Comments
 (0)