|
22 | 22 |
|
23 | 23 | //============================================================= |
24 | 24 | #include <algorithm> |
| 25 | +#include <cassert> |
25 | 26 | #include <cctype> |
26 | 27 | #include <cwctype> |
27 | 28 | #include <format> |
@@ -180,6 +181,7 @@ namespace pcs // i.e. "pythonic c++ strings" |
180 | 181 | inline NotFoundException(const char* what_arg) : MyBaseClass(what_arg) {} |
181 | 182 | }; |
182 | 183 |
|
| 184 | + |
183 | 185 | //=== Methods ========================================= |
184 | 186 |
|
185 | 187 | //--- capitalize() ------------------------------------ |
@@ -1435,6 +1437,92 @@ namespace pcs // i.e. "pythonic c++ strings" |
1435 | 1437 | return this->ljust(padding_width, value_type('0')); |
1436 | 1438 | } |
1437 | 1439 |
|
| 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 | + |
1438 | 1526 | }; |
1439 | 1527 |
|
1440 | 1528 |
|
|
0 commit comments