Don't use to_string conversion in requiresNameAnnounced
This commit is contained in:
parent
3dec680058
commit
742c32d936
@ -26,7 +26,8 @@ namespace guidance
|
|||||||
// Name Change Logic
|
// Name Change Logic
|
||||||
// Used both during Extraction as well as during Post-Processing
|
// Used both during Extraction as well as during Post-Processing
|
||||||
|
|
||||||
inline std::string longest_common_substring(const std::string &lhs, const std::string &rhs)
|
inline util::StringView longest_common_substring(const util::StringView &lhs,
|
||||||
|
const util::StringView &rhs)
|
||||||
{
|
{
|
||||||
if (lhs.empty() || rhs.empty())
|
if (lhs.empty() || rhs.empty())
|
||||||
return "";
|
return "";
|
||||||
@ -60,15 +61,15 @@ inline std::string longest_common_substring(const std::string &lhs, const std::s
|
|||||||
|
|
||||||
// TODO US-ASCII support only, no UTF-8 support
|
// TODO US-ASCII support only, no UTF-8 support
|
||||||
// While UTF-8 might work in some cases, we do not guarantee full functionality
|
// While UTF-8 might work in some cases, we do not guarantee full functionality
|
||||||
inline auto decompose(const std::string &lhs, const std::string &rhs)
|
template <typename StringView> inline auto decompose(const StringView &lhs, const StringView &rhs)
|
||||||
{
|
{
|
||||||
auto const lcs = longest_common_substring(lhs, rhs);
|
auto const lcs = longest_common_substring(lhs, rhs);
|
||||||
|
|
||||||
// trim spaces, transform to lower
|
// trim spaces, transform to lower
|
||||||
const auto trim = [](auto str) {
|
const auto trim = [](StringView view) {
|
||||||
// we compare suffixes based on this value, it might break UTF chars, but as long as we are
|
// we compare suffixes based on this value, it might break UTF chars, but as long as we are
|
||||||
// consistent in handling, we do not create bad results
|
// consistent in handling, we do not create bad results
|
||||||
boost::to_lower(str);
|
std::string str = boost::to_lower_copy(view.to_string());
|
||||||
auto front = str.find_first_not_of(" ");
|
auto front = str.find_first_not_of(" ");
|
||||||
|
|
||||||
if (front == std::string::npos)
|
if (front == std::string::npos)
|
||||||
@ -80,8 +81,7 @@ inline auto decompose(const std::string &lhs, const std::string &rhs)
|
|||||||
|
|
||||||
if (lcs.empty())
|
if (lcs.empty())
|
||||||
{
|
{
|
||||||
std::string empty = "";
|
return std::make_tuple(trim(lhs), trim(rhs), std::string(), std::string());
|
||||||
return std::make_tuple(trim(lhs), trim(rhs), empty, empty);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// find the common substring in both
|
// find the common substring in both
|
||||||
@ -92,32 +92,27 @@ inline auto decompose(const std::string &lhs, const std::string &rhs)
|
|||||||
BOOST_ASSERT(rhs_pos + lcs.size() <= rhs.size());
|
BOOST_ASSERT(rhs_pos + lcs.size() <= rhs.size());
|
||||||
|
|
||||||
// prefixes
|
// prefixes
|
||||||
std::string lhs_prefix = (lhs_pos > 0) ? lhs.substr(0, lhs_pos) : "";
|
auto lhs_prefix = (lhs_pos > 0) ? lhs.substr(0, lhs_pos) : StringView();
|
||||||
std::string rhs_prefix = (rhs_pos > 0) ? rhs.substr(0, rhs_pos) : "";
|
auto rhs_prefix = (rhs_pos > 0) ? rhs.substr(0, rhs_pos) : StringView();
|
||||||
|
|
||||||
// suffices
|
// suffices
|
||||||
std::string lhs_suffix = lhs.substr(lhs_pos + lcs.size());
|
auto lhs_suffix = lhs.substr(lhs_pos + lcs.size());
|
||||||
std::string rhs_suffix = rhs.substr(rhs_pos + lcs.size());
|
auto rhs_suffix = rhs.substr(rhs_pos + lcs.size());
|
||||||
|
|
||||||
lhs_prefix = trim(std::move(lhs_prefix));
|
return std::make_tuple(trim(lhs_prefix), trim(lhs_suffix), trim(rhs_prefix), trim(rhs_suffix));
|
||||||
lhs_suffix = trim(std::move(lhs_suffix));
|
|
||||||
rhs_prefix = trim(std::move(rhs_prefix));
|
|
||||||
rhs_suffix = trim(std::move(rhs_suffix));
|
|
||||||
|
|
||||||
return std::make_tuple(lhs_prefix, lhs_suffix, rhs_prefix, rhs_suffix);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Note: there is an overload without suffix checking below.
|
// Note: there is an overload without suffix checking below.
|
||||||
// (that's the reason we template the suffix table here)
|
// (that's the reason we template the suffix table here)
|
||||||
template <typename SuffixTable>
|
template <typename StringView, typename SuffixTable>
|
||||||
inline bool requiresNameAnnounced(const std::string &from_name,
|
inline bool requiresNameAnnounced(const StringView &from_name,
|
||||||
const std::string &from_ref,
|
const StringView &from_ref,
|
||||||
const std::string &from_pronunciation,
|
const StringView &from_pronunciation,
|
||||||
const std::string &from_exits,
|
const StringView &from_exits,
|
||||||
const std::string &to_name,
|
const StringView &to_name,
|
||||||
const std::string &to_ref,
|
const StringView &to_ref,
|
||||||
const std::string &to_pronunciation,
|
const StringView &to_pronunciation,
|
||||||
const std::string &to_exits,
|
const StringView &to_exits,
|
||||||
const SuffixTable &suffix_table)
|
const SuffixTable &suffix_table)
|
||||||
{
|
{
|
||||||
// first is empty and the second is not
|
// first is empty and the second is not
|
||||||
@ -134,7 +129,7 @@ inline bool requiresNameAnnounced(const std::string &from_name,
|
|||||||
boost::starts_with(from_name, to_name) || boost::starts_with(to_name, from_name);
|
boost::starts_with(from_name, to_name) || boost::starts_with(to_name, from_name);
|
||||||
|
|
||||||
const auto checkForPrefixOrSuffixChange =
|
const auto checkForPrefixOrSuffixChange =
|
||||||
[](const std::string &first, const std::string &second, const SuffixTable &suffix_table) {
|
[](const StringView &first, const StringView &second, const SuffixTable &suffix_table) {
|
||||||
std::string first_prefix, first_suffix, second_prefix, second_suffix;
|
std::string first_prefix, first_suffix, second_prefix, second_suffix;
|
||||||
std::tie(first_prefix, first_suffix, second_prefix, second_suffix) =
|
std::tie(first_prefix, first_suffix, second_prefix, second_suffix) =
|
||||||
decompose(first, second);
|
decompose(first, second);
|
||||||
@ -203,17 +198,17 @@ inline bool requiresNameAnnounced(const std::string &from_name,
|
|||||||
struct NopSuffixTable final
|
struct NopSuffixTable final
|
||||||
{
|
{
|
||||||
NopSuffixTable() {}
|
NopSuffixTable() {}
|
||||||
bool isSuffix(const std::string &) const { return false; }
|
bool isSuffix(const StringView &) const { return false; }
|
||||||
} static const table;
|
} static const table;
|
||||||
|
|
||||||
return requiresNameAnnounced(from_name,
|
return requiresNameAnnounced(util::StringView(from_name),
|
||||||
from_ref,
|
util::StringView(from_ref),
|
||||||
from_pronunciation,
|
util::StringView(from_pronunciation),
|
||||||
from_exits,
|
util::StringView(from_exits),
|
||||||
to_name,
|
util::StringView(to_name),
|
||||||
to_ref,
|
util::StringView(to_ref),
|
||||||
to_pronunciation,
|
util::StringView(to_pronunciation),
|
||||||
to_exits,
|
util::StringView(to_exits),
|
||||||
table);
|
table);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -225,37 +220,17 @@ inline bool requiresNameAnnounced(const NameID from_name_id,
|
|||||||
if (from_name_id == to_name_id)
|
if (from_name_id == to_name_id)
|
||||||
return false;
|
return false;
|
||||||
else
|
else
|
||||||
return requiresNameAnnounced(name_table.GetNameForID(from_name_id).to_string(),
|
return requiresNameAnnounced(name_table.GetNameForID(from_name_id),
|
||||||
name_table.GetRefForID(from_name_id).to_string(),
|
name_table.GetRefForID(from_name_id),
|
||||||
name_table.GetPronunciationForID(from_name_id).to_string(),
|
name_table.GetPronunciationForID(from_name_id),
|
||||||
name_table.GetExitsForID(from_name_id).to_string(),
|
name_table.GetExitsForID(from_name_id),
|
||||||
//
|
//
|
||||||
name_table.GetNameForID(to_name_id).to_string(),
|
name_table.GetNameForID(to_name_id),
|
||||||
name_table.GetRefForID(to_name_id).to_string(),
|
name_table.GetRefForID(to_name_id),
|
||||||
name_table.GetPronunciationForID(to_name_id).to_string(),
|
name_table.GetPronunciationForID(to_name_id),
|
||||||
name_table.GetExitsForID(to_name_id).to_string(),
|
name_table.GetExitsForID(to_name_id),
|
||||||
//
|
//
|
||||||
suffix_table);
|
suffix_table);
|
||||||
// FIXME: converts StringViews to strings since the name change heuristics mutates in place
|
|
||||||
}
|
|
||||||
|
|
||||||
inline bool requiresNameAnnounced(const NameID from_name_id,
|
|
||||||
const NameID to_name_id,
|
|
||||||
const util::NameTable &name_table)
|
|
||||||
{
|
|
||||||
if (from_name_id == to_name_id)
|
|
||||||
return false;
|
|
||||||
else
|
|
||||||
return requiresNameAnnounced(name_table.GetNameForID(from_name_id).to_string(),
|
|
||||||
name_table.GetRefForID(from_name_id).to_string(),
|
|
||||||
name_table.GetPronunciationForID(from_name_id).to_string(),
|
|
||||||
name_table.GetExitsForID(from_name_id).to_string(),
|
|
||||||
//
|
|
||||||
name_table.GetNameForID(to_name_id).to_string(),
|
|
||||||
name_table.GetRefForID(to_name_id).to_string(),
|
|
||||||
name_table.GetExitsForID(to_name_id).to_string(),
|
|
||||||
name_table.GetPronunciationForID(to_name_id).to_string());
|
|
||||||
// FIXME: converts StringViews to strings since the name change heuristics mutates in place
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace guidance
|
} // namespace guidance
|
||||||
|
Loading…
Reference in New Issue
Block a user