rename functions for converting strings to integers to snake case

while here:
- add some functions taking std::string/std::wstring
master
Tomasz Sowa 3 months ago
parent f97c06d441
commit b3137a7607

@ -17,9 +17,9 @@ tests: FORCE
$(MAKE) -C tests
tests-gcc10: FORCE
env CXX=g++10 CXXFLAGS="-Wl,-rpath=/usr/local/lib/gcc10/ -Wall -pedantic -O0 -g3 -std=c++20 -fmax-errors=1 -I../src -I/usr/local/include" $(MAKE) -C src
env CXX=g++10 CXXFLAGS="-Wl,-rpath=/usr/local/lib/gcc10/ -Wall -pedantic -O0 -g3 -std=c++20 -fmax-errors=1 -I../src -I/usr/local/include" $(MAKE) -C tests
tests-gcc11: FORCE
env CXX=g++11 CXXFLAGS="-Wl,-rpath=/usr/local/lib/gcc11/ -Wall -pedantic -O0 -g3 -std=c++20 -fmax-errors=1 -I../src -I/usr/local/include" $(MAKE) -C src
env CXX=g++11 CXXFLAGS="-Wl,-rpath=/usr/local/lib/gcc11/ -Wall -pedantic -O0 -g3 -std=c++20 -fmax-errors=1 -I../src -I/usr/local/include" $(MAKE) -C tests
tests-clang: FORCE

