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:
Tomasz Sowa 2021-03-03 02:24:30 +01:00
parent d9a4fa34e2
commit 448ad42961
4 changed files with 198 additions and 2 deletions

View File

@ -1,4 +1,5 @@
# DO NOT DELETE
inttostr.o: inttostr.h
misc.o: misc.h text.h
text.o: text.h

View File

@ -1 +1 @@
o = misc.o text.o
o = inttostr.o misc.o text.o

100
convert/inttostr.cpp Normal file
View File

@ -0,0 +1,100 @@
/*
* This file is a part of PikoTools
* and is distributed under the (new) BSD licence.
* Author: Tomasz Sowa <t.sowa@ttmath.org>
*/
/*
* Copyright (c) 2021, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* * Neither the name Tomasz Sowa nor the names of contributors to this
* project may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "inttostr.h"
namespace PT
{
std::wstring Toa(unsigned long long value, int base)
{
std::wstring res;
Toa(value, res, false, base);
return res;
}
std::wstring Toa(long long value, int base)
{
std::wstring res;
Toa(value, res, false, base);
return res;
}
std::wstring Toa(unsigned long value, int base)
{
return Toa(static_cast<unsigned long long>(value), base);
}
std::wstring Toa(long value, int base)
{
return Toa(static_cast<long long>(value), base);
}
std::wstring Toa(unsigned int value, int base)
{
return Toa(static_cast<unsigned long long>(value), base);
}
std::wstring Toa(int value, int base)
{
return Toa(static_cast<long long>(value), base);
}
std::wstring Toa(unsigned short value, int base)
{
return Toa(static_cast<unsigned long long>(value), base);
}
std::wstring Toa(short value, int base)
{
return Toa(static_cast<long long>(value), base);
}
}

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);
}