Skip to content

Commit 42928ad

Browse files
committed
#75 - Implement method CppStringT::substr()
Completed.
1 parent 65b40a7 commit 42928ad

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

cpp-strings/cppstrings.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ using namespace pcs;
2929
*/
3030
int main()
3131
{
32-
CppString s;
33-
CppWString ws;
32+
CppString s("-5.1");
33+
CppWString ws(L"-5.1");
3434

3535
std::cout << ws.isupper() << std::endl;
36+
std::cout << s.zfill(10).c_str() << std::endl;
3637

3738
return 0;
3839
}

cpp-strings/cppstrings.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,11 @@ namespace pcs // i.e. "pythonic c++ strings"
146146
inline CppStringT(const CharT* s, size_type count) : MyBaseClass(s, count) {}
147147
inline CppStringT(std::initializer_list<CharT> ilist) : MyBaseClass(ilist) {}
148148

149+
inline CppStringT(const MyBaseClass& other) : MyBaseClass(other) {}
150+
inline CppStringT(const MyBaseClass& other, const AllocatorT& alloc) : MyBaseClass(other, alloc) {}
151+
inline CppStringT(MyBaseClass&& other) : MyBaseClass(other) {}
152+
inline CppStringT(MyBaseClass&& other, const AllocatorT& alloc) : MyBaseClass(other, alloc) {}
153+
149154
template<class InputIt>
150155
inline CppStringT(InputIt first, InputIt last) : MyBaseClass(first, last) {}
151156

@@ -1334,6 +1339,17 @@ namespace pcs // i.e. "pythonic c++ strings"
13341339
}
13351340

13361341

1342+
//--- substr() ----------------------------------------
1343+
/** \brief Returns acopy of the string, starting at index start and ending after count characters. */
1344+
inline CppStringT substr(const size_type start, const size_type count) const noexcept
1345+
{
1346+
if (start > this->size())
1347+
return *this;
1348+
const size_type width = std::min(count, this->size() - start);
1349+
return CppStringT(MyBaseClass::substr(start, width));
1350+
}
1351+
1352+
13371353
//--- swapcase() --------------------------------------
13381354
/** \brief Returns a copy of the string with uppercase characters converted to lowercase and vice versa.
13391355
*

0 commit comments

Comments
 (0)