Make mapping reusable

Signed-off-by: Simon Rozman <simon@rozman.si>
This commit is contained in:
Simon Rozman
2024-04-25 15:16:15 +02:00
parent 566d40bd05
commit 82906899de
7 changed files with 28 additions and 139 deletions

View File

@@ -66,7 +66,6 @@
<ClCompile Include="..\src\common.cpp" />
<ClCompile Include="..\src\highlight.cpp" />
<ClCompile Include="..\src\language.cpp" />
<ClCompile Include="..\src\mapping.cpp" />
<ClCompile Include="..\src\pch.cpp">
<PrecompiledHeader>Create</PrecompiledHeader>
</ClCompile>

View File

@@ -14,9 +14,6 @@
<ClCompile Include="..\src\pch.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\mapping.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="..\src\translate.cpp">
<Filter>Source Files</Filter>
</ClCompile>

View File

@@ -10,6 +10,7 @@
#include <Windows.h>
#endif
#include <stdex/compat.hpp>
#include <stdex/mapping.hpp>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
@@ -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<size_t>;
///
/// A vector for destination-source index transformation mapping
///
class mapping_vector : public std::vector<mapping> {
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<mapping>;
///
/// Binary compares two strings

View File

@@ -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<size_t>(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<size_t>(src, el.dst);
}
}
}