added: Toa() for long long, int, short and unsigned as well

fixed: when serializing Date the year has at least 4 digits
       e.g. 0001 when the year is equal to one




git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@1093 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
Tomasz Sowa 2018-04-22 21:22:55 +00:00
parent 38355a2830
commit bf4fdf6da7
3 changed files with 61 additions and 11 deletions

View File

@ -52,7 +52,7 @@ namespace PT
// if the buffer is too small it will be terminated at the beginning (empty string)
// and the function returns false
template<class CharType>
bool Toa(unsigned long value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0)
bool Toa(unsigned long long value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0)
{
size_t i1, i2;
long rest;
@ -105,7 +105,7 @@ return true;
// if the buffer is too small it will be terminated at the beginning (empty string)
// and the function returns false
template<class CharType>
bool Toa(long value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0)
bool Toa(long long value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0)
{
if( len_out )
*len_out = 0;
@ -125,7 +125,7 @@ bool Toa(long value, CharType * buffer, size_t buf_len, int base = 10, size_t *
is_sign = true;
}
bool res = Toa(static_cast<unsigned long>(value), buf, buf_len, base, len_out);
bool res = Toa(static_cast<unsigned long long>(value), buf, buf_len, base, len_out);
if( res )
{
@ -144,13 +144,47 @@ return res;
template<class CharType>
bool Toa(unsigned long value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0)
{
return Toa(static_cast<unsigned long long>(value), buffer, buf_len, base, len_out);
}
template<class CharType>
bool Toa(long value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0)
{
return Toa(static_cast<long long>(value), buffer, buf_len, base, len_out);
}
template<class CharType>
bool Toa(unsigned int value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0)
{
return Toa(static_cast<unsigned long long>(value), buffer, buf_len, base, len_out);
}
template<class CharType>
bool Toa(int value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0)
{
return Toa(static_cast<long long>(value), buffer, buf_len, base, len_out);
}
template<class CharType>
bool Toa(unsigned short value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0)
{
return Toa(static_cast<unsigned long long>(value), buffer, buf_len, base, len_out);
}
template<class CharType>
bool Toa(short value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0)
{
return Toa(static_cast<long long>(value), buffer, buf_len, base, len_out);
}

View File

@ -1,3 +1,4 @@
# DO NOT DELETE
date.o: date.h
date.o: date.h ../convert/convert.h ../convert/inttostr.h
date.o: ../convert/strtoint.h ../convert/text.h ../convert/misc.h

View File

@ -40,7 +40,7 @@
#include <ctime>
#include <string>
#include "convert/convert.h"
namespace PT
@ -493,6 +493,9 @@ private:
void AssertRange(int & val, int val_min, int val_max);
template<class Stream>
void SerializeInt(Stream & out, int val, size_t min_width) const;
template<class Stream>
void SerializeInt(Stream & out, int val) const;
@ -512,17 +515,29 @@ private:
template<class Stream>
void Date::SerializeInt(Stream & out, int val, size_t min_width) const
{
char buf[64];
size_t len;
if( Toa(val, buf, sizeof(buf) / sizeof(char), 10, &len) )
{
for(size_t i = len ; i < min_width ; ++i)
{
out << '0';
}
out << buf;
}
}
template<class Stream>
void Date::SerializeInt(Stream & out, int val) const
{
if( val >= 0 && val < 10 )
out << '0';
out << val;
SerializeInt(out, val, 2);
}
@ -530,8 +545,8 @@ void Date::SerializeInt(Stream & out, int val) const
template<class Stream>
void Date::SerializeYearMonthDay(Stream & out) const
{
// !! IMPROVE ME the year should be printed with 4 digits, e.g. 0001 when the year is equal to one
out << year << '-';
SerializeInt(out, year, 4);
out << '-';
SerializeInt(out, month);
out << '-';
SerializeInt(out, day);