@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2017-2021, Tomasz Sowa
* Copyright (c) 2017-2022, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -44,10 +44,8 @@ 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 to_ull(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;
@ -96,8 +94,16 @@ unsigned long long Toull(const CharType * str, int base = 10, const CharType **
}
template<class StringType>
unsigned long long to_ull(const StringType & str, int base = 10, const typename StringType::value_type ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
{
return to_ull(str.c_str(), base, after_str, was_overflow, allow_skip_whitechars);
}
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)
long long to_ll(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;
@ -113,7 +119,7 @@ long long Toll(const CharType * str, int base = 10, const CharType ** after_str
str += 1;
}
unsigned long long uval = Toull(str, base, after_str, &was_overflow_u, false);
unsigned long long uval = to_ull(str, base, after_str, &was_overflow_u, false);
unsigned long long sign_add = ( was_sign ) ? 1 : 0;
if( was_overflow_u )
@ -138,14 +144,21 @@ long long Toll(const CharType * str, int base = 10, const CharType ** after_str
template<class StringType>
long long to_ll(const StringType & str, int base = 10, const typename StringType::value_type ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
{
return to_ll(str.c_str(), base, after_str, was_overflow, allow_skip_whitechars);
}
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)
IntegerType to_unsigned_integer_type(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);
unsigned long long val = to_ull(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()) )
{
@ -158,28 +171,39 @@ IntegerType ToUnsignedIntegerType(const CharType * str, int base = 10, const Cha
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)
unsigned long to_ul(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
{
return to_unsigned_integer_type<CharType, unsigned long>(str, base, after_str, was_overflow, allow_skip_whitechars);
}
template<class StringType>
unsigned long to_ul(const StringType & str, int base = 10, const typename StringType::value_type ** 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);
return to_unsigned_integer_type<typename StringType::value_type, unsigned long>(str.c_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)
unsigned int to_ui(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);
return to_unsigned_integer_type<CharType, unsigned int>(str, base, after_str, was_overflow, allow_skip_whitechars);
}
template<class StringType>
unsigned int to_ui(const StringType & str, int base = 10, const typename StringType::value_type ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
{
return to_unsigned_integer_type<typename StringType::value_type, unsigned int>(str.c_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)
IntegerType to_integer_type(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);
long long val = to_ll(str, base, after_str, &was_overflow_ll, allow_skip_whitechars);
if( was_overflow_ll ||
val < static_cast<long long>(std::numeric_limits<IntegerType>::min()) ||
@ -195,22 +219,28 @@ IntegerType ToIntegerType(const CharType * str, int base = 10, const CharType **
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)
long to_l(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);
return to_integer_type<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)
template<class StringType>
long to_l(const StringType & str, int base = 10, const typename StringType::value_type ** 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);
return to_integer_type<typename StringType::value_type, long>(str.c_str(), base, after_str, was_overflow, allow_skip_whitechars);
}
template<class CharType>
int to_i(const CharType * str, int base = 10, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
{
return to_integer_type<CharType, int>(str, base, after_str, was_overflow, allow_skip_whitechars);
}
template<class StringType>
int to_i(const StringType & str, int base = 10, const typename StringType::value_type ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
{
return to_integer_type<typename StringType::value_type, int>(str.c_str(), base, after_str, was_overflow, allow_skip_whitechars);
}
/*
@ -224,7 +254,7 @@ int Toi(const CharType * str, int base = 10, const CharType ** after_str = 0, bo
*
*/
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)
unsigned long long to_ull_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
{
if( allow_skip_whitechars )
str = skip_white(str);
@ -249,13 +279,13 @@ unsigned long long Toull_b(const CharType * str, const CharType ** after_str = 0
str += 1;
}
return Toull(str, base, after_str, was_overflow, false);
return to_ull(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)
long long to_ll_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;
@ -271,7 +301,7 @@ long long Toll_b(const CharType * str, const CharType ** after_str = 0, bool * w
str += 1;
}
unsigned long long uval = Toull_b(str, after_str, &was_overflow_u, false);
unsigned long long uval = to_ull_b(str, after_str, &was_overflow_u, false);
unsigned long long sign_add = ( was_sign ) ? 1 : 0;
if( was_overflow_u )
@ -302,12 +332,12 @@ long long Toll_b(const CharType * str, const CharType ** after_str = 0, bool * w
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)
IntegerType to_unsigned_integer_type_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);
unsigned long long val = to_ull_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()) )
{
@ -320,15 +350,15 @@ IntegerType ToUnsignedIntegerType_b(const CharType * str, const CharType ** afte
template<class CharType>
unsigned long Toul_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
unsigned long to_ul_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);
return to_unsigned_integer_type_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)
unsigned int to_ui_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);
return to_unsigned_integer_type_b<CharType, unsigned int>(str, after_str, was_overflow, allow_skip_whitechars);
}
@ -336,12 +366,12 @@ unsigned int Toui_b(const CharType * str, const CharType ** after_str = 0, bool
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)
IntegerType to_integer_type_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);
long long val = to_ll_b(str, after_str, &was_overflow_ll, allow_skip_whitechars);
if( was_overflow_ll ||
val < static_cast<long long>(std::numeric_limits<IntegerType>::min()) ||
@ -357,15 +387,15 @@ IntegerType ToIntegerType_b(const CharType * str, const CharType ** after_str =
template<class CharType>
long Tol_b(const CharType * str, const CharType ** after_str = 0, bool * was_overflow = 0, bool allow_skip_whitechars = true)
long to_l_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);
return to_integer_type_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)
int to_i_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);
return to_integer_type_b<CharType, int>(str, after_str, was_overflow, allow_skip_whitechars);
}

@ -1072,7 +1072,7 @@ long long Space::convert_string_to_long_long() const
{
bool was_overflow = false;
const char * after_str;
long long val = Toll(value.value_string.c_str(), 10, &after_str, &was_overflow, true);
long long val = to_ll(value.value_string.c_str(), 10, &after_str, &was_overflow, true);
return was_overflow ? 0 : val;
}
@ -1082,7 +1082,7 @@ long long Space::convert_wstring_to_long_long() const
{
bool was_overflow = false;
const wchar_t * after_str;
long long val = Toll(value.value_wstring.c_str(), 10, &after_str, &was_overflow, true);
long long val = to_ll(value.value_wstring.c_str(), 10, &after_str, &was_overflow, true);
return was_overflow ? 0 : val;
}
@ -1092,7 +1092,7 @@ unsigned long long Space::convert_string_to_ulong_long() const
{
bool was_overflow = false;
const char * after_str;
unsigned long long val = Toull(value.value_string.c_str(), 10, &after_str, &was_overflow, true);
unsigned long long val = to_ull(value.value_string.c_str(), 10, &after_str, &was_overflow, true);
return was_overflow ? 0 : val;
}
@ -1102,7 +1102,7 @@ unsigned long long Space::convert_wstring_to_ulong_long() const
{
bool was_overflow = false;
const wchar_t * after_str;
unsigned long long val = Toull(value.value_wstring.c_str(), 10, &after_str, &was_overflow, true);
unsigned long long val = to_ull(value.value_wstring.c_str(), 10, &after_str, &was_overflow, true);
return was_overflow ? 0 : val;
}

