Skip to content

Commit 1e776b1

Browse files
committed
#76 - Enhance class CppStringT::TransTable
Completed.
1 parent 9e8fa92 commit 1e776b1

File tree

1 file changed

+150
-22
lines changed

1 file changed

+150
-22
lines changed

cpp-strings/cppstrings.h

Lines changed: 150 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1439,7 +1439,7 @@ namespace pcs // i.e. "pythonic c++ strings"
14391439

14401440

14411441
//=== Translation Table ===============================
1442-
/** \brief The internal class of translation tables, as used with methods CppStringT::maketrans and CppStringT::translate. */
1442+
/** \brief The internal class of translation tables, used with methods CppStringT::maketrans and CppStringT::translate. */
14431443
class TransTable
14441444
{
14451445
public:
@@ -1448,79 +1448,207 @@ namespace pcs // i.e. "pythonic c++ strings"
14481448
using value_type = CppStringT;
14491449

14501450
//--- Constructors / destructor -------------------
1451+
/** \brief Creates a TransTable from a standard map. */
14511452
inline TransTable(const std::map<key_type, value_type> trans_table)
1452-
: table{ trans_table }
1453+
: m_table{ trans_table }
14531454
{}
14541455

1456+
/** \brief Creates a TransTable from two strings.
1457+
*
1458+
* Parameters keys and values must have the same size. The i-th
1459+
* character in key is associated in the translation table with
1460+
* the i-th character in values.
1461+
*/
14551462
TransTable(const CppStringT& keys, const CppStringT& values)
14561463
{
14571464
assert(keys.size() == values.size());
14581465
auto val_it = values.cbegin();
14591466
for (const auto k : keys)
1460-
table[k] = value_type(*val_it++);
1467+
m_table[k] = value_type(*val_it++);
1468+
}
1469+
1470+
/** \brief Creates a TransTable from three strings.
1471+
*
1472+
* Parameters keys and values must have the same size. The i-th
1473+
* character in key is associated in the translation table with
1474+
* the i -th character in values. Finally, the characters
1475+
* contained in string not_translated are associated in the
1476+
* translation table with the empty string.
1477+
*/
1478+
TransTable(const CppStringT& keys, const CppStringT& values, const CppStringT& not_translated)
1479+
{
1480+
assert(keys.size() == values.size());
1481+
auto val_it = values.cbegin();
1482+
for (const auto k : keys)
1483+
m_table[k] = value_type(*val_it++);
1484+
for (const auto k : not_translated)
1485+
m_table[k] = CppStringT();
14611486
}
14621487

1488+
/** \brief Creates a TransTable from two initalization lists.
1489+
*
1490+
* Parameters keys and values must have the same size. The i-th
1491+
* character in key is associated in the translation table with
1492+
* the i-th character in values.
1493+
*/
14631494
inline TransTable(const std::initializer_list<CppStringT> keys,
14641495
const std::initializer_list<CppStringT> values)
14651496
{
14661497
assert(keys.size() == values.size());
14671498
auto val_it = values.cbegin();
14681499
for (const auto k : keys)
1469-
table[(*k)[0]] = *val_it++;
1500+
m_table[(*k)[0]] = *val_it++;
14701501
}
14711502

1503+
/** \brief Creates a TransTable from three initalization lists.
1504+
*
1505+
* Parameters keys and values must have the same size. The i-th
1506+
* character in key is associated in the translation table with
1507+
* the i -th character in values. Finally, the characters
1508+
* contained in string not_translated are associated in the
1509+
* translation table with the empty string.
1510+
*/
1511+
inline TransTable(const std::initializer_list<CppStringT> keys,
1512+
const std::initializer_list<CppStringT> values,
1513+
const CppStringT& not_translated)
1514+
{
1515+
assert(keys.size() == values.size());
1516+
auto val_it = values.cbegin();
1517+
for (const auto k : keys)
1518+
m_table[(*k)[0]] = *val_it++;
1519+
for (const auto k : not_translated)
1520+
m_table[k] = CppStringT();
1521+
}
1522+
1523+
/** \brief Creates a TransTable from two pointers to null-terminated lists of characters.
1524+
*
1525+
* Parameters keys and values must have the same size. The i-th
1526+
* character in key is associated in the translation table with
1527+
* the i-th character in values.
1528+
*/
14721529
inline TransTable(const CharT* keys, const CharT* values)
14731530
{
14741531
while (*keys && *values)
1475-
table[*keys++] = value_type(*values++);
1532+
m_table[*keys++] = value_type(*values++);
1533+
}
1534+
1535+
/** \brief Creates a TransTable from three pointers to null-terminated lists of characters.
1536+
*
1537+
* Parameters keys and values must have the same size. The i-th
1538+
* character in key is associated in the translation table with
1539+
* the i -th character in values. Finally, the characters
1540+
* contained in string not_translated are associated in the
1541+
* translation table with the empty string.
1542+
*/
1543+
inline TransTable(const CharT* keys, const CharT* values, const CharT* not_translated)
1544+
{
1545+
while (*keys && *values)
1546+
m_table[*keys++] = value_type(*values++);
1547+
while (*not_translated)
1548+
m_table[*not_translated++] = CppStringT();
1549+
}
1550+
1551+
/** \brief Creates a TransTable from two containers iterators.
1552+
*
1553+
* Both containers should have the same size. The i-th
1554+
* character in key is associated in the translation table with
1555+
* the i-th character in values.
1556+
*/
1557+
template<class KeyIt, class ValueIt>
1558+
inline TransTable(KeyIt first_key, KeyIt last_key, ValueIt first_value, ValueIt last_value)
1559+
{
1560+
KeyIt key_it{ first_key };
1561+
ValueIt val_it{ first_value };
1562+
while (key_it != last_key && val_it != last_value)
1563+
m_table[*key_it++] = value_type(*val_it++);
14761564
}
14771565

1478-
template<class InputIt>
1479-
inline TransTable(InputIt first_key, InputIt last_key, InputIt first_value, InputIt last_value)
1566+
/** \brief Creates a TransTable from three containers iterators.
1567+
*
1568+
* Both containers should have the same size. The i-th
1569+
* character in key is associated in the translation table with
1570+
* the i -th character in values. Finally, the characters
1571+
* contained in string not_translated are associated in the
1572+
* translation table with the empty string.
1573+
*/
1574+
template<class KeyIt, class ValueIt>
1575+
inline TransTable(KeyIt first_key, KeyIt last_key,
1576+
ValueIt first_value, ValueIt last_value,
1577+
KeyIt first_not_translated, KeyIt last_not_translated)
14801578
{
1481-
InputIt key_it{ first_key };
1482-
InputIt val_it{ first_value };
1579+
KeyIt key_it{ first_key };
1580+
ValueIt val_it{ first_value };
14831581
while (key_it != last_key && val_it != last_value)
1484-
table[*key_it++] = value_type(*val_it++);
1582+
m_table[*key_it++] = value_type(*val_it++);
1583+
key_it = first_not_translated;
1584+
while (key_it != last_not_translated)
1585+
m_table[*key_it++] = CppStringT();
14851586
}
14861587

1588+
/** \brief Creates a TransTable from two string views.
1589+
*
1590+
* Parameters keys and values must have the same size. The i-th
1591+
* character in key is associated in the translation table with
1592+
* the i-th character in values.
1593+
*/
14871594
template<class StringViewLike>
14881595
explicit TransTable(const StringViewLike& keys, const StringViewLike& values)
14891596
{
14901597
assert(keys.size() == values.size());
14911598
auto val_it = values.cbegin();
14921599
for (const auto k : keys)
1493-
table[(*k)[0]] = value_type(*val_it++);
1600+
m_table[(*k)[0]] = value_type(*val_it++);
1601+
}
1602+
1603+
/** \brief Creates a TransTable from three string views.
1604+
*
1605+
* Parameters keys and values must have the same size. The i-th
1606+
* character in key is associated in the translation table with
1607+
* the i -th character in values. Finally, the characters
1608+
* contained in string not_translated are associated in the
1609+
* translation table with the empty string.
1610+
*/
1611+
template<class StringViewLike>
1612+
TransTable(const StringViewLike& keys, const StringViewLike& values, const StringViewLike& not_translated)
1613+
{
1614+
assert(keys.size() == values.size());
1615+
auto val_it = values.cbegin();
1616+
for (const auto k : keys)
1617+
m_table[k] = value_type(*val_it++);
1618+
for (const auto k : not_translated)
1619+
m_table[k] = CppStringT();
14941620
}
14951621

1496-
inline TransTable() noexcept = default;
1497-
inline TransTable(const TransTable&) noexcept = default;
1498-
inline TransTable(TransTable&&) noexcept = default;
1622+
inline TransTable() noexcept = default; //!< Default empty constructor.
1623+
inline TransTable(const TransTable&) noexcept = default; //!< Default copy constructor.
1624+
inline TransTable(TransTable&&) noexcept = default; //!< Default move constructor.
14991625

1500-
inline ~TransTable() noexcept = default;
1626+
inline ~TransTable() noexcept = default; //!< Default descrtuctor
15011627

15021628
//--- operators -----------------------------------
1503-
inline TransTable& operator= (const TransTable&) noexcept = default;
1504-
inline TransTable& operator= (TransTable&&) noexcept = default;
1629+
inline TransTable& operator= (const TransTable&) noexcept = default; //!< Default copy assignment
1630+
inline TransTable& operator= (TransTable&&) noexcept = default; //!< Default move assignment
15051631

1632+
/** \brief Assignment operator with a standard map. */
15061633
inline TransTable& operator= (const std::map<key_type, value_type>& trans_table) noexcept
15071634
{
1508-
table = trans_table;
1635+
m_table = trans_table;
15091636
return *this;
15101637
}
15111638

1512-
inline CppStringT operator[] (const CharT ch) const noexcept
1639+
/** \biref Indexing operator. */
1640+
inline CppStringT operator[] (const key_type ch) const noexcept
15131641
{
15141642
try {
1515-
return table[ch];
1643+
return m_table[ch];
15161644
}
15171645
catch (...) {
15181646
return CppStringT();
15191647
}
15201648
}
15211649

1522-
//--- data ----------------------------------------
1523-
std::map<key_type, value_type> table{};
1650+
private:
1651+
std::map<key_type, value_type> m_table{}; // the itnernal storage of the translation table. Access it via the indexing operator.
15241652
};
15251653

15261654
};

0 commit comments

Comments
 (0)