Skip to content
Merged

Dev #226

Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
222 commits
Select commit Hold shift + click to select a range
a428322
#3 - Create Visual Studio 2022 project
schmouk Mar 7, 2023
b3d0cbc
#2 - Create empty module cppstrings.h.
schmouk Mar 7, 2023
0b4227d
#3 - Create Visual Studio 2022 project
schmouk Mar 7, 2023
41767da
#4 - Modify .gitignore
schmouk Mar 7, 2023
5fc2724
#6 - Implement specialized class CppString
schmouk Mar 8, 2023
f7dd584
#7 - Implement specialized class CppWString
schmouk Mar 8, 2023
afa1687
#5 - Create templated base class CppStringT<>
schmouk Mar 8, 2023
266fe34
#60 - Implement method is_punctuation
schmouk Mar 9, 2023
3fd2973
#30 - Implement method CppStringT::isspace()
schmouk Mar 9, 2023
b606e4d
#60 - Implement method is_punctuation
schmouk Mar 9, 2023
f28a337
#30 - Implement method CppStringT::isspace()
schmouk Mar 9, 2023
c12ba5f
#61- Implement method is_words_sep
schmouk Mar 9, 2023
a45bc56
#35 - Implement method CppStringT::lower()
schmouk Mar 9, 2023
9fb272f
#55 - Implement method CppStringT::upper()
schmouk Mar 9, 2023
60782bb
#8 - Implement method CppStringT::capitalize()
schmouk Mar 9, 2023
a3c86ef
#10 - Implement method CppStringT::center()
schmouk Mar 9, 2023
1dee9cf
#16 - Implement method CppStringT::find()
schmouk Mar 10, 2023
9fe08ff
#11 - Implement method CppStringT::count()
schmouk Mar 10, 2023
f3cfc00
#12 - Implement method CppStringT::count_n()
schmouk Mar 10, 2023
224de07
#17 - Implement method CppStringT::find_n()
schmouk Mar 10, 2023
f82fc5d
#62 - Implement exception CppStringT::NotFoundException
schmouk Mar 10, 2023
fc98a07
#19 - Implement method CppStringT::index()
schmouk Mar 10, 2023
b428966
#20 - Implement method CppStringT::index_n()
schmouk Mar 10, 2023
c368826
#42 - Implement method CppStringT::rfind()
schmouk Mar 10, 2023
5d49ab1
#43 - Implement method CppStringT::rindex()
schmouk Mar 10, 2023
895404b
#66 - Fix methods find() and find_n()
schmouk Mar 10, 2023
8a666e5
#67 - Fix methods index() and index_n()
schmouk Mar 10, 2023
7eb0631
#68 - Fix method rfind()
schmouk Mar 10, 2023
14ecbfe
#64 - Implement method rfind_n()
schmouk Mar 10, 2023
2c76cd6
#69 - Fix method rindex()
schmouk Mar 10, 2023
eab2dab
#65 - Implement method rindex_n()
schmouk Mar 11, 2023
e3d8f11
#13 - Implement method CppStringT::endswith()
schmouk Mar 11, 2023
11cae64
#14 - Implement method CppStringT::endswith_n()
schmouk Mar 11, 2023
14054fb
#15 - Implement method CppStringT::expand_tabs()
schmouk Mar 11, 2023
4b0c706
#21 - Implement method CppStringT::isalnum()
schmouk Mar 11, 2023
6ca96ba
#22 - Implement method CppStringT::isalpha()
schmouk Mar 11, 2023
0f1e18f
#71 - Modify method is_punctuation()
schmouk Mar 11, 2023
f4c308c
#70 - Modify method is_space()
schmouk Mar 11, 2023
4903ec2
#23 - Implement method CppStringT::isascii()
schmouk Mar 12, 2023
66d1acd
#24 - Implement method CppStringT::isdecimal()
schmouk Mar 12, 2023
0bd3d0a
#25 - Implement method CppStringT::isdigit()
schmouk Mar 12, 2023
27edefc
#26 - Implement method CppStringT::isidentifier()
schmouk Mar 12, 2023
b41823b
#27 - Implement method CppStringT::islower()
schmouk Mar 12, 2023
f8f6970
#28 - Implement method CppStringT::isnumeric()
schmouk Mar 12, 2023
46387e1
#29 - Implement method CppStringT::isprintable()
schmouk Mar 12, 2023
bee370f
#31 - Implement method CppStringT::istitle()
schmouk Mar 12, 2023
4ee68e3
#32 - Implement method CppStringT::isupper()
schmouk Mar 12, 2023
e0f0489
#33 - Implement method CppStringT::join()
schmouk Mar 12, 2023
fad6641
#34 - Implement method CppStringT::ljust()
schmouk Mar 12, 2023
b6b54d6
#36 - Implement method CppStringT::lstrip()
schmouk Mar 12, 2023
76a222e
#38 - Implement method CppStringT::partition()
schmouk Mar 12, 2023
a9733c0
#39 - Implement method CppStringT::removeprefix()
schmouk Mar 12, 2023
de3e8e0
#40 - Implement method CppStringT::removesuffix()
schmouk Mar 12, 2023
f788380
#41 - Implement method CppStringT::replace()
schmouk Mar 12, 2023
faaa942
#44 - Implement method CppStringT::rjust()
schmouk Mar 12, 2023
8126e7a
#41 - Implement method CppStringT::replace()
schmouk Mar 12, 2023
6d3729e
#45 - Implement method CppStringT::rpartition()
schmouk Mar 12, 2023
b235aef
#46 - Implement method CppStringT::rsplit()
schmouk Mar 13, 2023
861e82c
#73 - Refactor method CppStringT::rsplit()
schmouk Mar 14, 2023
a66f7b5
#47 - Implement method CppStringT::rstrip()
schmouk Mar 14, 2023
2424122
#48 - Implement method CppStringT::split()
schmouk Mar 14, 2023
90310d7
#49 - Implement method CppStringT::splitlines()
schmouk Mar 14, 2023
0b9b7e7
#50 - Implement method CppStringT::startswith()
schmouk Mar 15, 2023
9845bab
#74 - Implement method CppStringT::startswith_n()
schmouk Mar 15, 2023
7351b16
#51 - Implement method CppStringT::strip()
schmouk Mar 15, 2023
00815f4
#52 - Implement method CppStringT::swapcase()
schmouk Mar 15, 2023
6996af3
#53 - Implement method CppStringT::title()
schmouk Mar 15, 2023
65b40a7
#56 - Implement method CppStringT::zfill()
schmouk Mar 16, 2023
42928ad
#75 - Implement method CppStringT::substr()
schmouk Mar 16, 2023
122dfd1
#57 - Implement operator ""cs
schmouk Mar 16, 2023
a7aab2b
#58 - Implement operator ""csv
schmouk Mar 16, 2023
9e8fa92
#37 - Implement method CppStringT::maketrans()
schmouk Mar 17, 2023
1e776b1
#76 - Enhance class CppStringT::TransTable
schmouk Mar 17, 2023
cfdb448
#54 - Implement method CppStringT::translate()
schmouk Mar 17, 2023
090b04c
#77 - Create a unit test solution in a new branch dev/tests
schmouk Mar 17, 2023
0bddd1f
#77 - Create a unit test solution in a new branch dev/tests
schmouk Mar 18, 2023
516d824
#78 - Test pcs::is_alpha() for char and wchar_t
schmouk Mar 18, 2023
11e0bb7
#79 - Test pcs::is_ascii() for char and wchar_t
schmouk Mar 18, 2023
23f2290
#80 - Test pcs::is_decimal() for char and wchar_t
schmouk Mar 18, 2023
b4f6791
#81 - Test pcs::is_id_continue() for char and wchar_t
schmouk Mar 18, 2023
7964ace
#82 - Test pcs::is_id_start() for char and wchar_t
schmouk Mar 18, 2023
6edec7a
#83 - Test pcs::is_lower() for char and wchar_t
schmouk Mar 18, 2023
aa12293
84 - Test pcs::is_printable() for char and wchar_t
schmouk Mar 18, 2023
d0a6f1c
#85 - Test pcs::is_punctuation() for char and wchar_t
schmouk Mar 18, 2023
0eacd20
#86 - Test pcs::is_space() for char and wchar_t
schmouk Mar 18, 2023
0575557
#87 - Test pcs::is_upper() for char and wchar_t
schmouk Mar 18, 2023
2d6e5cd
#88 - Test pcs::swap_case() for char and wchar_t
schmouk Mar 18, 2023
060d0e6
#89 - Test pcs::to_lower() for char and wchar_t
schmouk Mar 18, 2023
d4d697b
#90 - Test pcs::to_upper() for char and wchar_t
schmouk Mar 18, 2023
37f45be
#91 - Test pcs::operator""cs for char and wchar_t
schmouk Mar 18, 2023
435385f
#92 - Test pcs::operator""csv for char and wchar_t
schmouk Mar 18, 2023
345ed73
#93 - Test CppStringT::TransTable constructor #1 with char and wchar_t
schmouk Mar 18, 2023
166e6e2
#94 - Test CppStringT::TransTable constructor #2 with char and wchar_t
schmouk Mar 18, 2023
151260c
#95 - Test CppStringT::TransTable constructor #3 with char and wchar_t
schmouk Mar 18, 2023
7f12c48
#96 - Test CppStringT::TransTable constructor #4 with char and wchar_t
schmouk Mar 18, 2023
61cdb2e
#97 - Test CppStringT::TransTable constructor #5 with char and wchar_t
schmouk Mar 18, 2023
59d8d80
#98 - Test CppStringT::TransTable constructor #6 with char and wchar_t
schmouk Mar 18, 2023
adde769
#99 - Test CppStringT::TransTable constructor #7 with char and wchar_t
schmouk Mar 18, 2023
6e166c8
#100 - Test CppStringT::TransTable constructor #8 with char and wchar_t
schmouk Mar 19, 2023
de72dc6
#101 - Test CppStringT::TransTable constructor #9 with char and wchar_t
schmouk Mar 19, 2023
e861148
#104 - Test CppStringT::TransTable empty constructor with char and wc…
schmouk Mar 19, 2023
4d1fa4d
#105 - Test CppStringT::TransTable copy constructor with char and wch…
schmouk Mar 19, 2023
320109e
#106 - Test CppStringT::TransTable move constructor with char and wch…
schmouk Mar 19, 2023
8a94ae4
#107 - Test CppStringT::TransTable copy assignment with char and wchar_t
schmouk Mar 19, 2023
755de78
#108 - Test CppStringT::TransTable move assignment with char and wchar_t
schmouk Mar 19, 2023
e67fa5a
#109 - Test CppStringT::TransTable std::map-assignment with char and …
schmouk Mar 19, 2023
b9efdfe
#110 - Test CppStringT::TransTable::operator[] with char and wchar_t
schmouk Mar 19, 2023
31399dd
#111 - Test CppStringT empty constructor with char and wchar_t
schmouk Mar 19, 2023
8b862ac
#112 - Test CppStringT copy constructor with char and wchar_t
schmouk Mar 19, 2023
8ada1a9
#113 - Test CppStringT move constructor with char and wchar_t
schmouk Mar 19, 2023
f39c9aa
#114 - Test CppStringT constructor #6 with char and wchar_t
schmouk Mar 19, 2023
ab90c70
#115 - Test CppStringT constructor #7 with char and wchar_t
schmouk Mar 19, 2023
8893fd5
#116 - Test CppStringT constructor #8 with char and wchar_t
schmouk Mar 19, 2023
c6268ec
#117 - Test CppStringT constructor #9 with char and wchar_t
schmouk Mar 19, 2023
fb662fc
#118 - Test CppStringT constructor #10 with char and wchar_t
schmouk Mar 19, 2023
ceebe19
#119 - Test CppStringT constructor #11 with char and wchar_t
schmouk Mar 19, 2023
b02b516
#120 - Test CppStringT constructor #12 with char and wchar_t
schmouk Mar 19, 2023
cf190aa
#121 - Test CppStringT constructor #13 with char and wchar_t
schmouk Mar 19, 2023
6c41fde
#122 - Test CppStringT constructor #14 with char and wchar_t
schmouk Mar 19, 2023
487b5cc
#123 - Test CppStringT constructor #15 with char and wchar_t
schmouk Mar 19, 2023
401945c
#124 - Test CppStringT constructor #16 with char and wchar_t
schmouk Mar 19, 2023
e53011d
#179 - Test CppStringT constructor #19 with char and wchar_t
schmouk Mar 19, 2023
b1c2abc
#127 - Test CppStringT::capitalize() with char and wchar_t
schmouk Mar 19, 2023
9814e6d
#128 - Test CppStringT::center() with char and wchar_t
schmouk Mar 19, 2023
a54feb6
#129 - Test CppStringT::count() with char and wchar_t
schmouk Mar 19, 2023
97f6bb1
#130 - Test CppStringT::count_n() with char and wchar_t
schmouk Mar 19, 2023
33b1083
#131 - Test CppStringT::ends_with() with char and wchar_t
schmouk Mar 19, 2023
ef16d45
#132 - Test CppStringT::ends_with_n() with char and wchar_t
schmouk Mar 19, 2023
acd45ac
#133 - Test CppStringT::expand_tabs() with char and wchar_t
schmouk Mar 19, 2023
fb4586d
#186 - Implement new signatures for method CppStringT::find()
schmouk Mar 20, 2023
1c6ce39
#187 - Implement new signatures for CppStringT::find_n()
schmouk Mar 20, 2023
63bcf2a
#187 - Implement new signatures for CppStringT::find_n()
schmouk Mar 20, 2023
cc418b2
#134 - Test CppStringT::find() with char and wchar_t
schmouk Mar 20, 2023
7636c39
#135 - Test CppStringT::find_n() with char and wchar_t
schmouk Mar 20, 2023
403a87c
#190 - Implement new signatures for method CppStringT::index()
schmouk Mar 21, 2023
e9bbcdd
#136 - Test CppStringT::index() with char and wchar_t
schmouk Mar 21, 2023
951d497
#191 - Implement new signatures for method CppStringT::index_n()
schmouk Mar 21, 2023
a12477b
#137 - Test CppStringT::index_n() with char and wchar_t
schmouk Mar 21, 2023
4ec3564
#138 - Test CppStringT::isalnum() with char and wchar_t
schmouk Mar 21, 2023
3fdad29
#139 - Test CppStringT::isalpha() with char and wchar_t
schmouk Mar 21, 2023
4dc817f
#140 - Test CppStringT::isascii() with char and wchar_t
schmouk Mar 21, 2023
f4588b8
#141 - Test CppStringT::isdecimal() with char and wchar_t
schmouk Mar 21, 2023
b981f6a
#142 - Test CppStringT::isdigit() with char and wchar_t
schmouk Mar 21, 2023
2c3c035
#145 - Test CppStringT::isnumeric() with char and wchar_t
schmouk Mar 21, 2023
177811d
#143 - Test CppStringT::isidentifier() with char and wchar_t
schmouk Mar 22, 2023
365319f
#144 - Test CppStringT::islower() with char and wchar_t
schmouk Mar 22, 2023
33f809f
#146 - Test CppStringT::isprintable() with char and wchar_t
schmouk Mar 22, 2023
740ee0f
#147 - Test CppStringT::ispunctuation() with char and wchar_t
schmouk Mar 22, 2023
7ca47c7
#148 - Test CppStringT::isspace() with char and wchar_t
schmouk Mar 22, 2023
b1c0e4c
#149 - Test CppStringT::istitle() with char and wchar_t
schmouk Mar 22, 2023
474642e
#150 - Test CppStringT::isupper() with char and wchar_t
schmouk Mar 22, 2023
43f92f7
#151 - Test CppStringT::is_words_sep() with char and wchar_t
schmouk Mar 22, 2023
aba3db6
#152 - Test `CppStringT::join()` with `char` and `wchar_t`
schmouk May 12, 2023
383adcd
#153 - Test CppStringT::ljust() with char and wchar_t
schmouk May 12, 2023
78c5ebc
#154 - Test CppStringT::lower() with char and wchar_t
schmouk May 12, 2023
c7a867b
#155 - Test CppStringT::lstrip() with char and wchar_t
schmouk May 13, 2023
98a85fb
#156 - Test CppStringT::partition() with char and wchar_t
schmouk May 13, 2023
e85bfc9
#157 - Test CppStringT::removeprefix() with char and wchar_t
schmouk May 13, 2023
6faba8d
#158 - Test CppStringT::removesuffix() with char and wchar_t
schmouk May 13, 2023
56b7326
#159 - Test CppStringT::replace() with char and wchar_t
schmouk Nov 27, 2023
2c30532
#160 - Test CppStringT::rfind() with char and wchar_t
schmouk Nov 28, 2023
6ff8a0f
#196 - Extend and fix tests on find() and index() methods
schmouk Dec 2, 2023
cbcaae5
#161 - Test CppStringT::rfind_n() with char and wchar_t
schmouk Dec 5, 2023
db2e408
#199-refactor tests
schmouk Jul 7, 2025
866069a
#162-Test CppStringT::rindex() with char and wchar_t
schmouk Jul 7, 2025
beafb01
#163-Test CppStringT::rindex_n() with char and wchar_t
schmouk Jul 7, 2025
8b5c5b9
#163-Test CppStringT::rindex_n() with char and wchar_t
schmouk Jul 7, 2025
e3cf5e3
#164-Test CppStringT::rjust() with char and wchar_t
schmouk Jul 7, 2025
37d0dc8
#164-Test CppStringT::rjust() with char and wchar_t
schmouk Jul 7, 2025
96f452c
#165-Test CppStringT::rpartition() with char and wchar_t
schmouk Jul 7, 2025
1062816
#165-Test CppStringT::rpartition() with char and wchar_t
schmouk Jul 7, 2025
80c3288
#166-Test CppStringT::rsplit() with char and wchar_t
schmouk Jul 9, 2025
19d9688
#166-Test CppStringT::rsplit() with char and wchar_t
schmouk Jul 9, 2025
f1f476b
#167-Test CppStringT::rstrip() with char and wchar_t
schmouk Jul 9, 2025
c892008
#168-Test CppStringT::split() with char and wchar_t
schmouk Jul 9, 2025
90d8397
#169-Test CppStringT::splitlines() with char and wchar_t
schmouk Jul 10, 2025
446cb8e
#200-modify .ignore and VS projects to compile and test 32-bits platf…
schmouk Jul 10, 2025
a11502e
#170-Test CppStringT::startswith() with char and wchar_t
schmouk Jul 10, 2025
9005fb0
#171-Test CppStringT::startswith_n() with char and wchar_t
schmouk Jul 10, 2025
6575060
#172-Test CppStringT::strip() with char and wchar_t
schmouk Jul 10, 2025
41860e7
#173-Test CppStringT::substr() with char and wchar_t
schmouk Jul 10, 2025
ce29faf
#174-Test CppStringT::swapcase() with char and wchar_t
schmouk Jul 11, 2025
52404a8
#175-Test CppStringT::title() with char and wchar_t
schmouk Jul 11, 2025
f95e30f
#176-Test CppStringT::translate() with char and wchar_t
schmouk Jul 11, 2025
aa60e6a
#177-Test CppStringT::upper() with char and wchar_t
schmouk Jul 11, 2025
e3ff5ce
#178-Test CppStringT::zfill() with char and wchar_t
schmouk Jul 11, 2025
7588cb6
#202-add test on nullptr in CppStringT pointer constructors
schmouk Jul 11, 2025
873d6d1
#182-Implement method CppStringT::contains()
schmouk Jul 11, 2025
60f5163
#184-Test method CppStringT::contains()
schmouk Jul 11, 2025
c269f62
#183-Implement method CppStringT::contains_n()
schmouk Jul 11, 2025
016bdfd
#183-Implement method CppStringT::contains_n()
schmouk Jul 11, 2025
fbde103
#185-Test method CppStringT::contains_n()
schmouk Jul 11, 2025
20c35e8
#180-Implement CppStringT::operator* ()
schmouk Jul 11, 2025
1559542
#181-Test CppStringT::operator* ()
schmouk Jul 11, 2025
042fcca
#203-remove signatures with c-strings and chars
schmouk Jul 11, 2025
83f468c
#208-implement slices
schmouk Jul 12, 2025
43f4bbd
#208-implement slices
schmouk Jul 12, 2025
3f25a77
#209-test slices
schmouk Jul 13, 2025
f512977
#194-Implement operator () with slices
schmouk Jul 13, 2025
5aa3489
#195-Test operator () with slices
schmouk Jul 13, 2025
96ccdf7
#210-remove string_views stuff.
schmouk Jul 13, 2025
b3dc949
#211-fix c++ and c standards for every projects, configurations and p…
schmouk Jul 13, 2025
e986ede
#18-Implement method CppStringT::format()
schmouk Jul 13, 2025
18f4f7d
#18-Implement method CppStringT::format()
schmouk Jul 15, 2025
bccb85b
#205-test method CppStringT::format()
schmouk Jul 15, 2025
2074216
#217-set VS project to c++20 and c17
schmouk Jul 15, 2025
6110a76
#207-Add namespace pcs when missing in tests
schmouk Jul 15, 2025
b9ae59c
#206-fix missing \briefs in doxygen strings
schmouk Jul 15, 2025
2ee7546
#212-add attribute [[nodiscard]] at any useful place
schmouk Jul 15, 2025
2348d3c
#219-modify copyright dates
schmouk Jul 15, 2025
19d3913
#213-add attribute [[likely]] and [[unlikely]]
schmouk Jul 16, 2025
7a79232
Merge pull request #221 from schmouk/tests
schmouk Jul 16, 2025
685553f
#204-create PCS documentation
schmouk Jul 16, 2025
7a771e2
#204-create PCS documentation
schmouk Jul 16, 2025
96c75d6
#204-create PCS documentation
schmouk Jul 16, 2025
4fda9ec
Update README.md
schmouk Jul 16, 2025
2e1ad62
#204
schmouk Jul 16, 2025
cac9c07
#224-generate doxygen documentation
schmouk Jul 16, 2025
061477d
#204-create PCS documentation
schmouk Jul 16, 2025
7382f6b
#223-cpp-strings VS project clean-up
schmouk Jul 16, 2025
7d902d3
#222-simplify .gitignore
schmouk Jul 16, 2025
7df48fc
Merge pull request #225 from schmouk/documentation
schmouk Jul 16, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
#159 - Test CppStringT::replace() with char and wchar_t
Tested and validated;
Fixed a few bugs, notably within methods find() and find_n() and within tests of find() and find_n()!
  • Loading branch information
