Skip to content

Commit f95e30f

Browse files
committed
#176-Test CppStringT::translate() with char and wchar_t
Completed. Validated.
1 parent 52404a8 commit f95e30f

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

cpp-strings-tests/cpp-strings-tests.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3886,5 +3886,19 @@ namespace cppstringstests
38863886
Assert::AreEqual(wexpected.c_str(), wres.c_str());
38873887

38883888
}
3889+
3890+
TEST_METHOD(translate)
3891+
{
3892+
CppString::TransTable trans_table("oizeaslbgOIZEASLG", "012345789012345789");
3893+
CppString text("This is a big 'Oiseau' that can be seen in 'Le Zoo'");
3894+
CppString expected("Th15 15 4 819 '01534u' th4t c4n 83 533n 1n '73 200'");
3895+
Assert::AreEqual(expected.c_str(), text.translate(trans_table).c_str());
3896+
3897+
CppWString::TransTable wtrans_table(L"oizeaslbgOIZEASLG", L"012345789012345789");
3898+
CppWString wtext(L"This is a big 'Oiseau' that can be seen in 'Le Zoo'");
3899+
CppWString wexpected(L"Th15 15 4 819 '01534u' th4t c4n 83 533n 1n '73 200'");
3900+
Assert::AreEqual(wexpected.c_str(), wtext.translate(wtrans_table).c_str());
3901+
}
3902+
38893903
};
38903904
}

cpp-strings/cppstrings.h

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -348,14 +348,15 @@ namespace pcs // i.e. "pythonic c++ strings"
348348
}
349349

350350
/** \brief Indexing operator. */
351-
inline CppStringT operator[] (const key_type ch) noexcept
351+
inline value_type operator[] (const key_type ch) noexcept
352+
//inline CppStringT operator[] (const key_type ch) noexcept
352353
{
353354
auto it = m_table.find(ch);
354355
if (it != m_table.end()) {
355356
return it->second;
356357
}
357358
else {
358-
return CppStringT(ch);
359+
return ch; // CppStringT(ch);
359360
}
360361
}
361362

@@ -366,7 +367,8 @@ namespace pcs // i.e. "pythonic c++ strings"
366367

367368
private:
368369
std::map<typename key_type, typename value_type> m_table{}; // the internal storage of the translation table. Access it via the indexing operator.
369-
};
370+
371+
};
370372

371373

372374
//=== Constructors / Destructor =======================
@@ -1774,24 +1776,14 @@ namespace pcs // i.e. "pythonic c++ strings"
17741776
* to be translated is not available as an entry in the tranlation
17751777
* table, it is set as is in the resulting string.
17761778
*/
1777-
CppStringT translate(const TransTable& table) const noexcept
1779+
CppStringT translate(TransTable& table) noexcept
17781780
{
1779-
/*
17801781
CppStringT res{};
17811782
for (auto ch : *this) {
17821783
try { res += table[ch]; }
17831784
catch (...) { res += ch; }
17841785
}
17851786
return res;
1786-
*/
1787-
1788-
CppStringT res{};
1789-
auto _translate = [&](auto const ch) {
1790-
try { return table[ch]; }
1791-
catch (...) { return ch; }
1792-
};
1793-
std::ranges::copy(std::views::transform(*this, _translate), std::back_inserter(res));
1794-
return res;
17951787
}
17961788

17971789

0 commit comments

Comments
 (0)