[coding] Rename non-empty string serialization.

Signed-off-by: Viktor Govako <viktor.govako@gmail.com>
This commit is contained in:
Viktor Govako
2025-07-02 22:05:34 -03:00
committed by Konstantin Pastbin
parent 7b4862b442
commit 1d29e7816a
8 changed files with 24 additions and 36 deletions

View File

@@ -22,7 +22,7 @@ BoundaryPostcodesEnricher::BoundaryPostcodesEnricher(std::string const & boundar
while (src.Size() > 0) while (src.Size() > 0)
{ {
std::string postcode; std::string postcode;
utils::ReadString(src, postcode); rw::ReadNonEmpty(src, postcode);
std::vector<m2::PointD> geometry; std::vector<m2::PointD> geometry;
rw::ReadVectorOfPOD(src, geometry); rw::ReadVectorOfPOD(src, geometry);
CHECK(!postcode.empty() && !geometry.empty(), ()); CHECK(!postcode.empty() && !geometry.empty(), ());

View File

@@ -57,7 +57,7 @@ void BoundaryPostcodeCollector::Save()
FileWriter writer(GetFilename()); FileWriter writer(GetFilename());
for (auto const & p : m_data) for (auto const & p : m_data)
{ {
utils::WriteString(writer, p.first); rw::WriteNonEmpty(writer, p.first);
rw::WriteVectorOfPOD(writer, p.second); rw::WriteVectorOfPOD(writer, p.second);
} }
} }

View File

@@ -91,7 +91,7 @@ unordered_map<string, vector<m2::PointD>> Read(string const & dumpFilename)
while (src.Size() > 0) while (src.Size() > 0)
{ {
string postcode; string postcode;
utils::ReadString(src, postcode); rw::ReadNonEmpty(src, postcode);
vector<m2::PointD> geometry; vector<m2::PointD> geometry;
rw::ReadVectorOfPOD(src, geometry); rw::ReadVectorOfPOD(src, geometry);
result.emplace(std::move(postcode), std::move(geometry)); result.emplace(std::move(postcode), std::move(geometry));

View File

@@ -14,7 +14,6 @@
#include <optional> #include <optional>
#include <set> #include <set>
#include <string> #include <string>
#include <unordered_map>
#include <vector> #include <vector>
namespace routing namespace routing
@@ -74,7 +73,7 @@ void Save(Sink & sink, ConditionalRAVectorT const & ac)
for (auto const & e : ac) for (auto const & e : ac)
{ {
Save(sink, e.m_accessType); Save(sink, e.m_accessType);
utils::WriteString(sink, e.m_openingHours); rw::WriteNonEmpty(sink, e.m_openingHours);
} }
} }
@@ -86,7 +85,7 @@ void Load(Source & src, ConditionalRAVectorT & vec)
for (uint32_t i = 0; i < count; ++i) for (uint32_t i = 0; i < count; ++i)
{ {
Load(src, vec[i].m_accessType); Load(src, vec[i].m_accessType);
utils::ReadString(src, vec[i].m_openingHours); rw::ReadNonEmpty(src, vec[i].m_openingHours);
} }
} }

View File

