Skip to content

Commit 9e8fa92

Browse files
committed
#37 - Implement method CppStringT::maketrans()
Finally implement as an internal class named `TransTable`.
1 parent a7aab2b commit 9e8fa92

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

cpp-strings/cppstrings.h

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
//=============================================================
2424
#include <algorithm>
25+
#include <cassert>
2526
#include <cctype>
2627
#include <cwctype>
2728
#include <format>
@@ -180,6 +181,7 @@ namespace pcs // i.e. "pythonic c++ strings"
180181
inline NotFoundException(const char* what_arg) : MyBaseClass(what_arg) {}
181182
};
182183

184+
183185
//=== Methods =========================================
184186

185187
//--- capitalize() ------------------------------------
@@ -1435,6 +1437,92 @@ namespace pcs // i.e. "pythonic c++ strings"
14351437
return this->ljust(padding_width, value_type('0'));
14361438
}
14371439

1440+
1441+
//=== Translation Table ===============================
1442+
/** \brief The internal class of translation tables, as used with methods CppStringT::maketrans and CppStringT::translate. */
1443+
class TransTable
1444+
{
1445+
public:
1446+
//--- wrappers ------------------------------------
1447+
using key_type = CharT;
1448+
using value_type = CppStringT;
1449+
1450+
//--- Constructors / destructor -------------------
1451+
inline TransTable(const std::map<key_type, value_type> trans_table)
1452+
: table{ trans_table }
1453+
{}
1454+
1455+
TransTable(const CppStringT& keys, const CppStringT& values)
1456+
{
1457+
assert(keys.size() == values.size());
1458+
auto val_it = values.cbegin();
1459+
for (const auto k : keys)
1460+
table[k] = value_type(*val_it++);
1461+
}
1462+
1463+
inline TransTable(const std::initializer_list<CppStringT> keys,
1464+
const std::initializer_list<CppStringT> values)
1465+
{
1466+
assert(keys.size() == values.size());
1467+
auto val_it = values.cbegin();
1468+
for (const auto k : keys)
1469+
table[(*k)[0]] = *val_it++;
1470+
}
1471+
1472+
inline TransTable(const CharT* keys, const CharT* values)
1473+
{
1474+
while (*keys && *values)
1475+
table[*keys++] = value_type(*values++);
1476+
}
1477+
1478+
template<class InputIt>
1479+
inline TransTable(InputIt first_key, InputIt last_key, InputIt first_value, InputIt last_value)
1480+
{
1481+
InputIt key_it{ first_key };
1482+
InputIt val_it{ first_value };
1483+
while (key_it != last_key && val_it != last_value)
1484+
table[*key_it++] = value_type(*val_it++);
1485+
}
1486+
1487+
template<class StringViewLike>
1488+
explicit TransTable(const StringViewLike& keys, const StringViewLike& values)
1489+
{
1490+
assert(keys.size() == values.size());
1491+
auto val_it = values.cbegin();
1492+
for (const auto k : keys)
1493+
table[(*k)[0]] = value_type(*val_it++);
1494+
}
1495+
1496+
inline TransTable() noexcept = default;
1497+
inline TransTable(const TransTable&) noexcept = default;
1498+
inline TransTable(TransTable&&) noexcept = default;
1499+
1500+
inline ~TransTable() noexcept = default;
1501+
1502+
//--- operators -----------------------------------
1503+
inline TransTable& operator= (const TransTable&) noexcept = default;
1504+
inline TransTable& operator= (TransTable&&) noexcept = default;
1505+
1506+
inline TransTable& operator= (const std::map<key_type, value_type>& trans_table) noexcept
1507+
{
1508+
table = trans_table;
1509+
return *this;
1510+
}
1511+
1512+
inline CppStringT operator[] (const CharT ch) const noexcept
1513+
{
1514+
try {
1515+
return table[ch];
1516+
}
1517+
catch (...) {
1518+
return CppStringT();
1519+
}
1520+
}
1521+
1522+
//--- data ----------------------------------------
1523+
std::map<key_type, value_type> table{};
1524+
};
1525+
14381526
};
14391527

14401528

0 commit comments

Comments
 (0)