schmouk committed Nov 27, 2023
commit 56b7326ded9d4ceb9dcffc3c67771dc9d69889ca
148 changes: 130 additions & 18 deletions cpp-strings-tests/cpp-strings-tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,45 +171,119 @@ namespace cppstringstests

TEST_METHOD(find)
{
size_t found_pos;

pcs::CppString test{ "ABC0123456789." };
for (int c = 0; c <= 255; ++c) {
char ch{ char(c) };
Assert::AreEqual(test.MyBaseClass::find(ch), test.find(ch));
Assert::AreEqual(test.substr(2).MyBaseClass::find(ch), test.find(ch, 2));
Assert::AreEqual(test.substr(2,5).MyBaseClass::find(ch), test.find(ch, 2, 5+2-1));

found_pos = test.substr(2).MyBaseClass::find(ch);
if (found_pos == pcs::CppString::npos)
Assert::AreEqual(found_pos, test.find(ch, 2));
else
Assert::AreEqual(found_pos, test.find(ch, 2) - 2);

found_pos = test.substr(2, 5).MyBaseClass::find(ch);
if (found_pos == pcs::CppString::npos)
Assert::AreEqual(found_pos, test.find(ch, 2, pcs::CppString::size_type(5 + 2 - 1)));
else
Assert::AreEqual(found_pos, test.find(ch, 2, pcs::CppString::size_type(5 + 2 - 1)) - 2);

CppString s(ch);
Assert::AreEqual(test.MyBaseClass::find(s), test.find(s));
Assert::AreEqual(test.substr(2).MyBaseClass::find(s), test.find(s,2));
Assert::AreEqual(test.substr(3, 5).MyBaseClass::find(s), test.find(s, 3, 5+3-1));

char str[2]{ ch, 0 };
Assert::AreEqual(test.MyBaseClass::find(str), test.find(str));
Assert::AreEqual(test.substr(2).MyBaseClass::find(str), test.find(str, 2));
Assert::AreEqual(test.substr(3, 5).MyBaseClass::find(str), test.find(str, 3, 5+3-1));
found_pos = test.substr(2).MyBaseClass::find(s);
if (found_pos == pcs::CppString::npos)
Assert::AreEqual(found_pos, test.find(s, 2));
else
Assert::AreEqual(found_pos, test.find(s, 2) - 2);

found_pos = test.substr(2, 5).MyBaseClass::find(s);
if (found_pos == pcs::CppString::npos)
Assert::AreEqual(found_pos, test.find(s, 2, pcs::CppString::size_type(5 + 2 - 1)));
else
Assert::AreEqual(found_pos, test.find(s, 2, pcs::CppString::size_type(5 + 2 - 1)) - 2);

if (c > 0) {
char str[2]{ ch, 0 };
Assert::AreEqual(test.MyBaseClass::find(str), test.find(str));

found_pos = test.substr(2).MyBaseClass::find(str);
if (found_pos == pcs::CppString::npos)
Assert::AreEqual(found_pos, test.find(str, 2));
else
Assert::AreEqual(found_pos, test.find(str, 2) - 2);

found_pos = test.substr(2, 5).MyBaseClass::find(str);
if (found_pos == pcs::CppString::npos)
Assert::AreEqual(found_pos, test.find(str, 2, pcs::CppString::size_type(5 + 2 - 1)));
else
Assert::AreEqual(found_pos, test.find(str, 2, pcs::CppString::size_type(5 + 2 - 1)) - 2);
}
}
Assert::AreEqual(pcs::CppString::npos, test.find("A", 1));
Assert::AreEqual(size_t(0), test.find(""));
Assert::AreEqual(pcs::CppString::npos, test.find(".", 14));
Assert::AreEqual(size_t(13), test.find(".", 13));

pcs::CppWString wtest{ L"ABC0123456789." };
for (int wc = 0; wc <=0xffff; ++wc) {
wchar_t wch{ wchar_t(wc) };
Assert::AreEqual(wtest.MyBaseClass::find(wch), wtest.find(wch));
Assert::AreEqual(wtest.substr(2).MyBaseClass::find(wch), wtest.find(wch, 2));
Assert::AreEqual(wtest.substr(2, 5).MyBaseClass::find(wch), wtest.find(wch, 2, 5 + 2 - 1));

found_pos = wtest.substr(2).MyBaseClass::find(wch);
if (found_pos == pcs::CppString::npos)
Assert::AreEqual(found_pos, wtest.find(wch, 2));
else
Assert::AreEqual(found_pos, wtest.find(wch, 2) - 2);

found_pos = wtest.substr(2, 5).MyBaseClass::find(wch);
if (found_pos == pcs::CppString::npos)
Assert::AreEqual(found_pos, wtest.find(wch, 2, pcs::CppWString::size_type(5 + 2 - 1)));
else
Assert::AreEqual(found_pos, wtest.find(wch, 2, pcs::CppWString::size_type(5 + 2 - 1)) - 2);

CppWString ws(wch);
Assert::AreEqual(wtest.MyBaseClass::find(ws), wtest.find(ws));
Assert::AreEqual(wtest.substr(2).MyBaseClass::find(ws), wtest.find(ws, 2));
Assert::AreEqual(wtest.substr(3, 5).MyBaseClass::find(ws), wtest.find(ws, 3, 5 + 3 - 1));

wchar_t wstr[2]{ wch, 0 };
Assert::AreEqual(wtest.MyBaseClass::find(wstr), wtest.find(wstr));
Assert::AreEqual(wtest.substr(2).MyBaseClass::find(wstr), wtest.find(wstr, 2));
Assert::AreEqual(wtest.substr(3, 5).MyBaseClass::find(wstr), wtest.find(wstr, 3, 5 + 3 - 1));
found_pos = wtest.substr(2).MyBaseClass::find(ws);
if (found_pos == pcs::CppString::npos)
Assert::AreEqual(found_pos, wtest.find(ws, 2));
else
Assert::AreEqual(found_pos, wtest.find(ws, 2) - 2);

found_pos = wtest.substr(2, 5).MyBaseClass::find(ws);
if (found_pos == pcs::CppString::npos)
Assert::AreEqual(found_pos, wtest.find(ws, 2, pcs::CppString::size_type(5 + 2 - 1)));
else
Assert::AreEqual(found_pos, wtest.find(ws, 2, pcs::CppString::size_type(5 + 2 - 1)) - 2);


if (wc > 0) {
wchar_t wstr[2]{ wch, 0 };
Assert::AreEqual(wtest.MyBaseClass::find(wstr), wtest.find(wstr));

found_pos = wtest.substr(2).MyBaseClass::find(wstr);
if (found_pos == pcs::CppString::npos)
Assert::AreEqual(found_pos, wtest.find(wstr, 2));
else
Assert::AreEqual(found_pos, wtest.find(wstr, 2) - 2);

found_pos = wtest.substr(2, 5).MyBaseClass::find(wstr);
if (found_pos == pcs::CppString::npos)
Assert::AreEqual(found_pos, wtest.find(wstr, 2, pcs::CppString::size_type(5 + 2 - 1)));
else
Assert::AreEqual(found_pos, wtest.find(wstr, 2, pcs::CppString::size_type(5 + 2 - 1)) - 2);
}
}
Assert::AreEqual(pcs::CppString::npos, wtest.find(L"A", 1));
Assert::AreEqual(size_t(0), wtest.find(L""));
Assert::AreEqual(pcs::CppString::npos, wtest.find(L".", 14));
Assert::AreEqual(size_t(13), wtest.find(L".", 13));
}