@@ -1,46 +1,35 @@
#pragma once #pragma once
#include "coding/reader.hpp" #include "coding/read_write_utils.hpp"
#include "coding/varint.hpp" #include "coding/varint.hpp"
#include "coding/writer.hpp"
#include "base/assert.hpp" #include "base/assert.hpp"
#include "base/buffer_vector.hpp" #include "base/buffer_vector.hpp"
#include "base/control_flow.hpp" #include "base/control_flow.hpp"
#include <map>
#include <string> #include <string>
#include <string_view> #include <string_view>
#include <vector> #include <vector>
namespace utils namespace rw
{ {
template <class TSink, bool EnableExceptions = false> template <class TSink>
void WriteString(TSink & sink, std::string const & s) void WriteNonEmpty(TSink & sink, std::string const & s)
{ {
if (EnableExceptions && s.empty()) CHECK(!s.empty(), ());
MYTHROW(Writer::WriteException, ("String is empty"));
else
CHECK(!s.empty(), ());
size_t const sz = s.size(); size_t const sz = s.size();
WriteVarUint(sink, static_cast<uint32_t>(sz - 1)); WriteVarUint(sink, static_cast<uint32_t>(sz - 1));
sink.Write(s.c_str(), sz); sink.Write(s.c_str(), sz);
} }
template <class TSource, bool EnableExceptions = false> template <class TSource>
void ReadString(TSource & src, std::string & s) void ReadNonEmpty(TSource & src, std::string & s)
{ {
uint32_t const sz = ReadVarUint<uint32_t>(src) + 1; uint32_t const sz = ReadVarUint<uint32_t>(src) + 1;
s.resize(sz); s.resize(sz);
src.Read(&s[0], sz); src.Read(&s[0], sz);
if (EnableExceptions && s.empty())
MYTHROW(Reader::ReadException, ("String is empty"));
else
CHECK(!s.empty(), ());
} }
} // namespace utils } // namespace rw
// A class to store strings in multiple languages. // A class to store strings in multiple languages.
// May be used e.g. to store several translations of a feature's name. // May be used e.g. to store several translations of a feature's name.
@@ -201,15 +190,14 @@ public:
/// @name Used for serdes. /// @name Used for serdes.
/// @{ /// @{
template <class TSink> template <class TSink>
void Write(TSink & sink) const void WriteNonEmpty(TSink & sink) const
{ {
utils::WriteString(sink, m_s); rw::WriteNonEmpty(sink, m_s);
} }
template <class TSource> template <class TSource>
void Read(TSource & src) void ReadNonEmpty(TSource & src)
{ {
utils::ReadString(src, m_s); rw::ReadNonEmpty(src, m_s);
} }
std::string const & GetBuffer() const { return m_s; } std::string const & GetBuffer() const { return m_s; }

View File

@@ -176,7 +176,7 @@ struct FeatureParamsBase
using namespace feature; using namespace feature;
if (header & HEADER_MASK_HAS_NAME) if (header & HEADER_MASK_HAS_NAME)
name.Write(sink); name.WriteNonEmpty(sink);
if (header & HEADER_MASK_HAS_LAYER) if (header & HEADER_MASK_HAS_LAYER)
WriteToSink(sink, layer); WriteToSink(sink, layer);
@@ -187,7 +187,7 @@ struct FeatureParamsBase
switch (headerGeomType) switch (headerGeomType)
{ {
case HeaderGeomType::Point: WriteToSink(sink, rank); break; case HeaderGeomType::Point: WriteToSink(sink, rank); break;
case HeaderGeomType::Line: utils::WriteString(sink, ref); break; case HeaderGeomType::Line: rw::WriteNonEmpty(sink, ref); break;
case HeaderGeomType::Area: case HeaderGeomType::Area:
case HeaderGeomType::PointEx: house.Write(sink); break; case HeaderGeomType::PointEx: house.Write(sink); break;
} }
@@ -200,7 +200,7 @@ struct FeatureParamsBase
using namespace feature; using namespace feature;
if (header & HEADER_MASK_HAS_NAME) if (header & HEADER_MASK_HAS_NAME)
name.Read(src); name.ReadNonEmpty(src);
if (header & HEADER_MASK_HAS_LAYER) if (header & HEADER_MASK_HAS_LAYER)
layer = ReadPrimitiveFromSource<int8_t>(src); layer = ReadPrimitiveFromSource<int8_t>(src);
@@ -211,7 +211,7 @@ struct FeatureParamsBase
switch (headerGeomType) switch (headerGeomType)
{ {
case HeaderGeomType::Point: rank = ReadPrimitiveFromSource<uint8_t>(src); break; case HeaderGeomType::Point: rank = ReadPrimitiveFromSource<uint8_t>(src); break;
case HeaderGeomType::Line: utils::ReadString(src, ref); break; case HeaderGeomType::Line: rw::ReadNonEmpty(src, ref); break;
case HeaderGeomType::Area: case HeaderGeomType::Area:
case HeaderGeomType::PointEx: house.Read(src); break; case HeaderGeomType::PointEx: house.Read(src); break;
} }

View File

@@ -46,7 +46,7 @@ public:
for (auto const & it : m_metadata) for (auto const & it : m_metadata)
{ {
WriteVarUint(sink, static_cast<uint32_t>(it.first)); WriteVarUint(sink, static_cast<uint32_t>(it.first));
utils::WriteString(sink, it.second); rw::WriteNonEmpty(sink, it.second);
} }
} }
@@ -57,7 +57,7 @@ public:
for (size_t i = 0; i < sz; ++i) for (size_t i = 0; i < sz; ++i)
{ {
auto const key = ReadVarUint<uint32_t>(src); auto const key = ReadVarUint<uint32_t>(src);
utils::ReadString(src, m_metadata[key]); rw::ReadNonEmpty(src, m_metadata[key]);
} }
} }

View File

@@ -1,6 +1,7 @@
#pragma once #pragma once
#include "coding/string_utf8_multilang.hpp" #include "coding/string_utf8_multilang.hpp"
#include "coding/writer.hpp"
#include "geometry/point2d.hpp" #include "geometry/point2d.hpp"
#include "geometry/point_with_altitude.hpp" #include "geometry/point_with_altitude.hpp"