diff --git a/ZRCola/zrcolacomppnl.cpp b/ZRCola/zrcolacomppnl.cpp index 4baf946..3b3a016 100644 --- a/ZRCola/zrcolacomppnl.cpp +++ b/ZRCola/zrcolacomppnl.cpp @@ -150,7 +150,8 @@ void wxZRColaComposerPanel::SynchronizePanels() dst = std::move(dst2); dst2.clear(); - map.invert(); + for (auto& m : map) + m.invert(); m_mapping.push_back(std::move(map)); map.clear(); } @@ -161,7 +162,8 @@ void wxZRColaComposerPanel::SynchronizePanels() dst = std::move(dst2); dst2.clear(); - map.invert(); + for (auto& m : map) + m.invert(); m_mapping.push_back(std::move(map)); map.clear(); } @@ -203,16 +205,16 @@ void wxZRColaComposerPanel::OnSourcePaint(wxPaintEvent& event) m_selSource.second = to; m_sourceHex->SetSelection( - m_selSourceHex.first = (long)m_mappingSourceHex.to_dst(from), - m_selSourceHex.second = (long)m_mappingSourceHex.to_dst(to )); + m_selSourceHex.first = (long)stdex::src2dst(m_mappingSourceHex, from), + m_selSourceHex.second = (long)stdex::src2dst(m_mappingSourceHex, to )); m_destination->SetSelection( m_selDestination.first = (long)MapToDestination(from), m_selDestination.second = (long)MapToDestination(to )); m_destinationHex->SetSelection( - m_selDestinationHex.first = (long)m_mappingDestinationHex.to_dst(m_selDestination.first ), - m_selDestinationHex.second = (long)m_mappingDestinationHex.to_dst(m_selDestination.second)); + m_selDestinationHex.first = (long)stdex::src2dst(m_mappingDestinationHex, m_selDestination.first ), + m_selDestinationHex.second = (long)stdex::src2dst(m_mappingDestinationHex, m_selDestination.second)); } } @@ -230,16 +232,16 @@ void wxZRColaComposerPanel::OnSourceHexPaint(wxPaintEvent& event) m_selSourceHex.second = to; m_source->SetSelection( - m_selSource.first = (long)m_mappingSourceHex.to_src(from), - m_selSource.second = (long)m_mappingSourceHex.to_src(to )); + m_selSource.first = (long)stdex::dst2src(m_mappingSourceHex, from), + m_selSource.second = (long)stdex::dst2src(m_mappingSourceHex, to )); m_destination->SetSelection( m_selDestination.first = (long)MapToDestination(m_selSource.first ), m_selDestination.second = (long)MapToDestination(m_selSource.second)); m_destinationHex->SetSelection( - m_selDestinationHex.first = (long)m_mappingDestinationHex.to_dst(m_selDestination.first ), - m_selDestinationHex.second = (long)m_mappingDestinationHex.to_dst(m_selDestination.second)); + m_selDestinationHex.first = (long)stdex::src2dst(m_mappingDestinationHex, m_selDestination.first ), + m_selDestinationHex.second = (long)stdex::src2dst(m_mappingDestinationHex, m_selDestination.second)); } } @@ -276,16 +278,16 @@ void wxZRColaComposerPanel::OnDestinationPaint(wxPaintEvent& event) m_selDestination.second = to; m_destinationHex->SetSelection( - m_selDestinationHex.first = (long)m_mappingDestinationHex.to_dst(from), - m_selDestinationHex.second = (long)m_mappingDestinationHex.to_dst(to )); + m_selDestinationHex.first = (long)stdex::src2dst(m_mappingDestinationHex, from), + m_selDestinationHex.second = (long)stdex::src2dst(m_mappingDestinationHex, to )); m_source->SetSelection( m_selSource.first = (long)MapToSource(from), m_selSource.second = (long)MapToSource(to )); m_sourceHex->SetSelection( - m_selSourceHex.first = (long)m_mappingSourceHex.to_dst(m_selSource.first ), - m_selSourceHex.second = (long)m_mappingSourceHex.to_dst(m_selSource.second)); + m_selSourceHex.first = (long)stdex::src2dst(m_mappingSourceHex, m_selSource.first ), + m_selSourceHex.second = (long)stdex::src2dst(m_mappingSourceHex, m_selSource.second)); } } @@ -303,16 +305,16 @@ void wxZRColaComposerPanel::OnDestinationHexPaint(wxPaintEvent& event) m_selDestinationHex.second = to; m_destination->SetSelection( - m_selDestination.first = (long)m_mappingDestinationHex.to_src(from), - m_selDestination.second = (long)m_mappingDestinationHex.to_src(to )); + m_selDestination.first = (long)stdex::dst2src(m_mappingDestinationHex, from), + m_selDestination.second = (long)stdex::dst2src(m_mappingDestinationHex, to )); m_source->SetSelection( m_selSource.first = (long)MapToSource(m_selDestination.first ), m_selSource.second = (long)MapToSource(m_selDestination.second)); m_sourceHex->SetSelection( - m_selSourceHex.first = (long)m_mappingSourceHex.to_dst(m_selSource.first ), - m_selSourceHex.second = (long)m_mappingSourceHex.to_dst(m_selSource.second)); + m_selSourceHex.first = (long)stdex::src2dst(m_mappingSourceHex, m_selSource.first ), + m_selSourceHex.second = (long)stdex::src2dst(m_mappingSourceHex, m_selSource.second)); } } @@ -416,8 +418,8 @@ void wxZRColaComposerPanel::SetHexValue(wxTextCtrl *wnd, std::pair & wnd->SetValue(hex); wnd->SetSelection( - range.first = (long)mapping.to_dst(from), - range.second = (long)mapping.to_dst(to )); + range.first = (long)stdex::src2dst(mapping, from), + range.second = (long)stdex::src2dst(mapping, to )); } diff --git a/ZRCola/zrcolacomppnl.h b/ZRCola/zrcolacomppnl.h index b49a785..151c751 100644 --- a/ZRCola/zrcolacomppnl.h +++ b/ZRCola/zrcolacomppnl.h @@ -97,7 +97,7 @@ protected: inline size_t wxZRColaComposerPanel::MapToDestination(_In_ size_t src) const { for (auto m = m_mapping.cbegin(), m_end = m_mapping.cend(); m != m_end; ++m) - src = m->to_dst(src); + src = stdex::src2dst(*m, src); return src; } @@ -106,7 +106,7 @@ inline size_t wxZRColaComposerPanel::MapToDestination(_In_ size_t src) const inline size_t wxZRColaComposerPanel::MapToSource(_In_ size_t dst) const { for (auto m = m_mapping.crbegin(), m_end = m_mapping.crend(); m != m_end; ++m) - dst = m->to_src(dst); + dst = stdex::dst2src(*m, dst); return dst; } diff --git a/lib/libZRCola/build/libZRCola.vcxproj b/lib/libZRCola/build/libZRCola.vcxproj index 45f4cbd..d7d4dcd 100644 --- a/lib/libZRCola/build/libZRCola.vcxproj +++ b/lib/libZRCola/build/libZRCola.vcxproj @@ -66,7 +66,6 @@ - Create diff --git a/lib/libZRCola/build/libZRCola.vcxproj.filters b/lib/libZRCola/build/libZRCola.vcxproj.filters index ea574dd..7edbec4 100644 --- a/lib/libZRCola/build/libZRCola.vcxproj.filters +++ b/lib/libZRCola/build/libZRCola.vcxproj.filters @@ -14,9 +14,6 @@ Source Files - - Source Files - Source Files diff --git a/lib/libZRCola/include/zrcola/common.h b/lib/libZRCola/include/zrcola/common.h index 19e53e6..17ba9d8 100644 --- a/lib/libZRCola/include/zrcola/common.h +++ b/lib/libZRCola/include/zrcola/common.h @@ -10,6 +10,7 @@ #include #endif #include +#include #include #include #include @@ -714,57 +715,15 @@ namespace ZRCola { } }; - /// /// Source-destination index transformation mapping /// - struct mapping { - public: - size_t src; ///< Character index in source string - size_t dst; ///< Character index in destination string - - inline mapping() : src(0), dst(0) {}; - inline mapping(_In_ size_t s, _In_ size_t d) : src(s), dst(d) {} - - /// - /// Reverses source and destination indexes - /// - inline void invert() { size_t tmp = src; src = dst; dst = tmp; } - }; - + using mapping = stdex::mapping; /// /// A vector for destination-source index transformation mapping /// - class mapping_vector : public std::vector { - public: - /// - /// Transforms character index of destination to source - /// - /// \param[in] decmp Character index in destination string - /// - /// \returns Character index in source string - /// - size_t to_src(_In_ size_t dst) const; - - /// - /// Transforms source index to destination index - /// - /// \param[in] cmp Character index in source string - /// - /// \returns Character index in destination string - /// - size_t to_dst(_In_ size_t src) const; - - /// - /// Reverses source and destination indexes - /// - inline void invert() - { - for (iterator i = begin(), iEnd = end(); i != iEnd; ++i) - i->invert(); - } - }; + using mapping_vector = std::vector; /// /// Binary compares two strings diff --git a/lib/libZRCola/src/mapping.cpp b/lib/libZRCola/src/mapping.cpp deleted file mode 100644 index b95393d..0000000 --- a/lib/libZRCola/src/mapping.cpp +++ /dev/null @@ -1,68 +0,0 @@ -/* - SPDX-License-Identifier: GPL-3.0-or-later - Copyright © 2015-2022 Amebis -*/ - -#include "pch.h" - - -size_t ZRCola::mapping_vector::to_src(_In_ size_t dst) const -{ - if (empty()) { - // One-to-one mapping. - return dst; - } - - for (size_type l = 0, r = size();;) { - if (l < r) { - size_type m = (l + r) / 2; - const mapping &el = (*this)[m]; - - if ( dst < el.dst) r = m; - else if (el.dst < dst) l = m + 1; - else { - // An exact match found. - return el.src; - } - } else if (l) { - // We found a map interval. - const mapping &el = (*this)[l - 1]; - return el.src + (dst - el.dst); - } else { - // The destination character index is left of the first transformation. - const mapping &el = (*this)[0]; - return std::min(dst, el.src); - } - } -} - - -size_t ZRCola::mapping_vector::to_dst(_In_ size_t src) const -{ - if (empty()) { - // One-to-one mapping. - return src; - } - - for (size_type l = 0, r = size();;) { - if (l < r) { - size_type m = (l + r) / 2; - const mapping &el = (*this)[m]; - - if ( src < el.src) r = m; - else if (el.src < src) l = m + 1; - else { - // An exact match found. - return el.dst; - } - } else if (l) { - // We found a map interval. - const mapping &el = (*this)[l - 1]; - return el.dst + (src - el.src); - } else { - // The source character index is left of the first transformation. - const mapping &el = (*this)[0]; - return std::min(src, el.dst); - } - } -} diff --git a/lib/stdex b/lib/stdex index c6c7498..c1616b0 160000 --- a/lib/stdex +++ b/lib/stdex @@ -1 +1 @@ -Subproject commit c6c7498562a47d80ebcbabba3e714ee37d2ca9c0 +Subproject commit c1616b032e9597b072de6fae634ef242a6a67b1d