From bf73b69808a909153ca4ed2c6f2e08d0cb8cd021 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89milie=20Feral?= Date: Mon, 20 Jan 2020 12:06:05 +0100 Subject: [PATCH] [poincare] Units: change signature of serialize to follow same conventions as Expression::serialize --- poincare/include/poincare/unit.h | 4 ++-- poincare/src/unit.cpp | 14 ++++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/poincare/include/poincare/unit.h b/poincare/include/poincare/unit.h index 2c5eec7ac..3f016e830 100644 --- a/poincare/include/poincare/unit.h +++ b/poincare/include/poincare/unit.h @@ -30,7 +30,7 @@ public: {} const char * symbol() const { return m_symbol; } const int8_t exponent() const { return m_exponent; } - size_t serialize(char * buffer, size_t length) const; + int serialize(char * buffer, int bufferSize) const; private: const char * m_symbol; int8_t m_exponent; @@ -51,7 +51,7 @@ public: const Prefix * allowedPrefixesUpperBound() const { return m_allowedPrefixesUpperBound; } bool canParse(const char * symbol, size_t length, const Prefix * * prefix) const; - size_t serialize(char * buffer, size_t length, const Prefix * prefix) const; + int serialize(char * buffer, int bufferSize, const Prefix * prefix) const; private: const char * m_rootSymbol; const char * m_definition; diff --git a/poincare/src/unit.cpp b/poincare/src/unit.cpp index 97a715817..4a79aa1e0 100644 --- a/poincare/src/unit.cpp +++ b/poincare/src/unit.cpp @@ -10,8 +10,9 @@ namespace Poincare { static inline int minInt(int x, int y) { return x < y ? x : y; } -size_t UnitNode::Prefix::serialize(char * buffer, size_t length) const { - return minInt(strlcpy(buffer, m_symbol, length), length - 1); +int UnitNode::Prefix::serialize(char * buffer, int bufferSize) const { + assert(bufferSize >= 0); + return minInt(strlcpy(buffer, m_symbol, bufferSize), bufferSize - 1); } bool UnitNode::Representative::canParse(const char * symbol, size_t length, @@ -31,11 +32,12 @@ bool UnitNode::Representative::canParse(const char * symbol, size_t length, return false; } -size_t UnitNode::Representative::serialize(char * buffer, size_t length, const Prefix * prefix) const { - size_t prefixLength = prefix->serialize(buffer, length); +int UnitNode::Representative::serialize(char * buffer, int bufferSize, const Prefix * prefix) const { + int prefixLength = prefix->serialize(buffer, bufferSize); + assert(prefixLength < bufferSize); buffer += prefixLength; - length -= prefixLength; - return prefixLength + minInt(strlcpy(buffer, m_rootSymbol, length), length - 1); + bufferSize -= prefixLength; + return prefixLength + minInt(strlcpy(buffer, m_rootSymbol, bufferSize), bufferSize - 1); } bool UnitNode::Dimension::canParse(const char * symbol, size_t length,