added: Toa() methods for converting to a string (new file convert/inttostr.cpp):

template<class StringType> void Toa(unsigned long long value, StringType & res, bool clear_string = true, int base = 10);
template<class StringType> void Toa(long long value, StringType & res, bool clear_string = true, int base = 10);
template<class StringType> void Toa(unsigned long value, StringType & res, bool clear_string = true, int base = 10);
template<class StringType> void Toa(long value, StringType & res, bool clear_string = true, int base = 10);
template<class StringType> void Toa(unsigned int value, StringType & res, bool clear_string = true, int base = 10);
template<class StringType> void Toa(int value, StringType & res, bool clear_string = true, int base = 10);
template<class StringType> void Toa(unsigned short value, StringType & res, bool clear_string = true, int base = 10);
template<class StringType> void Toa(short value, StringType & res, bool clear_string = true, int base = 10);
std::wstring Toa(unsigned long long value, int base);
std::wstring Toa(long long value, int base);
std::wstring Toa(unsigned long value, int base);
std::wstring Toa(long value, int base);
std::wstring Toa(unsigned int value, int base);
std::wstring Toa(int value, int base);
std::wstring Toa(unsigned short value, int base);
std::wstring Toa(short value, int base);
This commit is contained in:
2021-03-03 02:24:30 +01:00
parent d9a4fa34e2
commit 448ad42961
4 changed files with 198 additions and 2 deletions

View File

@@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2012-2017, Tomasz Sowa
* Copyright (c) 2012-2021, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -38,6 +38,7 @@
#ifndef headerfile_picotools_convert_inttostr
#define headerfile_picotools_convert_inttostr
#include <string>
@@ -188,6 +189,100 @@ bool Toa(short value, CharType * buffer, size_t buf_len, int base = 10, size_t *
template<class StringType>
void Toa(unsigned long long value, StringType & res, bool clear_string = true, int base = 10)
{
typename StringType::value_type buffer[50];
size_t buffer_len = sizeof(buffer) / sizeof(wchar_t);
if( clear_string )
res.clear();
/*
* the size of the buffer is sufficient so the status should always be true
*/
size_t len_out;
Toa(value, buffer, buffer_len, base, &len_out);
res.append(buffer, len_out);
}
template<class StringType>
void Toa(long long value, StringType & res, bool clear_string = true, int base = 10)
{
typename StringType::value_type buffer[50];
size_t buffer_len = sizeof(buffer) / sizeof(wchar_t);
if( clear_string )
res.clear();
/*
* the size of the buffer is sufficient so the status should always be true
*/
size_t len_out;
Toa(value, buffer, buffer_len, base, &len_out);
res.append(buffer, len_out);
}
template<class StringType>
void Toa(unsigned long value, StringType & res, bool clear_string = true, int base = 10)
{
Toa(static_cast<unsigned long long>(value), res, clear_string, base);
}
template<class StringType>
void Toa(long value, StringType & res, bool clear_string = true, int base = 10)
{
Toa(static_cast<long long>(value), res, clear_string, base);
}
template<class StringType>
void Toa(unsigned int value, StringType & res, bool clear_string = true, int base = 10)
{
Toa(static_cast<unsigned long long>(value), res, clear_string, base);
}
template<class StringType>
void Toa(int value, StringType & res, bool clear_string = true, int base = 10)
{
Toa(static_cast<long long>(value), res, clear_string, base);
}
template<class StringType>
void Toa(unsigned short value, StringType & res, bool clear_string = true, int base = 10)
{
Toa(static_cast<unsigned long long>(value), res, clear_string, base);
}
template<class StringType>
void Toa(short value, StringType & res, bool clear_string = true, int base = 10)
{
Toa(static_cast<long long>(value), res, clear_string, base);
}
std::wstring Toa(unsigned long long value, int base = 10);
std::wstring Toa(long long value, int base = 10);
std::wstring Toa(unsigned long value, int base = 10);
std::wstring Toa(long value, int base = 10);
std::wstring Toa(unsigned int value, int base = 10);
std::wstring Toa(int value, int base = 10);
std::wstring Toa(unsigned short value, int base = 10);
std::wstring Toa(short value, int base = 10);
}