308 lines
8.4 KiB
C++
308 lines
8.4 KiB
C++
/*
|
|
* This file is a part of PikoTools
|
|
* and is distributed under the (new) BSD licence.
|
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
|
*/
|
|
|
|
/*
|
|
* Copyright (c) 2012-2022, 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.
|
|
*/
|
|
|
|
#ifndef headerfile_picotools_convert_inttostr
|
|
#define headerfile_picotools_convert_inttostr
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
namespace pt
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// if the buffer is too small it will be terminated at the beginning (empty string)
|
|
// and the function returns false
|
|
// min_width - if greater than zero then it is used for zero padding
|
|
template<class CharType>
|
|
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;
|
|
long rest;
|
|
|
|
if( len_out )
|
|
*len_out = 0;
|
|
|
|
if( buf_len == 0 )
|
|
return false;
|
|
|
|
i1 = i2 = 0;
|
|
|
|
if( base < 2 ) base = 2;
|
|
if( base > 16 ) base = 16;
|
|
|
|
do
|
|
{
|
|
rest = value % base;
|
|
value = value / base;
|
|
buffer[i2++] = (rest < 10) ? char(rest) + '0' : char(rest) - 10 + 'A';
|
|
}
|
|
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 )
|
|
{
|
|
buffer[0] = 0; // ops, the buffer was too small
|
|
return false;
|
|
}
|
|
|
|
if( len_out )
|
|
*len_out = i2 - i1;
|
|
|
|
buffer[i2--] = 0;
|
|
|
|
for( ; i1 < i2 ; ++i1, --i2)
|
|
{
|
|
CharType temp = buffer[i1];
|
|
buffer[i1] = buffer[i2];
|
|
buffer[i2] = temp;
|
|
}
|
|
|
|
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 long value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = nullptr, size_t min_width = 0)
|
|
{
|
|
if( len_out )
|
|
*len_out = 0;
|
|
|
|
if( buf_len == 0 )
|
|
return false;
|
|
|
|
CharType * buf = buffer;
|
|
bool is_sign = false;
|
|
|
|
if( value < 0 )
|
|
{
|
|
buffer[0] = '-';
|
|
buf += 1;
|
|
buf_len -= 1;
|
|
value = -value;
|
|
is_sign = true;
|
|
}
|
|
|
|
bool res = Toa(static_cast<unsigned long long>(value), buf, buf_len, base, len_out, min_width);
|
|
|
|
if( res )
|
|
{
|
|
if( len_out && is_sign )
|
|
*len_out += 1;
|
|
}
|
|
else
|
|
{
|
|
buffer[0] = 0;
|
|
// len_out is set to zero by Toa()
|
|
}
|
|
|
|
return res;
|
|
}
|
|
|
|
|
|
|
|
|
|
template<class CharType>
|
|
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, min_width);
|
|
}
|
|
|
|
template<class CharType>
|
|
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, min_width);
|
|
}
|
|
|
|
|
|
|
|
template<class CharType>
|
|
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, min_width);
|
|
}
|
|
|
|
|
|
template<class CharType>
|
|
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, min_width);
|
|
}
|
|
|
|
|
|
template<class CharType>
|
|
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, min_width);
|
|
}
|
|
|
|
|
|
template<class CharType>
|
|
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, min_width);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class StringType>
|
|
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];
|
|
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, min_width);
|
|
res.append(buffer, len_out);
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
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];
|
|
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, min_width);
|
|
res.append(buffer, len_out);
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
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, min_width);
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
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, min_width);
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
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, min_width);
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
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, min_width);
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
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, min_width);
|
|
}
|
|
|
|
|
|
template<class StringType>
|
|
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, min_width);
|
|
}
|
|
|
|
|
|
|
|
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, size_t min_width = 0);
|
|
std::string to_str(unsigned long value, int base = 10, size_t min_width = 0);
|
|
std::string to_str(long value, int base = 10, size_t min_width = 0);
|
|
std::string to_str(unsigned int value, int base = 10, size_t min_width = 0);
|
|
std::string to_str(int value, int base = 10, size_t min_width = 0);
|
|
std::string to_str(unsigned short value, int base = 10, size_t min_width = 0);
|
|
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, size_t min_width = 0);
|
|
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, size_t min_width = 0);
|
|
std::wstring to_wstr(long value, int base = 10, size_t min_width = 0);
|
|
std::wstring to_wstr(unsigned int value, int base = 10, size_t min_width = 0);
|
|
std::wstring to_wstr(int value, int base = 10, size_t min_width = 0);
|
|
std::wstring to_wstr(unsigned short value, int base = 10, size_t min_width = 0);
|
|
std::wstring to_wstr(short value, int base = 10, size_t min_width = 0);
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
#endif
|