2012-07-03 16:58:18 +02:00
|
|
|
/*
|
|
|
|
* This file is a part of PikoTools
|
|
|
|
* and is distributed under the (new) BSD licence.
|
|
|
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
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);
2021-03-03 02:24:30 +01:00
|
|
|
* Copyright (c) 2012-2021, Tomasz Sowa
|
2012-07-03 16:58:18 +02:00
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2013-11-28 23:21:10 +01:00
|
|
|
#ifndef headerfile_picotools_convert_inttostr
|
|
|
|
#define headerfile_picotools_convert_inttostr
|
2012-07-03 16:58:18 +02:00
|
|
|
|
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);
2021-03-03 02:24:30 +01:00
|
|
|
#include <string>
|
2012-07-03 16:58:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace PT
|
|
|
|
{
|
|
|
|
|
|
|
|
|
added: functions for dealing with white characters:
bool IsWhite(wchar_t c, bool check_additional_chars, bool treat_new_line_as_white) (checking unicode white characters too)
CharType * SkipWhite(CharType * str, bool check_additional_chars = true, bool treat_new_line_as_white = true)
IsDigit(wchar_t c, int base, int * digit)
added: functions to converting from a string to an integer:
unsigned long long Toull(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0)
long long Toll(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0)
unsigned long Toul(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0)
unsigned int Toui(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0)
long Tol(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0)
int Toi(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0)
changed: some work in Space (new Api)
now Text() methods returns std::wstring by value (before they were returned by reference)
added std::wstring & TextRef() methods
added unsigned int UInt(), unsigned long ULong() and LongLong() and ULongLong()
GetValue() renamed to GetFirstValue()
AText() renamed to TextA() and they return std::string by value now
git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@1066 e52654a7-88a9-db11-a3e9-0013d4bc506e
2017-12-05 17:32:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
// if the buffer is too small it will be terminated at the beginning (empty string)
|
|
|
|
// and the function returns false
|
|
|
|
template<class CharType>
|
2018-04-22 23:22:55 +02:00
|
|
|
bool Toa(unsigned long long value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0)
|
2012-07-03 16:58:18 +02:00
|
|
|
{
|
|
|
|
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( 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>
|
2018-04-22 23:22:55 +02:00
|
|
|
bool Toa(long long value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 0)
|
2012-07-03 16:58:18 +02:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2018-04-22 23:22:55 +02:00
|
|
|
bool res = Toa(static_cast<unsigned long long>(value), buf, buf_len, base, len_out);
|
2012-07-03 16:58:18 +02:00
|
|
|
|
|
|
|
if( res )
|
|
|
|
{
|
|
|
|
if( len_out && is_sign )
|
|
|
|
*len_out += 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buffer[0] = 0;
|
|
|
|
// len_out is set to zero by Toa()
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-04-22 23:22:55 +02:00
|
|
|
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);
|
|
|
|
}
|
2012-07-03 16:58:18 +02:00
|
|
|
|
|
|
|
|
2018-04-22 23:22:55 +02:00
|
|
|
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);
|
|
|
|
}
|
2012-07-03 16:58:18 +02:00
|
|
|
|
|
|
|
|
2018-04-22 23:22:55 +02:00
|
|
|
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);
|
|
|
|
}
|
2012-07-03 16:58:18 +02:00
|
|
|
|
|
|
|
|
2018-04-22 23:22:55 +02:00
|
|
|
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);
|
|
|
|
}
|
2012-07-03 16:58:18 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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);
2021-03-03 02:24:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-03-15 19:23:49 +01:00
|
|
|
std::string to_str(unsigned long long value, int base = 10);
|
|
|
|
std::string to_str(long long value, int base = 10);
|
|
|
|
std::string to_str(unsigned long value, int base = 10);
|
|
|
|
std::string to_str(long value, int base = 10);
|
|
|
|
std::string to_str(unsigned int value, int base = 10);
|
|
|
|
std::string to_str(int value, int base = 10);
|
|
|
|
std::string to_str(unsigned short value, int base = 10);
|
|
|
|
std::string to_str(short value, int base = 10);
|
|
|
|
|
|
|
|
std::wstring to_wstr(unsigned long long value, int base = 10);
|
|
|
|
std::wstring to_wstr(long long value, int base = 10);
|
|
|
|
std::wstring to_wstr(unsigned long value, int base = 10);
|
|
|
|
std::wstring to_wstr(long value, int base = 10);
|
|
|
|
std::wstring to_wstr(unsigned int value, int base = 10);
|
|
|
|
std::wstring to_wstr(int value, int base = 10);
|
|
|
|
std::wstring to_wstr(unsigned short value, int base = 10);
|
|
|
|
std::wstring to_wstr(short value, int base = 10);
|
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);
2021-03-03 02:24:30 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-07-03 16:58:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|