@ -572,7 +572,7 @@ void SpaceParser::parse_integer_value(Space * space)
base = 8;
}
long long val = Toll(token.c_str(), base, &after_str, &was_overflow, false);
long long val = to_ll(token.c_str(), base, &after_str, &was_overflow, false);
if( was_overflow )
{

@ -1,15 +1,15 @@
# DO NOT DELETE
./convert.o: convert.h test.h ../src/convert/convert.h
./convert.o: ../src/convert/inttostr.h ../src/convert/patternreplacer.h
./convert.o: ../src/textstream/textstream.h ../src/textstream/stream.h
./convert.o: ../src/space/space.h ../src/textstream/types.h
./convert.o: ../src/convert/inttostr.h ../src/utf8/utf8.h
./convert.o: ../src/textstream/stream.h ../src/utf8/utf8_templates.h
./convert.o: ../src/utf8/utf8_private.h ../src/date/date.h
./convert.o: ../src/membuffer/membuffer.h ../src/textstream/types.h
./convert.o: ../src/convert/strtoint.h ../src/convert/text.h
./convert.o: ../src/convert/misc.h ../src/convert/double.h
./convert.o: convert.h ../src/convert/convert.h ../src/convert/inttostr.h
./convert.o: ../src/convert/patternreplacer.h ../src/textstream/textstream.h
./convert.o: ../src/textstream/stream.h ../src/space/space.h
./convert.o: ../src/textstream/types.h ../src/convert/inttostr.h
./convert.o: ../src/utf8/utf8.h ../src/textstream/stream.h
./convert.o: ../src/utf8/utf8_templates.h ../src/utf8/utf8_private.h
./convert.o: ../src/date/date.h ../src/membuffer/membuffer.h
./convert.o: ../src/textstream/types.h ../src/convert/strtoint.h
./convert.o: ../src/convert/text.h ../src/convert/misc.h
./convert.o: ../src/convert/double.h test.h
./csvparser.o: csvparser.h ../src/csv/csvparser.h ../src/space/space.h
./csvparser.o: ../src/textstream/types.h ../src/convert/inttostr.h
./csvparser.o: ../src/utf8/utf8.h ../src/textstream/stream.h
@ -17,7 +17,16 @@
./csvparser.o: ../src/convert/baseparser.h ../src/textstream/textstream.h
./csvparser.o: ../src/textstream/stream.h ../src/date/date.h
./csvparser.o: ../src/membuffer/membuffer.h ../src/textstream/types.h test.h
./main.o: convert.h mainoptionsparser.h csvparser.h
./main.o: convert.h ../src/convert/convert.h ../src/convert/inttostr.h
./main.o: ../src/convert/patternreplacer.h ../src/textstream/textstream.h
./main.o: ../src/textstream/stream.h ../src/space/space.h
./main.o: ../src/textstream/types.h ../src/convert/inttostr.h
./main.o: ../src/utf8/utf8.h ../src/textstream/stream.h
./main.o: ../src/utf8/utf8_templates.h ../src/utf8/utf8_private.h
./main.o: ../src/date/date.h ../src/membuffer/membuffer.h
./main.o: ../src/textstream/types.h ../src/convert/strtoint.h
./main.o: ../src/convert/text.h ../src/convert/misc.h ../src/convert/double.h
./main.o: test.h mainoptionsparser.h csvparser.h
./test.o: test.h
./mainoptionsparser.o: mainoptionsparser.h test.h
./mainoptionsparser.o: ../src/mainoptions/mainoptionsparser.h

@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2021, Tomasz Sowa
* Copyright (c) 2021-2022, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -34,8 +34,6 @@
#include <iostream>
#include "convert.h"
#include "test.h"
#include "convert/convert.h"
namespace pt
@ -1919,6 +1917,181 @@ void test_text46()
}
void test_text47()
{
reset_test_counter("to_ull");
IntegerHelper<unsigned long long> helper_tab[] = {
{L"", 0ULL, 10, L"", false, false},
{L"0", 0ULL, 10, L"", false, false},
{L"00", 0ULL, 10, L"", false, false},
{L"1", 1ULL, 10, L"", false, false},
{L"50", 50ULL, 10, L"", false, false},
{L"050", 50ULL, 10, L"", false, false},
{L"100", 100ULL, 10, L"", false, false},
{L"00100", 100ULL, 10, L"", false, false},
{L"128", 128ULL, 10, L"", false, false},
{L"1000", 1000ULL, 10, L"", false, false},
{L"65535", 65535ULL, 10, L"", false, false},
{L"65536", 65536ULL, 10, L"", false, false},
{L"65537", 65537ULL, 10, L"", false, false},
{L"4294967295", 4294967295ULL, 10, L"", false, false},
{L"4294967296", 4294967296ULL, 10, L"", false, false},
{L"4294967297", 4294967297ULL, 10, L"", false, false},
{L"18446744073709551614", 18446744073709551614ULL, 10, L"", false, false},
{L"18446744073709551615", 18446744073709551615ULL, 10, L"", false, false},
{L" 1024", 0ULL, 10, L" 1024", false, false},
{L" 1024", 1024ULL, 10, L"", false, true},
{L" \t 1024", 0ULL, 10, L" \t 1024", false, false},
{L" \t 1024", 1024ULL, 10, L"", false, true},
{L" 1024foo", 0ULL, 10, L" 1024foo", false, false},
{L" 1024foo", 1024ULL, 10, L"foo", false, true},
{L" \t 1024foo", 0ULL, 10, L" \t 1024foo", false, false},
{L" \t 1024foo", 1024ULL, 10, L"foo", false, true},
{L"", 0ULL, 2, L"", false, false},
{L"0", 0ULL, 2, L"", false, false},
{L"1", 1ULL, 2, L"", false, false},
{L"2", 0ULL, 2, L"2", false, false},
{L"101001011", 0b101001011ULL, 2, L"", false, false},
{L"001011001101", 0b001011001101ULL, 2, L"", false, false},
{L"", 0ULL, 8, L"", false, false},
{L"0", 0ULL, 8, L"", false, false},
{L"1", 1ULL, 8, L"", false, false},
{L"7", 7ULL, 8, L"", false, false},
{L"8", 0ULL, 8, L"8", false, false},
{L"54321760123", 054321760123ULL, 8, L"", false, false},
{L"54012418", 05401241ULL, 8, L"8", false, false},
{L"01256252foo", 01256252ULL, 8, L"foo", false, false},
{L"", 0ULL, 16, L"", false, false},
{L"0", 0ULL, 16, L"", false, false},
{L"9", 9ULL, 16, L"", false, false},
{L"a", 10ULL, 16, L"", false, false},
{L"f", 15ULL, 16, L"", false, false},
{L"g", 0ULL, 16, L"g", false, false},
{L"A", 10ULL, 16, L"", false, false},
{L"F", 15ULL, 16, L"", false, false},
{L"G", 0ULL, 16, L"G", false, false},
{L"FFFF", 0xFFFFULL, 16, L"", false, false},
{L"54FB91", 0x54FB91ULL, 16, L"", false, false},
{L"abcdef00123", 0xabcdef00123ULL, 16, L"", false, false},
{L"0a65bC3aDB1", 0x0a65bC3aDB1ULL, 16, L"", false, false},
{L"0a65bC3aDB1g", 0x0a65bC3aDB1ULL, 16, L"g", false, false},
{L"0a65bC3aDB1G", 0x0a65bC3aDB1ULL, 16, L"G", false, false},
{L"0a65bC3aDB1z", 0x0a65bC3aDB1ULL, 16, L"z", false, false},
{L"0a65bC3aDB1Z", 0x0a65bC3aDB1ULL, 16, L"Z", false, false},
};
size_t len = sizeof(helper_tab) / sizeof(IntegerHelper<unsigned long long>);
make_str_to_int_tests<std::wstring, unsigned long long>(&to_ull, &to_ull, helper_tab, len);
}
void test_text48()
{
reset_test_counter("to_ll");
IntegerHelper<long long> helper_tab[] = {
{L"", 0LL, 10, L"", false, false},
{L"0", 0LL, 10, L"", false, false},
{L"-0", 0LL, 10, L"", false, false},
{L"00", 0LL, 10, L"", false, false},
{L"-00", 0LL, 10, L"", false, false},
{L"100", 100LL, 10, L"", false, false},
{L"-100", -100LL, 10, L"", false, false},
{L"00100", 100LL, 10, L"", false, false},
{L"-00100", -100LL, 10, L"", false, false},
{L"128", 128LL, 10, L"", false, false},
{L"-128", -128LL, 10, L"", false, false},
{L"1000", 1000LL, 10, L"", false, false},
{L"-1000", -1000LL, 10, L"", false, false},
{L"65535", 65535LL, 10, L"", false, false},
{L"65536", 65536LL, 10, L"", false, false},
{L"65537", 65537LL, 10, L"", false, false},
{L"-65535", -65535LL, 10, L"", false, false},
{L"-65536", -65536LL, 10, L"", false, false},
{L"-65537", -65537LL, 10, L"", false, false},
{L"4294967295", 4294967295LL, 10, L"", false, false},
{L"4294967296", 4294967296LL, 10, L"", false, false},
{L"4294967297", 4294967297LL, 10, L"", false, false},
{L"-4294967295", -4294967295LL, 10, L"", false, false},
{L"-4294967296", -4294967296LL, 10, L"", false, false},
{L"-4294967297", -4294967297LL, 10, L"", false, false},
{L"9223372036854775806", 9223372036854775806LL, 10, L"", false, false},
{L"9223372036854775807", 9223372036854775807LL, 10, L"", false, false},
{L"9223372036854775808", 0LL, 10, L"", true, false},
{L"-9223372036854775807", -9223372036854775807LL, 10, L"", false, false},
{L"-9223372036854775808", std::numeric_limits<long long>::min(), 10, L"", false, false}, // gcc and clang not allow -9223372036854775808LL as a literal
{L"-9223372036854775809", 0LL, 10, L"", true, false},
{L"123456789876543210123", 0LL, 10, L"", true, false},
{L"-123456789876543210123", 0LL, 10, L"", true, false},
{L" -1024", 0LL, 10, L" -1024", false, false},
{L" -1024", -1024LL, 10, L"", false, true},
{L" \t -1024", 0LL, 10, L" \t -1024", false, false},
{L" \t -1024", -1024LL, 10, L"", false, true},
{L" -1024foo", 0LL, 10, L" -1024foo", false, false},
{L" -1024foo", -1024LL, 10, L"foo", false, true},
{L" \t -1024foo", 0LL, 10, L" \t -1024foo", false, false},
{L" \t -1024foo", -1024LL, 10, L"foo", false, true},
{L"", 0LL, 2, L"", false, false},
{L"0", 0LL, 2, L"", false, false},
{L"-0", 0LL, 2, L"", false, false},
{L"1", 1LL, 2, L"", false, false},
{L"2", 0LL, 2, L"2", false, false},
{L"-1", -1LL, 2, L"", false, false},
{L"-2", 0LL, 2, L"2", false, false},
{L"-101001011", -0b101001011LL, 2, L"", false, false},
{L"-001011001101", -0b001011001101LL, 2, L"", false, false},
{L"", 0LL, 8, L"", false, false},
{L"0", 0LL, 8, L"", false, false},
{L"-0", 0LL, 8, L"", false, false},
{L"1", 1LL, 8, L"", false, false},
{L"7", 7LL, 8, L"", false, false},
{L"8", 0LL, 8, L"8", false, false},
{L"-1", -1LL, 8, L"", false, false},
{L"-7", -7LL, 8, L"", false, false},
{L"-8", 0LL, 8, L"8", false, false},
{L"54321760123", 054321760123LL, 8, L"", false, false},
{L"54012418", 05401241LL, 8, L"8", false, false},
{L"01256252foo", 01256252LL, 8, L"foo", false, false},
{L"-54321760123", -054321760123LL, 8, L"", false, false},
{L"-54012418", -05401241LL, 8, L"8", false, false},
{L"-01256252foo", -01256252LL, 8, L"foo", false, false},
{L"", 0LL, 16, L"", false, false},
{L"0", 0LL, 16, L"", false, false},
{L"-0", 0LL, 16, L"", false, false},
{L"ab65c", 0xab65cLL, 16, L"", false, false},
{L"-654FEc23", -0x654FEc23LL, 16, L"", false, false},
{L"076af12b", 0x76af12bLL, 16, L"", false, false},
{L"-076de12be", -0x076de12beLL, 16, L"", false, false},
{L"7FFFFFFFFFFFFFFF", 0x7FFFFFFFFFFFFFFFLL, 16, L"", false, false},
{L"8000000000000000", 0LL, 16, L"", true, false},
{L"8000000000000001", 0LL, 16, L"", true, false},
{L"abcdef0123456789", 0LL, 16, L"", true, false},
{L"-7FFFFFFFFFFFFFFF", -0x7FFFFFFFFFFFFFFFLL, 16, L"", false, false},
{L"-8000000000000000", std::numeric_limits<long long>::min(), 16, L"", false, false},
{L"-8000000000000001", 0LL, 16, L"", true, false},
{L"-abcdef0123456789", 0LL, 16, L"", true, false},
};
size_t len = sizeof(helper_tab) / sizeof(IntegerHelper<long long>);
make_str_to_int_tests<std::wstring, long long>(&to_ll, &to_ll, helper_tab, len);
}
// IMPROVEME put integer tests for functions: to_ul(), to_ui(), to_l(), to_i(), to_ull_b(), to_ll_b(), to_ul_b(), to_ui_b(), to_l_b(), to_i_b()
void make_tests()
@ -1969,6 +2142,8 @@ void make_tests()
test_text44();
test_text45();
test_text46();
test_text47();
test_text48();
}

