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
This commit is contained in:
2017-12-05 16:32:21 +00:00
parent cde990ba82
commit 7d51372844
20 changed files with 970 additions and 138 deletions

27
convert/Makefile Normal file
View File

@@ -0,0 +1,27 @@
include Makefile.o.dep
libname=convert.a
all: $(libname)
$(libname): $(o)
$(AR) rcs $(libname) $(o)
%.o: %.cpp
$(CXX) -c $(CXXFLAGS) -I.. $<
depend:
makedepend -Y. -I.. -f- *.cpp > Makefile.dep
echo -n "o = " > Makefile.o.dep
ls -1 *.cpp | xargs -I foo echo -n foo " " | sed -E "s/([^\.]*)\.cpp[ ]/\1\.o/g" >> Makefile.o.dep
clean:
rm -f *.o
rm -f $(libname)
include Makefile.dep

4
convert/Makefile.dep Normal file
View File

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

1
convert/Makefile.o.dep Normal file
View File

@@ -0,0 +1 @@
o = misc.o text.o

View File

@@ -40,6 +40,7 @@
#include "inttostr.h"
#include "strtoint.h"
#include "text.h"
#endif

View File

@@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2012-2013, Tomasz Sowa
* Copyright (c) 2012-2017, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@@ -45,6 +45,10 @@ 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>

55
convert/misc.cpp Normal file
View 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
convert/misc.h Normal file
View 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

211
convert/strtoint.h Normal file
View File

@@ -0,0 +1,211 @@
/*
* 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)
{
unsigned long long res = 0;
bool carry = false;
int digit;
SetOverflow(was_overflow, false);
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 )
{
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 was_sign = false;
bool was_overflow_u = false;
SetOverflow(was_overflow, false);
str = SkipWhite(str);
if( *str == '-' )
{
was_sign = true;
str += 1;
}
// we do not trim spaces between a sign and a digit
unsigned long long uval = Toull(str, base, after_str, &was_overflow_u);
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 was_overflow_ll = false;
SetOverflow(was_overflow, false);
unsigned long long val = Toull(str, base, after_str, &was_overflow_ll);
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)
{
return ToUnsignedIntegerType<CharType, unsigned long>(str, base, after_str, was_overflow);
}
template<class CharType>
unsigned int Toui(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0)
{
return ToUnsignedIntegerType<CharType, unsigned int>(str, base, after_str, was_overflow);
}
template<typename CharType, typename IntegerType>
IntegerType ToIntegerType(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0)
{
bool was_overflow_ll = false;
SetOverflow(was_overflow, false);
long long val = Toll(str, base, after_str, &was_overflow_ll);
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)
{
return ToIntegerType<CharType, long>(str, base, after_str, was_overflow);
}
template<class CharType>
int Toi(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0)
{
return ToIntegerType<CharType, int>(str, base, after_str, was_overflow);
}
}
#endif

159
convert/text.cpp Normal file
View File

@@ -0,0 +1,159 @@
/*
* 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 <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;
}
}

69
convert/text.h Normal file
View File

@@ -0,0 +1,69 @@
/*
* 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_text
#define headerfile_picotools_convert_text
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);
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;
}
}
#endif