TEST_METHOD(find_n)
{
// notice: find_n() is fully tested via TEST_METHOD(find)
pcs::CppString test{ "ABC0123456789." };
for (int c = 0; c <= 255; ++c) {
char ch{ char(c) };
Expand Down Expand Up @@ -910,5 +984,43 @@ namespace cppstringstests
Assert::AreEqual(L"abbaba", L"abbabaabcd"cs.removesuffix(L"abcd").c_str());
Assert::AreEqual(L"abcd", L"abcdab"cs.removesuffix(L"ab").c_str());
}


TEST_METHOD(replace)
{
pcs::CppString s("abbaa");
Assert::AreEqual("abbaa", s.replace("e", "fff").c_str());
Assert::AreEqual("AAbbAAAA", s.replace("a", "AA").c_str());
Assert::AreEqual("aBBaa", s.replace("b", "B").c_str());

Assert::AreEqual("abbaa", s.replace("e", "fff", 0).c_str());
Assert::AreEqual("abbaa", s.replace("a", "AA", 0).c_str());
Assert::AreEqual("abbaa", s.replace("b", "B", 0).c_str());

Assert::AreEqual("abbaa", s.replace("e", "fff", 1).c_str());
Assert::AreEqual("AAbbaa", s.replace("a", "AA", 1).c_str());
Assert::AreEqual("aBbaa", s.replace("b", "B", 1).c_str());

Assert::AreEqual("abbaa", s.replace("e", "fff", 2).c_str());
Assert::AreEqual("AAbbAAAA", s.replace("a", "AA", 3).c_str());
Assert::AreEqual("aBBaa", s.replace("b", "B", 5).c_str());

pcs::CppWString ws(L"abbaa");
Assert::AreEqual(L"abbaa", ws.replace(L"e", L"fff").c_str());
Assert::AreEqual(L"AAbbAAAA", ws.replace(L"a", L"AA").c_str());
Assert::AreEqual(L"aBBaa", ws.replace(L"b", L"B").c_str());

Assert::AreEqual(L"abbaa", ws.replace(L"e", L"fff", 0).c_str());
Assert::AreEqual(L"abbaa", ws.replace(L"a", L"AA", 0).c_str());
Assert::AreEqual(L"abbaa", ws.replace(L"b", L"B", 0).c_str());

Assert::AreEqual(L"abbaa", ws.replace(L"e", L"fff", 1).c_str());
Assert::AreEqual(L"AAbbaa", ws.replace(L"a", L"AA", 1).c_str());
Assert::AreEqual(L"aBbaa", ws.replace(L"b", L"B", 1).c_str());

Assert::AreEqual(L"abbaa", ws.replace(L"e", L"fff", 2).c_str());
Assert::AreEqual(L"AAbbAAAA", ws.replace(L"a", L"AA", 3).c_str());
Assert::AreEqual(L"aBBaa", ws.replace(L"b", L"B", 5).c_str());
}
};
}
55 changes: 19 additions & 36 deletions cpp-strings/cppstrings.h
Original file line number Diff line number Diff line change
Expand Up @@ -583,13 +583,12 @@ namespace pcs // i.e. "pythonic c++ strings"
*/
constexpr size_type find(const CppStringT& sub, const size_type start = 0, const size_type end = -1) const noexcept
{
size_type start_{ start };
const size_type end_{ (end == -1) ? this->size() : end };

if (start_ > end_)
if (start > end_)
return CppStringT::npos;
else
return find_n(sub, start_, end_ - start_ + 1);
return find_n(sub, start, end_ - start + 1);
}

