moved all directories to src subdirectory
This commit is contained in:
47
src/convert/convert.h
Normal file
47
src/convert/convert.h
Normal file
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* 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-2018, 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"
|
||||
#include "patternreplacer.h"
|
||||
#include "strtoint.h"
|
||||
#include "text.h"
|
||||
|
||||
#endif
|
156
src/convert/inttostr.cpp
Normal file
156
src/convert/inttostr.cpp
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) 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::string to_str(unsigned long long value, int base)
|
||||
{
|
||||
std::string res;
|
||||
Toa(value, res, false, base);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
std::string to_str(long long value, int base)
|
||||
{
|
||||
std::string res;
|
||||
Toa(value, res, false, base);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
std::string to_str(unsigned long value, int base)
|
||||
{
|
||||
return to_str(static_cast<unsigned long long>(value), base);
|
||||
}
|
||||
|
||||
|
||||
std::string to_str(long value, int base)
|
||||
{
|
||||
return to_str(static_cast<long long>(value), base);
|
||||
}
|
||||
|
||||
|
||||
std::string to_str(unsigned int value, int base)
|
||||
{
|
||||
return to_str(static_cast<unsigned long long>(value), base);
|
||||
}
|
||||
|
||||
|
||||
std::string to_str(int value, int base)
|
||||
{
|
||||
return to_str(static_cast<long long>(value), base);
|
||||
}
|
||||
|
||||
|
||||
std::string to_str(unsigned short value, int base)
|
||||
{
|
||||
return to_str(static_cast<unsigned long long>(value), base);
|
||||
}
|
||||
|
||||
|
||||
std::string to_str(short value, int base)
|
||||
{
|
||||
return to_str(static_cast<long long>(value), base);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
std::wstring to_wstr(unsigned long long value, int base)
|
||||
{
|
||||
std::wstring res;
|
||||
Toa(value, res, false, base);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
std::wstring to_wstr(long long value, int base)
|
||||
{
|
||||
std::wstring res;
|
||||
Toa(value, res, false, base);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
std::wstring to_wstr(unsigned long value, int base)
|
||||
{
|
||||
return to_wstr(static_cast<unsigned long long>(value), base);
|
||||
}
|
||||
|
||||
|
||||
std::wstring to_wstr(long value, int base)
|
||||
{
|
||||
return to_wstr(static_cast<long long>(value), base);
|
||||
}
|
||||
|
||||
|
||||
std::wstring to_wstr(unsigned int value, int base)
|
||||
{
|
||||
return to_wstr(static_cast<unsigned long long>(value), base);
|
||||
}
|
||||
|
||||
|
||||
std::wstring to_wstr(int value, int base)
|
||||
{
|
||||
return to_wstr(static_cast<long long>(value), base);
|
||||
}
|
||||
|
||||
|
||||
std::wstring to_wstr(unsigned short value, int base)
|
||||
{
|
||||
return to_wstr(static_cast<unsigned long long>(value), base);
|
||||
}
|
||||
|
||||
|
||||
std::wstring to_wstr(short value, int base)
|
||||
{
|
||||
return to_wstr(static_cast<long long>(value), base);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
298
src/convert/inttostr.h
Normal file
298
src/convert/inttostr.h
Normal file
@@ -0,0 +1,298 @@
|
||||
/*
|
||||
* 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-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.
|
||||
*/
|
||||
|
||||
#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
|
||||
template<class CharType>
|
||||
bool Toa(unsigned long 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 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 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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
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::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);
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
55
src/convert/misc.cpp
Normal file
55
src/convert/misc.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* This file is a part of PikoTools
|
||||
* and is distributed under the (new) BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2017, 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 "misc.h"
|
||||
|
||||
|
||||
namespace PT
|
||||
{
|
||||
|
||||
|
||||
void SetOverflow(bool * was_overflow, bool val)
|
||||
{
|
||||
if( was_overflow )
|
||||
*was_overflow = val;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
55
src/convert/misc.h
Normal file
55
src/convert/misc.h
Normal file
@@ -0,0 +1,55 @@
|
||||
/*
|
||||
* This file is a part of PikoTools
|
||||
* and is distributed under the (new) BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2017, 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_misc
|
||||
#define headerfile_picotools_convert_misc
|
||||
|
||||
#include <limits>
|
||||
#include "text.h"
|
||||
|
||||
|
||||
namespace PT
|
||||
{
|
||||
|
||||
void SetOverflow(bool * was_overflow, bool val);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
||||
|
169
src/convert/patternreplacer.h
Normal file
169
src/convert/patternreplacer.h
Normal file
@@ -0,0 +1,169 @@
|
||||
/*
|
||||
* This file is a part of PikoTools
|
||||
* and is distributed under the (new) BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2018, 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_patternreplacer
|
||||
#define headerfile_picotools_convert_patternreplacer
|
||||
|
||||
#include <string>
|
||||
#include "textstream/textstream.h"
|
||||
#include "strtoint.h"
|
||||
|
||||
|
||||
|
||||
namespace PT
|
||||
{
|
||||
|
||||
template<typename CharType, typename StrType>
|
||||
class PatternReplacerBase
|
||||
{
|
||||
public:
|
||||
|
||||
/*
|
||||
* output_string can be the same string as input pattern
|
||||
*/
|
||||
template<typename ... Types>
|
||||
void ReplaceToString(const StrType & pattern, StrType & output_string, Types ... types)
|
||||
{
|
||||
ReplaceGeneric(pattern, output_string, types...);
|
||||
}
|
||||
|
||||
|
||||
template<typename ... Types>
|
||||
StrType Replace(const StrType & pattern, Types ... types)
|
||||
{
|
||||
StrType output_string;
|
||||
ReplaceGeneric(pattern, output_string, types...);
|
||||
|
||||
return output_string;
|
||||
}
|
||||
|
||||
|
||||
private:
|
||||
|
||||
std::vector<StrType> params;
|
||||
TextStreamBase<CharType, 256, 4096> buffer;
|
||||
StrType temp_str;
|
||||
|
||||
|
||||
|
||||
template<typename ... Types>
|
||||
void ReplaceGeneric(const StrType & pattern, StrType & output_string, Types ... types)
|
||||
{
|
||||
params.clear();
|
||||
AddParams(types...);
|
||||
ReplacePattern(pattern);
|
||||
buffer.to_string(output_string);
|
||||
|
||||
params.clear();
|
||||
buffer.clear();
|
||||
temp_str.clear();
|
||||
}
|
||||
|
||||
|
||||
void AddParams()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
template<typename Type, typename ... Types>
|
||||
void AddParams(const Type & type, Types ... types)
|
||||
{
|
||||
buffer.clear();
|
||||
buffer << type;
|
||||
buffer.to_string(temp_str);
|
||||
params.push_back(temp_str);
|
||||
temp_str.clear();
|
||||
|
||||
AddParams(types...);
|
||||
}
|
||||
|
||||
|
||||
void ReplacePattern(const StrType & pattern)
|
||||
{
|
||||
buffer.clear();
|
||||
|
||||
for(size_t i=0 ; i<pattern.size() ; )
|
||||
{
|
||||
bool pattern_changed = false;
|
||||
|
||||
if( pattern[i] == '%' )
|
||||
{
|
||||
if( i + 1 < pattern.size() )
|
||||
{
|
||||
if( pattern[i+1] == '%' )
|
||||
{
|
||||
buffer << '%';
|
||||
i += 2;
|
||||
pattern_changed = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
const CharType * index_start_str = &pattern[i+1];
|
||||
const CharType * after_str;
|
||||
bool was_overflow;
|
||||
unsigned long index = Toul(index_start_str, 10, &after_str, &was_overflow, false);
|
||||
|
||||
if( !was_overflow && after_str > index_start_str && (size_t)index < params.size() )
|
||||
{
|
||||
i = i + 1 + (after_str - index_start_str);
|
||||
buffer << params[index];
|
||||
pattern_changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if( !pattern_changed )
|
||||
{
|
||||
buffer << pattern[i];
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
typedef PatternReplacerBase<char, std::string> PatternReplacer;
|
||||
typedef PatternReplacerBase<wchar_t, std::wstring> WPatternReplacer;
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
378
src/convert/strtoint.h
Normal file
378
src/convert/strtoint.h
Normal file
@@ -0,0 +1,378 @@
|
||||
/*
|
||||
* This file is a part of PikoTools
|
||||
* and is distributed under the (new) BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2017, 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_strtoint
|
||||
#define headerfile_picotools_convert_strtoint
|
||||
|
||||
#include <limits>
|
||||
#include "text.h"
|
||||
#include "misc.h"
|
||||
|
||||
|
||||
namespace PT
|
||||
{
|
||||
|
||||
|
||||
|
||||
|
||||
template<typename CharType>
|
||||
unsigned long long Toull(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
|
||||
{
|
||||
unsigned long long res = 0;
|
||||
bool carry = false;
|
||||
int digit;
|
||||
|
||||
SetOverflow(was_overflow, false);
|
||||
|
||||
if( allow_skip_whitechars )
|
||||
str = SkipWhite(str);
|
||||
|
||||
while( !carry && IsDigit(*str, base, &digit) )
|
||||
{
|
||||
#ifdef __GNUC__
|
||||
carry = __builtin_mul_overflow(res, static_cast<unsigned long long>(base), &res);
|
||||
|
||||
if( !carry )
|
||||
{
|
||||
carry = __builtin_add_overflow(res, static_cast<unsigned long long>(digit), &res);
|
||||
}
|
||||
#else
|
||||
// on other compilers than GCC or CLANG we do not test overflow at the moment
|
||||
res = res * static_cast<unsigned long long>(base) + static_cast<unsigned long long>(digit);
|
||||
#endif
|
||||
|
||||
str += 1;
|
||||
}
|
||||
|
||||
if( carry )
|
||||
{
|
||||
if( after_str )
|
||||
{
|
||||
while( IsDigit(*str, base, &digit) )
|
||||
{
|
||||
str += 1;
|
||||
}
|
||||
}
|
||||
|
||||
SetOverflow(was_overflow, true);
|
||||
res = 0;
|
||||
}
|
||||
|
||||
if( after_str )
|
||||
*after_str = str;
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
template<typename CharType>
|
||||
long long Toll(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
|
||||
{
|
||||
bool was_sign = false;
|
||||
bool was_overflow_u = false;
|
||||
|
||||
SetOverflow(was_overflow, false);
|
||||
|
||||
if( allow_skip_whitechars )
|
||||
str = SkipWhite(str);
|
||||
|
||||
if( *str == '-' )
|
||||
{
|
||||
was_sign = true;
|
||||
str += 1;
|
||||
}
|
||||
|
||||
unsigned long long uval = Toull(str, base, after_str, &was_overflow_u, false);
|
||||
unsigned long long sign_add = ( was_sign ) ? 1 : 0;
|
||||
|
||||
if( was_overflow_u )
|
||||
{
|
||||
SetOverflow(was_overflow, true);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if( uval > static_cast<unsigned long long>(std::numeric_limits<long long>::max()) + sign_add )
|
||||
{
|
||||
SetOverflow(was_overflow, true);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if( was_sign )
|
||||
{
|
||||
return static_cast<long long>(0) - static_cast<long long>(uval);
|
||||
}
|
||||
|
||||
return static_cast<long long>(uval);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
template<typename CharType, typename IntegerType>
|
||||
IntegerType ToUnsignedIntegerType(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
|
||||
{
|
||||
bool was_overflow_ll = false;
|
||||
|
||||
SetOverflow(was_overflow, false);
|
||||
unsigned long long val = Toull(str, base, after_str, &was_overflow_ll, allow_skip_whitechars);
|
||||
|
||||
if( was_overflow_ll || val > static_cast<unsigned long long>(std::numeric_limits<IntegerType>::max()) )
|
||||
{
|
||||
SetOverflow(was_overflow, true);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return static_cast<IntegerType>(val);
|
||||
}
|
||||
|
||||
|
||||
template<class CharType>
|
||||
unsigned long Toul(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
|
||||
{
|
||||
return ToUnsignedIntegerType<CharType, unsigned long>(str, base, after_str, was_overflow, allow_skip_whitechars);
|
||||
}
|
||||
|
||||
template<class CharType>
|
||||
unsigned int Toui(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
|
||||
{
|
||||
return ToUnsignedIntegerType<CharType, unsigned int>(str, base, after_str, was_overflow, allow_skip_whitechars);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
template<typename CharType, typename IntegerType>
|
||||
IntegerType ToIntegerType(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
|
||||
{
|
||||
bool was_overflow_ll = false;
|
||||
|
||||
SetOverflow(was_overflow, false);
|
||||
long long val = Toll(str, base, after_str, &was_overflow_ll, allow_skip_whitechars);
|
||||
|
||||
if( was_overflow_ll ||
|
||||
val < static_cast<long long>(std::numeric_limits<IntegerType>::min()) ||
|
||||
val > static_cast<long long>(std::numeric_limits<IntegerType>::max()) )
|
||||
{
|
||||
SetOverflow(was_overflow, true);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return static_cast<IntegerType>(val);
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<class CharType>
|
||||
long Tol(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
|
||||
{
|
||||
return ToIntegerType<CharType, long>(str, base, after_str, was_overflow, allow_skip_whitechars);
|
||||
}
|
||||
|
||||
template<class CharType>
|
||||
int Toi(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
|
||||
{
|
||||
return ToIntegerType<CharType, int>(str, base, after_str, was_overflow, allow_skip_whitechars);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
*
|
||||
* the base will be automatically detected
|
||||
*
|
||||
* (first digit is [1-9]) - base 10
|
||||
* 0 - 8
|
||||
* # - 16
|
||||
* & - 2
|
||||
*
|
||||
*/
|
||||
template<typename CharType>
|
||||
unsigned long long Toull_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
|
||||
{
|
||||
if( allow_skip_whitechars )
|
||||
str = SkipWhite(str);
|
||||
|
||||
int base = 10;
|
||||
|
||||
if( *str == '0' )
|
||||
{
|
||||
base = 8;
|
||||
str += 1;
|
||||
}
|
||||
else
|
||||
if( *str == '#' )
|
||||
{
|
||||
base = 16;
|
||||
str += 1;
|
||||
}
|
||||
else
|
||||
if( *str == '&' )
|
||||
{
|
||||
base = 2;
|
||||
str += 1;
|
||||
}
|
||||
|
||||
return Toull(str, base, after_str, was_overflow, false);
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<typename CharType>
|
||||
long long Toll_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
|
||||
{
|
||||
bool was_sign = false;
|
||||
bool was_overflow_u = false;
|
||||
|
||||
SetOverflow(was_overflow, false);
|
||||
|
||||
if( allow_skip_whitechars )
|
||||
str = SkipWhite(str);
|
||||
|
||||
if( *str == '-' )
|
||||
{
|
||||
was_sign = true;
|
||||
str += 1;
|
||||
}
|
||||
|
||||
unsigned long long uval = Toull_b(str, after_str, &was_overflow_u, false);
|
||||
unsigned long long sign_add = ( was_sign ) ? 1 : 0;
|
||||
|
||||
if( was_overflow_u )
|
||||
{
|
||||
SetOverflow(was_overflow, true);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if( uval > static_cast<unsigned long long>(std::numeric_limits<long long>::max()) + sign_add )
|
||||
{
|
||||
SetOverflow(was_overflow, true);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if( was_sign )
|
||||
{
|
||||
return static_cast<long long>(0) - static_cast<long long>(uval);
|
||||
}
|
||||
|
||||
return static_cast<long long>(uval);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
template<typename CharType, typename IntegerType>
|
||||
IntegerType ToUnsignedIntegerType_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
|
||||
{
|
||||
bool was_overflow_ll = false;
|
||||
|
||||
SetOverflow(was_overflow, false);
|
||||
unsigned long long val = Toull_b(str, after_str, &was_overflow_ll, allow_skip_whitechars);
|
||||
|
||||
if( was_overflow_ll || val > static_cast<unsigned long long>(std::numeric_limits<IntegerType>::max()) )
|
||||
{
|
||||
SetOverflow(was_overflow, true);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return static_cast<IntegerType>(val);
|
||||
}
|
||||
|
||||
|
||||
template<class CharType>
|
||||
unsigned long Toul_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
|
||||
{
|
||||
return ToUnsignedIntegerType_b<CharType, unsigned long>(str, after_str, was_overflow, allow_skip_whitechars);
|
||||
}
|
||||
|
||||
template<class CharType>
|
||||
unsigned int Toui_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
|
||||
{
|
||||
return ToUnsignedIntegerType_b<CharType, unsigned int>(str, after_str, was_overflow, allow_skip_whitechars);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
template<typename CharType, typename IntegerType>
|
||||
IntegerType ToIntegerType_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
|
||||
{
|
||||
bool was_overflow_ll = false;
|
||||
|
||||
SetOverflow(was_overflow, false);
|
||||
long long val = Toll_b(str, after_str, &was_overflow_ll, allow_skip_whitechars);
|
||||
|
||||
if( was_overflow_ll ||
|
||||
val < static_cast<long long>(std::numeric_limits<IntegerType>::min()) ||
|
||||
val > static_cast<long long>(std::numeric_limits<IntegerType>::max()) )
|
||||
{
|
||||
SetOverflow(was_overflow, true);
|
||||
return 0;
|
||||
}
|
||||
|
||||
return static_cast<IntegerType>(val);
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<class CharType>
|
||||
long Tol_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
|
||||
{
|
||||
return ToIntegerType_b<CharType, long>(str, after_str, was_overflow, allow_skip_whitechars);
|
||||
}
|
||||
|
||||
template<class CharType>
|
||||
int Toi_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
|
||||
{
|
||||
return ToIntegerType_b<CharType, int>(str, after_str, was_overflow, allow_skip_whitechars);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
209
src/convert/text.cpp
Normal file
209
src/convert/text.cpp
Normal file
@@ -0,0 +1,209 @@
|
||||
/*
|
||||
* This file is a part of PikoTools
|
||||
* and is distributed under the (new) BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2017-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 <cstddef>
|
||||
#include "text.h"
|
||||
|
||||
|
||||
namespace PT
|
||||
{
|
||||
|
||||
// white_chars table should be sorted (a binary search algorithm is used to find a character)
|
||||
// we do not treat a new line character (10) as a white character here
|
||||
// also space (32) and tab (9) are not inserted here
|
||||
static const wchar_t white_chars_table[] = {
|
||||
0x000B, // LINE TABULATION (vertical tabulation)
|
||||
0x000C, // FORM FEED (FF)
|
||||
0x000D, // CARRIAGE RETURN (CR) - a character at the end in a dos text file
|
||||
0x0085, // NEXT LINE (NEL)
|
||||
0x00A0, // NO-BREAK SPACE (old name: NON-BREAKING SPACE)
|
||||
0x1680, // OGHAM SPACE MARK
|
||||
0x180E, // MONGOLIAN VOWEL SEPARATOR
|
||||
0x2000, // EN QUAD
|
||||
0x2001, // EM QUAD
|
||||
0x2002, // EN SPACE
|
||||
0x2003, // EM SPACE
|
||||
0x2004, // THREE-PER-EM SPACE
|
||||
0x2005, // FOUR-PER-EM SPACE
|
||||
0x2006, // SIX-PER-EM SPACE
|
||||
0x2007, // FIGURE SPACE
|
||||
0x2008, // PUNCTUATION SPACE
|
||||
0x2009, // THIN SPACE
|
||||
0x200A, // HAIR SPACE
|
||||
0x2028, // LINE SEPARATOR
|
||||
0x2029, // PARAGRAPH SEPARATOR
|
||||
0x202F, // NARROW NO-BREAK SPACE
|
||||
0x205F, // MEDIUM MATHEMATICAL SPACE
|
||||
0x3000, // IDEOGRAPHIC SPACE
|
||||
0xFEFF, // ZERO WIDTH NO-BREAK SPACE
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
/*
|
||||
if check_additional_chars is false then we are testing only a space (32), tab (9) and a new line (10) (if treat_new_line_as_white is true)
|
||||
*/
|
||||
bool IsWhite(wchar_t c, bool check_additional_chars, bool treat_new_line_as_white)
|
||||
{
|
||||
// space (32) and tab (9) are the most common white chars
|
||||
// so we check them at the beginning (optimisation)
|
||||
if( c == 32 || c == 9 )
|
||||
return true;
|
||||
|
||||
std::size_t len = sizeof(white_chars_table) / sizeof(wchar_t);
|
||||
std::size_t o1 = 0;
|
||||
std::size_t o2 = len - 1;
|
||||
|
||||
if( c == 10 )
|
||||
return treat_new_line_as_white ? true : false;
|
||||
|
||||
if( !check_additional_chars )
|
||||
return false;
|
||||
|
||||
if( c < white_chars_table[o1] || c > white_chars_table[o2] )
|
||||
return false;
|
||||
|
||||
if( c == white_chars_table[o1] || c == white_chars_table[o2] )
|
||||
return true;
|
||||
|
||||
while( o1 + 1 < o2 )
|
||||
{
|
||||
std::size_t o = (o2 - o1)/2 + o1;
|
||||
|
||||
if( c == white_chars_table[o] )
|
||||
return true;
|
||||
|
||||
if( c > white_chars_table[o] )
|
||||
o1 = o;
|
||||
else
|
||||
o2 = o;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
|
||||
bool IsDigit(wchar_t c, int base, int * digit)
|
||||
{
|
||||
int d = 0;
|
||||
|
||||
if( c >= '0' && c <= '9' )
|
||||
{
|
||||
d = c - '0';
|
||||
}
|
||||
else
|
||||
if( c >= 'a' && c <= 'f' )
|
||||
{
|
||||
d = c - 'a' + 10;
|
||||
}
|
||||
else
|
||||
if( c >= 'A' && c <= 'F' )
|
||||
{
|
||||
d = c - 'A' + 10;
|
||||
}
|
||||
else
|
||||
{
|
||||
if( digit )
|
||||
*digit = d;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
if( digit )
|
||||
*digit = d;
|
||||
|
||||
return d < base;
|
||||
}
|
||||
|
||||
|
||||
|
||||
char ToLower(char c)
|
||||
{
|
||||
return pt_private::ToLowerGeneric(c);
|
||||
}
|
||||
|
||||
wchar_t ToLower(wchar_t c)
|
||||
{
|
||||
return pt_private::ToLowerGeneric(c);
|
||||
}
|
||||
|
||||
|
||||
char ToUpper(char c)
|
||||
{
|
||||
return pt_private::ToUpperGeneric(c);
|
||||
}
|
||||
|
||||
wchar_t ToUpper(wchar_t c)
|
||||
{
|
||||
return pt_private::ToUpperGeneric(c);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
void ToLower(std::string & str)
|
||||
{
|
||||
pt_private::ToLowerStrGeneric(str);
|
||||
}
|
||||
|
||||
void ToLower(std::wstring & str)
|
||||
{
|
||||
pt_private::ToLowerStrGeneric(str);
|
||||
}
|
||||
|
||||
|
||||
void ToUpper(std::string & str)
|
||||
{
|
||||
pt_private::ToLowerStrGeneric(str);
|
||||
}
|
||||
|
||||
void ToUpper(std::wstring & str)
|
||||
{
|
||||
pt_private::ToLowerStrGeneric(str);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
296
src/convert/text.h
Normal file
296
src/convert/text.h
Normal file
@@ -0,0 +1,296 @@
|
||||
/*
|
||||
* This file is a part of PikoTools
|
||||
* and is distributed under the (new) BSD licence.
|
||||
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (c) 2017-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.
|
||||
*/
|
||||
|
||||
#ifndef headerfile_picotools_convert_text
|
||||
#define headerfile_picotools_convert_text
|
||||
|
||||
#include "text_private.h"
|
||||
|
||||
|
||||
|
||||
namespace PT
|
||||
{
|
||||
|
||||
bool IsWhite(wchar_t c, bool check_additional_chars = true, bool treat_new_line_as_white = true);
|
||||
|
||||
|
||||
bool IsDigit(wchar_t c, int base = 10, int * digit = 0);
|
||||
|
||||
|
||||
char ToLower(char c);
|
||||
wchar_t ToLower(wchar_t c);
|
||||
|
||||
char ToUpper(char c);
|
||||
wchar_t ToUpper(wchar_t c);
|
||||
|
||||
// rename to something like to_lower_emplace
|
||||
// and add to_lower which returns string
|
||||
void ToLower(std::string & str);
|
||||
void ToLower(std::wstring & str);
|
||||
|
||||
void ToUpper(std::string & str);
|
||||
void ToUpper(std::wstring & str);
|
||||
|
||||
|
||||
////////////////////////////
|
||||
|
||||
template<class CharType>
|
||||
CharType * SkipWhite(CharType * str, bool check_additional_chars = true, bool treat_new_line_as_white = true)
|
||||
{
|
||||
while( IsWhite(static_cast<wchar_t>(*str), check_additional_chars, treat_new_line_as_white) )
|
||||
{
|
||||
str += 1;
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
*
|
||||
* str_end is pointing at the end of the string (the last item + one)
|
||||
*
|
||||
* return value is a pointer to the first white character after a non-white character at the end
|
||||
* or to the last+one if there is no any white characters
|
||||
*
|
||||
*/
|
||||
template<class CharType>
|
||||
CharType * SkipWhiteFromBack(CharType * str_begin, CharType * str_end, bool check_additional_chars = true, bool treat_new_line_as_white = true)
|
||||
{
|
||||
while( str_end > str_begin && IsWhite(static_cast<wchar_t>(*(str_end-1)), check_additional_chars, treat_new_line_as_white) )
|
||||
{
|
||||
str_end -= 1;
|
||||
}
|
||||
|
||||
return str_end;
|
||||
}
|
||||
|
||||
|
||||
template<class CharType>
|
||||
CharType * SkipWhiteFromBack(CharType * str, bool check_additional_chars = true, bool treat_new_line_as_white = true)
|
||||
{
|
||||
CharType * str_begin = str;
|
||||
|
||||
while( *str != 0 )
|
||||
{
|
||||
str += 1;
|
||||
}
|
||||
|
||||
return SkipWhiteFromBack(str_begin, str, check_additional_chars, treat_new_line_as_white);
|
||||
}
|
||||
|
||||
|
||||
template<class StringType1, class StringType2>
|
||||
int CompareNoCase(const StringType1 * str1, const StringType2 * str2)
|
||||
{
|
||||
while( *str1 && *str2 && ToLower(*str1) == ToLower(*str2) )
|
||||
{
|
||||
++str1;
|
||||
++str2;
|
||||
}
|
||||
|
||||
if( *str1 == 0 && *str2 == 0 )
|
||||
return 0;
|
||||
|
||||
int c1;
|
||||
int c2;
|
||||
|
||||
if constexpr (sizeof(StringType1) == 1 && sizeof(StringType2) == 1)
|
||||
{
|
||||
c1 = ToLower((wchar_t)(unsigned char)(*str1));
|
||||
c2 = ToLower((wchar_t)(unsigned char)(*str2));
|
||||
}
|
||||
else
|
||||
{
|
||||
c1 = ToLower(*str1);
|
||||
c2 = ToLower(*str2);
|
||||
}
|
||||
|
||||
return c1 - c2;
|
||||
}
|
||||
|
||||
|
||||
template<class StringType1, class StringType2>
|
||||
int CompareNoCase(const StringType1 & str1, const StringType2 & str2)
|
||||
{
|
||||
return CompareNoCase(str1.c_str(), str2.c_str());
|
||||
}
|
||||
|
||||
|
||||
template<class StringType1, class StringType2>
|
||||
int CompareNoCasep(const StringType1 * str1, const StringType2 * str2)
|
||||
{
|
||||
return CompareNoCase(str1, str2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
template<class StringType1, class StringType2>
|
||||
int CompareNoCase(const StringType1 * str1_begin, const StringType1 * str1_end, const StringType2 * str2)
|
||||
{
|
||||
while( str1_begin < str1_end && *str2 && ToLower(*str1_begin) == ToLower(*str2) )
|
||||
{
|
||||
++str1_begin;
|
||||
++str2;
|
||||
}
|
||||
|
||||
if( str1_begin == str1_end && *str2 == 0 )
|
||||
return 0;
|
||||
|
||||
int c1;
|
||||
int c2;
|
||||
|
||||
if constexpr (sizeof(StringType1) == 1 && sizeof(StringType2) == 1)
|
||||
{
|
||||
c1 = str1_begin < str1_end ? ToLower((wchar_t)(unsigned char)(*str1_begin)) : 0;
|
||||
c2 = ToLower((wchar_t)(unsigned char)(*str2));
|
||||
}
|
||||
else
|
||||
{
|
||||
c1 = str1_begin < str1_end ? ToLower(*str1_begin) : 0;
|
||||
c2 = ToLower(*str2);
|
||||
}
|
||||
|
||||
return c1 - c2;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
template<class StringType1, class StringType2>
|
||||
bool EqualNoCase(const StringType1 * str1, const StringType2 * str2)
|
||||
{
|
||||
return CompareNoCase(str1, str2) == 0;
|
||||
}
|
||||
|
||||
|
||||
template<class StringType1, class StringType2>
|
||||
bool EqualNoCase(const StringType1 & str1, const StringType2 & str2)
|
||||
{
|
||||
return EqualNoCase(str1.c_str(), str2.c_str());
|
||||
}
|
||||
|
||||
|
||||
template<class StringType1, class StringType2>
|
||||
bool EqualNoCasep(const StringType1 * str1, const StringType2 * str2)
|
||||
{
|
||||
return EqualNoCase(str1, str2);
|
||||
}
|
||||
|
||||
|
||||
|
||||
template<class StringType1, class StringType2>
|
||||
bool EqualNoCase(const StringType1 * str1_begin, const StringType1 * str1_end, const StringType2 * str2)
|
||||
{
|
||||
return CompareNoCase(str1_begin, str1_end, str2) == 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
template<class StringType1, class StringType2>
|
||||
bool IsSubStringp(const StringType1 * short_str, const StringType2 * long_str)
|
||||
{
|
||||
while( *short_str && *long_str && wchar_t(*short_str) == wchar_t(*long_str) )
|
||||
{
|
||||
++short_str;
|
||||
++long_str;
|
||||
}
|
||||
|
||||
if( *short_str == 0 )
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
template<class StringType1, class StringType2>
|
||||
bool IsSubString(const StringType1 * short_str, const StringType2 * long_str)
|
||||
{
|
||||
return IsSubStringp(short_str, long_str);
|
||||
}
|
||||
|
||||
|
||||
template<class StringType1, class StringType2>
|
||||
bool IsSubString(const StringType1 & short_str, const StringType2 & long_str)
|
||||
{
|
||||
return IsSubStringp(short_str.c_str(), long_str.c_str());
|
||||
}
|
||||
|
||||
|
||||
template<class StringType1, class StringType2>
|
||||
bool IsSubStringNoCasep(const StringType1 * short_str, const StringType2 * long_str)
|
||||
{
|
||||
while( *short_str && *long_str && ToLower(*short_str) == ToLower(*long_str) )
|
||||
{
|
||||
++short_str;
|
||||
++long_str;
|
||||
}
|
||||
|
||||
if( *short_str == 0 )
|
||||
return true;
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
template<class StringType1, class StringType2>
|
||||
bool IsSubStringNoCase(const StringType1 * short_str, const StringType2 * long_str)
|
||||
{
|
||||
return IsSubStringNoCasep(short_str, long_str);
|
||||
}
|
||||
|
||||
|
||||
template<class StringType1, class StringType2>
|
||||
bool IsSubStringNoCase(const StringType1 & short_str, const StringType2 & long_str)
|
||||
{
|
||||
return IsSubStringNoCasep(short_str.c_str(), long_str.c_str());
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
#endif
|
96
src/convert/text_private.h
Normal file
96
src/convert/text_private.h
Normal file
@@ -0,0 +1,96 @@
|
||||
/*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef headerfile_picotools_convert_text_private
|
||||
#define headerfile_picotools_convert_text_private
|
||||
|
||||
#include <string>
|
||||
|
||||
|
||||
namespace PT
|
||||
{
|
||||
|
||||
namespace pt_private
|
||||
{
|
||||
|
||||
template<class CharType>
|
||||
CharType ToLowerGeneric(CharType c)
|
||||
{
|
||||
if( c >= 'A' && c <= 'Z' )
|
||||
return c - 'A' + 'a';
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
template<class CharType>
|
||||
CharType ToUpperGeneric(CharType c)
|
||||
{
|
||||
if( c >= 'a' && c <= 'z' )
|
||||
return c - 'a' + 'A';
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
void ToLowerStrGeneric(StringType & s)
|
||||
{
|
||||
typename StringType::size_type i;
|
||||
|
||||
for(i=0 ; i<s.size() ; ++i)
|
||||
s[i] = ToLowerGeneric(s[i]);
|
||||
}
|
||||
|
||||
|
||||
template<class StringType>
|
||||
void ToUpperStrGeneric(StringType & s)
|
||||
{
|
||||
typename StringType::size_type i;
|
||||
|
||||
for(i=0 ; i<s.size() ; ++i)
|
||||
s[i] = ToUpperGeneric(s[i]);
|
||||
}
|
||||
|
||||
|
||||
} // namespace pt_private
|
||||
|
||||
} // namespace PT
|
||||
|
||||
|
||||
#endif
|
||||
|
Reference in New Issue
Block a user