added: convert (functions for converting) currently only int(long) -> string
added: textstream (an efficient memory stream for creating strings -- it's using MemBuffer) git-svn-id: svn://ttmath.org/publicrep/pikotools/trunk@425 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
45
convert/convert.h
Normal file
45
convert/convert.h
Normal file
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* 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, 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_membuffer_convert_convert
|
||||
#define headerfile_picotools_membuffer_convert_convert
|
||||
|
||||
|
||||
#include "inttostr.h"
|
||||
|
||||
|
||||
#endif
|
156
convert/inttostr.h
Normal file
156
convert/inttostr.h
Normal file
@@ -0,0 +1,156 @@
|
||||
/*
|
||||
* 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, 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_membuffer_convert_inttostr
|
||||
#define headerfile_picotools_membuffer_convert_inttostr
|
||||
|
||||
|
||||
|
||||
|
||||
namespace PT
|
||||
{
|
||||
|
||||
|
||||
// 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(unsigned long value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 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( 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 value, CharType * buffer, size_t buf_len, int base = 10, size_t * len_out = 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>(value), buf, buf_len, base, len_out);
|
||||
|
||||
if( res )
|
||||
{
|
||||
if( len_out && is_sign )
|
||||
*len_out += 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
buffer[0] = 0;
|
||||
// len_out is set to zero by Toa()
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
Reference in New Issue
Block a user