/** Returns the lowest index in the string where character ch is found within the slice str[start:end], or -1 (i.e. 'npos') if ch is not found.
Expand Down Expand Up @@ -642,12 +641,16 @@ namespace pcs // i.e. "pythonic c++ strings"
*/
inline constexpr size_type find_n(const CppStringT& sub, const size_type start, const size_type count) const noexcept
{
constexpr size_type npos{ CppStringT::npos };

try {
CppStringT part{ this->substr(start, count) };
return part.MyBaseClass::find(sub);
const CppStringT sub_str{ this->substr(start, count) };
const size_type found_pos{ sub_str.MyBaseClass::find(sub) };

return (found_pos == npos) ? npos : found_pos + start;
}
catch (...) {
return CppStringT::npos;
return npos;
}
}

Expand Down Expand Up @@ -1049,7 +1052,7 @@ namespace pcs // i.e. "pythonic c++ strings"
* The separator between elements is the string to which this method is applied.
*/
template<class... NextCppStringsT>
inline CppStringT join(const CppStringT& first, const NextCppStringsT... others) const noexcept
inline CppStringT join(const CppStringT& first, const NextCppStringsT&... others) const noexcept
requires (sizeof...(others) > 0)
{
return first + *this + this->join(others...);
Expand Down Expand Up @@ -1182,43 +1185,23 @@ namespace pcs // i.e. "pythonic c++ strings"


//--- replace() ---------------------------------------
/** \brief Returns a copy of the string with all occurrences of substring old replaced by new. */
CppStringT replace(const CppStringT& old, const CppStringT& new_) const noexcept
{
if (!this->contains(old))
return *this;

CppStringT res{};
size_type last_index = 0;
size_type current_index = 0;
while ((current_index = this->find(old)) != CppStringT::npos) {
res += this->substr(last_index, current_index - last_index) + new_;
last_index = current_index;
}

if (last_index != this->size())
res += this->substr(last_index, this->size - last_index);

return res;
}

/** \brief Returns a copy of the string with first count occurrences of substring old replaced by new. */
CppStringT replace(const CppStringT& old, const CppStringT& new_, size_type count) const noexcept
/** \brief Returns a copy of the string with first count occurrences of substring 'old' replaced by 'new_'. */
CppStringT replace(const CppStringT& old, const CppStringT& new_, size_type count = -1) const noexcept
{
if (!this->contains(old) || count == 0)
if (old == new_ || old.empty())
return *this;

CppStringT res{};
size_type last_index = 0;
size_type prev_index = 0;
size_type current_index = 0;
while (count > 0 && (current_index = this->find(old)) != CppStringT::npos) {
res += this->substr(last_index, current_index - last_index) + new_;
last_index = current_index;
while (count > 0 && (current_index = this->find(old, prev_index)) != CppStringT::npos) {
res += this->substr(prev_index, current_index - prev_index) + new_;
prev_index = current_index + 1;
--count;
}

if (last_index != this->size())
res += this->substr(last_index, this->size - last_index);
if (prev_index < this->size())
res += this->substr(prev_index, this->size() - prev_index);

return res;
}
Expand Down