add min_width parameter to methods converting int to string
This commit is contained in:
parent
5d2788d0d8
commit
c3b7ab5793
|
@ -5,7 +5,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2021, Tomasz Sowa
|
* Copyright (c) 2021-2022, Tomasz Sowa
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
@ -41,114 +41,114 @@
|
||||||
namespace pt
|
namespace pt
|
||||||
{
|
{
|
||||||
|
|
||||||
std::string to_str(unsigned long long value, int base)
|
std::string to_str(unsigned long long value, int base, size_t min_width)
|
||||||
{
|
{
|
||||||
std::string res;
|
std::string res;
|
||||||
Toa(value, res, false, base);
|
Toa(value, res, false, base, min_width);
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::string to_str(long long value, int base)
|
std::string to_str(long long value, int base, size_t min_width)
|
||||||
{
|
{
|
||||||
std::string res;
|
std::string res;
|
||||||
Toa(value, res, false, base);
|
Toa(value, res, false, base, min_width);
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::string to_str(unsigned long value, int base)
|
std::string to_str(unsigned long value, int base, size_t min_width)
|
||||||
{
|
{
|
||||||
return to_str(static_cast<unsigned long long>(value), base);
|
return to_str(static_cast<unsigned long long>(value), base, min_width);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::string to_str(long value, int base)
|
std::string to_str(long value, int base, size_t min_width)
|
||||||
{
|
{
|
||||||
return to_str(static_cast<long long>(value), base);
|
return to_str(static_cast<long long>(value), base, min_width);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::string to_str(unsigned int value, int base)
|
std::string to_str(unsigned int value, int base, size_t min_width)
|
||||||
{
|
{
|
||||||
return to_str(static_cast<unsigned long long>(value), base);
|
return to_str(static_cast<unsigned long long>(value), base, min_width);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::string to_str(int value, int base)
|
std::string to_str(int value, int base, size_t min_width)
|
||||||
{
|
{
|
||||||
return to_str(static_cast<long long>(value), base);
|
return to_str(static_cast<long long>(value), base, min_width);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::string to_str(unsigned short value, int base)
|
std::string to_str(unsigned short value, int base, size_t min_width)
|
||||||
{
|
{
|
||||||
return to_str(static_cast<unsigned long long>(value), base);
|
return to_str(static_cast<unsigned long long>(value), base, min_width);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::string to_str(short value, int base)
|
std::string to_str(short value, int base, size_t min_width)
|
||||||
{
|
{
|
||||||
return to_str(static_cast<long long>(value), base);
|
return to_str(static_cast<long long>(value), base, min_width);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
std::wstring to_wstr(unsigned long long value, int base)
|
std::wstring to_wstr(unsigned long long value, int base, size_t min_width)
|
||||||
{
|
{
|
||||||
std::wstring res;
|
std::wstring res;
|
||||||
Toa(value, res, false, base);
|
Toa(value, res, false, base, min_width);
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::wstring to_wstr(long long value, int base)
|
std::wstring to_wstr(long long value, int base, size_t min_width)
|
||||||
{
|
{
|
||||||
std::wstring res;
|
std::wstring res;
|
||||||
Toa(value, res, false, base);
|
Toa(value, res, false, base, min_width);
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::wstring to_wstr(unsigned long value, int base)
|
std::wstring to_wstr(unsigned long value, int base, size_t min_width)
|
||||||
{
|
{
|
||||||
return to_wstr(static_cast<unsigned long long>(value), base);
|
return to_wstr(static_cast<unsigned long long>(value), base, min_width);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::wstring to_wstr(long value, int base)
|
std::wstring to_wstr(long value, int base, size_t min_width)
|
||||||
{
|
{
|
||||||
return to_wstr(static_cast<long long>(value), base);
|
return to_wstr(static_cast<long long>(value), base, min_width);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::wstring to_wstr(unsigned int value, int base)
|
std::wstring to_wstr(unsigned int value, int base, size_t min_width)
|
||||||
{
|
{
|
||||||
return to_wstr(static_cast<unsigned long long>(value), base);
|
return to_wstr(static_cast<unsigned long long>(value), base, min_width);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::wstring to_wstr(int value, int base)
|
std::wstring to_wstr(int value, int base, size_t min_width)
|
||||||
{
|
{
|
||||||
return to_wstr(static_cast<long long>(value), base);
|
return to_wstr(static_cast<long long>(value), base, min_width);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::wstring to_wstr(unsigned short value, int base)
|
std::wstring to_wstr(unsigned short value, int base, size_t min_width)
|
||||||
{
|
{
|
||||||
return to_wstr(static_cast<unsigned long long>(value), base);
|
return to_wstr(static_cast<unsigned long long>(value), base, min_width);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
std::wstring to_wstr(short value, int base)
|
std::wstring to_wstr(short value, int base, size_t min_width)
|
||||||
{
|
{
|
||||||
return to_wstr(static_cast<long long>(value), base);
|
return to_wstr(static_cast<long long>(value), base, min_width);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -5,7 +5,7 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2012-2021, Tomasz Sowa
|
* Copyright (c) 2012-2022, Tomasz Sowa
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
@ -52,8 +52,9 @@ namespace pt
|
||||||
|
|
||||||
// if the buffer is too small it will be terminated at the beginning (empty string)
|
// if the buffer is too small it will be terminated at the beginning (empty string)
|
||||||
// and the function returns false
|
// and the function returns false
|
||||||
|
// min_width - if greater than zero then it is used for zero padding
|
||||||
template<class CharType>
|
template<class CharType>
|
||||||
bool Toa(unsigned long 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 = nullptr, size_t min_width = 0)
|
||||||
{
|
{
|
||||||
size_t i1, i2;
|
size_t i1, i2;
|
||||||
long rest;
|
long rest;
|
||||||
|
@ -77,6 +78,14 @@ long rest;
|
||||||
}
|
}
|
||||||
while(value != 0 && i2 < buf_len);
|
while(value != 0 && i2 < buf_len);
|
||||||
|
|
||||||
|
if( min_width > 0 )
|
||||||
|
{
|
||||||
|
for( ; i2 < min_width && i2 < buf_len ; ++i2)
|
||||||
|
{
|
||||||
|
buffer[i2] = '0';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if( i2 >= buf_len )
|
if( i2 >= buf_len )
|
||||||
{
|
{
|
||||||
buffer[0] = 0; // ops, the buffer was too small
|
buffer[0] = 0; // ops, the buffer was too small
|
||||||
|
@ -106,7 +115,7 @@ return true;
|
||||||
// if the buffer is too small it will be terminated at the beginning (empty string)
|
// if the buffer is too small it will be terminated at the beginning (empty string)
|
||||||
// and the function returns false
|
// and the function returns false
|
||||||
template<class CharType>
|
template<class CharType>
|
||||||
bool Toa(long 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 = nullptr, size_t min_width = 0)
|
||||||
{
|
{
|
||||||
if( len_out )
|
if( len_out )
|
||||||
*len_out = 0;
|
*len_out = 0;
|
||||||
|
@ -126,7 +135,7 @@ bool Toa(long long value, CharType * buffer, size_t buf_len, int base = 10, size
|
||||||
is_sign = true;
|
is_sign = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool res = Toa(static_cast<unsigned long long>(value), buf, buf_len, base, len_out);
|
bool res = Toa(static_cast<unsigned long long>(value), buf, buf_len, base, len_out, min_width);
|
||||||
|
|
||||||
if( res )
|
if( res )
|
||||||
{
|
{
|
||||||
|
@ -146,44 +155,44 @@ return res;
|
||||||
|
|
||||||
|
|
||||||
template<class CharType>
|
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 value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0, size_t min_width = 0)
|
||||||
{
|
{
|
||||||
return Toa(static_cast<unsigned long long>(value), buffer, buf_len, base, len_out);
|
return Toa(static_cast<unsigned long long>(value), buffer, buf_len, base, len_out, min_width);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<class CharType>
|
template<class CharType>
|
||||||
bool Toa(long value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0)
|
bool Toa(long value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0, size_t min_width = 0)
|
||||||
{
|
{
|
||||||
return Toa(static_cast<long long>(value), buffer, buf_len, base, len_out);
|
return Toa(static_cast<long long>(value), buffer, buf_len, base, len_out, min_width);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
template<class CharType>
|
template<class CharType>
|
||||||
bool Toa(unsigned int value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0)
|
bool Toa(unsigned int value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0, size_t min_width = 0)
|
||||||
{
|
{
|
||||||
return Toa(static_cast<unsigned long long>(value), buffer, buf_len, base, len_out);
|
return Toa(static_cast<unsigned long long>(value), buffer, buf_len, base, len_out, min_width);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class CharType>
|
template<class CharType>
|
||||||
bool Toa(int value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0)
|
bool Toa(int value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0, size_t min_width = 0)
|
||||||
{
|
{
|
||||||
return Toa(static_cast<long long>(value), buffer, buf_len, base, len_out);
|
return Toa(static_cast<long long>(value), buffer, buf_len, base, len_out, min_width);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class CharType>
|
template<class CharType>
|
||||||
bool Toa(unsigned short value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0)
|
bool Toa(unsigned short value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0, size_t min_width = 0)
|
||||||
{
|
{
|
||||||
return Toa(static_cast<unsigned long long>(value), buffer, buf_len, base, len_out);
|
return Toa(static_cast<unsigned long long>(value), buffer, buf_len, base, len_out, min_width);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class CharType>
|
template<class CharType>
|
||||||
bool Toa(short value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0)
|
bool Toa(short value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0, size_t min_width = 0)
|
||||||
{
|
{
|
||||||
return Toa(static_cast<long long>(value), buffer, buf_len, base, len_out);
|
return Toa(static_cast<long long>(value), buffer, buf_len, base, len_out, min_width);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -192,7 +201,7 @@ bool Toa(short value, CharType * buffer, size_t buf_len, int base = 10, size_t *
|
||||||
|
|
||||||
|
|
||||||
template<class StringType>
|
template<class StringType>
|
||||||
void Toa(unsigned long long value, StringType & res, bool clear_string = true, int base = 10)
|
void Toa(unsigned long long value, StringType & res, bool clear_string = true, int base = 10, size_t min_width = 0)
|
||||||
{
|
{
|
||||||
typename StringType::value_type buffer[50];
|
typename StringType::value_type buffer[50];
|
||||||
size_t buffer_len = sizeof(buffer) / sizeof(wchar_t);
|
size_t buffer_len = sizeof(buffer) / sizeof(wchar_t);
|
||||||
|
@ -204,13 +213,13 @@ void Toa(unsigned long long value, StringType & res, bool clear_string = true, i
|
||||||
* the size of the buffer is sufficient so the status should always be true
|
* the size of the buffer is sufficient so the status should always be true
|
||||||
*/
|
*/
|
||||||
size_t len_out;
|
size_t len_out;
|
||||||
Toa(value, buffer, buffer_len, base, &len_out);
|
Toa(value, buffer, buffer_len, base, &len_out, min_width);
|
||||||
res.append(buffer, len_out);
|
res.append(buffer, len_out);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class StringType>
|
template<class StringType>
|
||||||
void Toa(long long value, StringType & res, bool clear_string = true, int base = 10)
|
void Toa(long long value, StringType & res, bool clear_string = true, int base = 10, size_t min_width = 0)
|
||||||
{
|
{
|
||||||
typename StringType::value_type buffer[50];
|
typename StringType::value_type buffer[50];
|
||||||
size_t buffer_len = sizeof(buffer) / sizeof(wchar_t);
|
size_t buffer_len = sizeof(buffer) / sizeof(wchar_t);
|
||||||
|
@ -222,71 +231,71 @@ void Toa(long long value, StringType & res, bool clear_string = true, int base =
|
||||||
* the size of the buffer is sufficient so the status should always be true
|
* the size of the buffer is sufficient so the status should always be true
|
||||||
*/
|
*/
|
||||||
size_t len_out;
|
size_t len_out;
|
||||||
Toa(value, buffer, buffer_len, base, &len_out);
|
Toa(value, buffer, buffer_len, base, &len_out, min_width);
|
||||||
res.append(buffer, len_out);
|
res.append(buffer, len_out);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class StringType>
|
template<class StringType>
|
||||||
void Toa(unsigned long value, StringType & res, bool clear_string = true, int base = 10)
|
void Toa(unsigned long value, StringType & res, bool clear_string = true, int base = 10, size_t min_width = 0)
|
||||||
{
|
{
|
||||||
Toa(static_cast<unsigned long long>(value), res, clear_string, base);
|
Toa(static_cast<unsigned long long>(value), res, clear_string, base, min_width);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class StringType>
|
template<class StringType>
|
||||||
void Toa(long value, StringType & res, bool clear_string = true, int base = 10)
|
void Toa(long value, StringType & res, bool clear_string = true, int base = 10, size_t min_width = 0)
|
||||||
{
|
{
|
||||||
Toa(static_cast<long long>(value), res, clear_string, base);
|
Toa(static_cast<long long>(value), res, clear_string, base, min_width);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class StringType>
|
template<class StringType>
|
||||||
void Toa(unsigned int value, StringType & res, bool clear_string = true, int base = 10)
|
void Toa(unsigned int value, StringType & res, bool clear_string = true, int base = 10, size_t min_width = 0)
|
||||||
{
|
{
|
||||||
Toa(static_cast<unsigned long long>(value), res, clear_string, base);
|
Toa(static_cast<unsigned long long>(value), res, clear_string, base, min_width);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class StringType>
|
template<class StringType>
|
||||||
void Toa(int value, StringType & res, bool clear_string = true, int base = 10)
|
void Toa(int value, StringType & res, bool clear_string = true, int base = 10, size_t min_width = 0)
|
||||||
{
|
{
|
||||||
Toa(static_cast<long long>(value), res, clear_string, base);
|
Toa(static_cast<long long>(value), res, clear_string, base, min_width);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class StringType>
|
template<class StringType>
|
||||||
void Toa(unsigned short value, StringType & res, bool clear_string = true, int base = 10)
|
void Toa(unsigned short value, StringType & res, bool clear_string = true, int base = 10, size_t min_width = 0)
|
||||||
{
|
{
|
||||||
Toa(static_cast<unsigned long long>(value), res, clear_string, base);
|
Toa(static_cast<unsigned long long>(value), res, clear_string, base, min_width);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
template<class StringType>
|
template<class StringType>
|
||||||
void Toa(short value, StringType & res, bool clear_string = true, int base = 10)
|
void Toa(short value, StringType & res, bool clear_string = true, int base = 10, size_t min_width = 0)
|
||||||
{
|
{
|
||||||
Toa(static_cast<long long>(value), res, clear_string, base);
|
Toa(static_cast<long long>(value), res, clear_string, base, min_width);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
std::string to_str(unsigned long long value, int base = 10);
|
std::string to_str(unsigned long long value, int base = 10, size_t min_width = 0);
|
||||||
std::string to_str(long long value, int base = 10);
|
std::string to_str(long long value, int base = 10, size_t min_width = 0);
|
||||||
std::string to_str(unsigned long value, int base = 10);
|
std::string to_str(unsigned long value, int base = 10, size_t min_width = 0);
|
||||||
std::string to_str(long value, int base = 10);
|
std::string to_str(long value, int base = 10, size_t min_width = 0);
|
||||||
std::string to_str(unsigned int value, int base = 10);
|
std::string to_str(unsigned int value, int base = 10, size_t min_width = 0);
|
||||||
std::string to_str(int value, int base = 10);
|
std::string to_str(int value, int base = 10, size_t min_width = 0);
|
||||||
std::string to_str(unsigned short value, int base = 10);
|
std::string to_str(unsigned short value, int base = 10, size_t min_width = 0);
|
||||||
std::string to_str(short value, int base = 10);
|
std::string to_str(short value, int base = 10, size_t min_width = 0);
|
||||||
|
|
||||||
std::wstring to_wstr(unsigned long long value, int base = 10);
|
std::wstring to_wstr(unsigned long long value, int base = 10, size_t min_width = 0);
|
||||||
std::wstring to_wstr(long long value, int base = 10);
|
std::wstring to_wstr(long long value, int base = 10, size_t min_width = 0);
|
||||||
std::wstring to_wstr(unsigned long value, int base = 10);
|
std::wstring to_wstr(unsigned long value, int base = 10, size_t min_width = 0);
|
||||||
std::wstring to_wstr(long value, int base = 10);
|
std::wstring to_wstr(long value, int base = 10, size_t min_width = 0);
|
||||||
std::wstring to_wstr(unsigned int value, int base = 10);
|
std::wstring to_wstr(unsigned int value, int base = 10, size_t min_width = 0);
|
||||||
std::wstring to_wstr(int value, int base = 10);
|
std::wstring to_wstr(int value, int base = 10, size_t min_width = 0);
|
||||||
std::wstring to_wstr(unsigned short value, int base = 10);
|
std::wstring to_wstr(unsigned short value, int base = 10, size_t min_width = 0);
|
||||||
std::wstring to_wstr(short value, int base = 10);
|
std::wstring to_wstr(short value, int base = 10, size_t min_width = 0);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue