Avoid some string copies

This commit is contained in:
Maarten L. Hekkelman
2026-02-21 14:25:13 +01:00
parent c3e1c46b63
commit 96f0ae694c
2 changed files with 10 additions and 15 deletions

View File

@@ -2091,11 +2091,11 @@ void category::write_cif(std::ostream &os, const std::vector<uint16_t> &order, b
if (v == nullptr)
continue;
if (v->str().find('\n') == std::string_view::npos)
if (v->is_string() and v->sv().find('\n') == std::string_view::npos)
{
std::size_t l = v->str().length();
std::size_t l = v->sv().length();
if (not sac_parser::is_unquoted_string(v->str()))
if (not sac_parser::is_unquoted_string(v->sv()))
l += 2;
if (l > 132)
@@ -2617,14 +2617,9 @@ void category::write_table(std::ostream &os, const std::vector<uint16_t> &order,
std::size_t w = itemWidths[cix];
std::string s;
auto iv = r->get(cix);
if (iv != nullptr)
if (auto iv = r->get(cix); iv != nullptr and not iv->empty())
s = iv->str();
if (s == "?" or s == ".")
s = "";
if (s.length() < w)
{
if (right_aligned[cix])

View File

@@ -25,6 +25,7 @@
*/
#include "cif++/validate.hpp"
#include "cif++/cif++.hpp"
#include <cassert>
@@ -200,7 +201,7 @@ int type_validator::compare(const item_value &a, const item_value &b) const
return a.compare(b, false);
return a.str().compare(b.str());
default:
throw std::runtime_error("invalid primitive type");
}
@@ -236,20 +237,19 @@ bool item_validator::validate_value(const item_value &value, std::error_code &ec
{
try
{
auto s = value.str();
if (not m_type->m_rx->match(s))
if (not m_type->m_rx->match(value.sv()))
ec = make_error_code(validation_error::value_does_not_match_rx);
}
catch (...)
{
ec = make_error_code(validation_error::value_does_not_match_rx);
}
if (ec == std::errc{} and not m_enums.empty() and m_enums.count(value.str()) == 0)
ec = make_error_code(validation_error::value_is_not_in_enumeration_list);
}
}
}
if (ec == std::errc{} and not m_enums.empty() and m_enums.count(value.str()) == 0)
ec = make_error_code(validation_error::value_is_not_in_enumeration_list);
}
return ec == std::errc{};