@ -5,7 +5,7 @@
*/
/*
* Copyright (c) 2021, Tomasz Sowa
* Copyright (c) 2021-2022, Tomasz Sowa
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
@ -35,12 +35,53 @@
#ifndef headerfile_pikotools_tests_convert
#define headerfile_pikotools_tests_convert
#include "convert/convert.h"
#include "test.h"
namespace pt
{
namespace pt_convert_tests
{
template<typename IntegerType>
struct IntegerHelper {
const wchar_t * val;
IntegerType val_int;
int base;
const wchar_t * after_str;
bool was_overflow;
bool allow_skip_whitechars;
};
template<typename StringType, typename IntegerType>
void make_str_to_int_tests(
IntegerType (*convert_function1)(const typename StringType::value_type *, int, const typename StringType::value_type **, bool *, bool allow_skip_whitechars),
IntegerType (*convert_function2)(const StringType &, int, const typename StringType::value_type **, bool *, bool allow_skip_whitechars),
IntegerHelper<IntegerType> * helper_tab,
size_t len)
{
for(size_t i=0 ; i<len ; ++i)
{
IntegerHelper<IntegerType> & helper = helper_tab[i];
std::wstring str1 = helper.val;
const wchar_t * after;
bool was_overflow;
test(convert_function1(str1.c_str(), helper.base, &after, &was_overflow, helper.allow_skip_whitechars), helper.val_int);
test(helper.after_str, after);
test(helper.was_overflow, was_overflow);
test(convert_function2(str1, helper.base, &after, &was_overflow, helper.allow_skip_whitechars), helper.val_int);
test(helper.after_str, after);
test(helper.was_overflow, was_overflow);
}
}
void make_tests();

Loading…
Cancel
Save