Skip to content

Commit

Permalink
MSVC fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
eliaskosunen committed Jan 17, 2024
1 parent 5f20acc commit 46deb9a
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 11 deletions.
24 changes: 21 additions & 3 deletions src/scn/impl/reader/integer_reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -226,17 +226,35 @@ auto parse_integer_value(std::basic_string_view<CharT> source,
"Invalid integer value");
}

// Skip leading zeroes
auto start = source.data();
const auto end = source.data() + source.size();
{
for (; start != end; ++start) {
if (*start != CharT{'0'}) {
break;
}
}
if (SCN_UNLIKELY(start == end || char_to_int(*start) >= base)) {
value = 0;
return ranges::next(source.begin(),
ranges::distance(source.data(), start));
}
}

if constexpr (std::is_same_v<CharT, char>) {
if (base == 10) {
SCN_TRY(ptr, parse_decimal_integer_fast(
source, value, sign == sign_type::minus_sign));
detail::make_string_view_from_pointers(start, end),
value, sign == sign_type::minus_sign));
return ranges::next(source.begin(),
ranges::distance(source.data(), ptr));
}
}

SCN_TRY(ptr, parse_regular_integer(source, value, base,
sign == sign_type::minus_sign));
SCN_TRY(ptr, parse_regular_integer(
detail::make_string_view_from_pointers(start, end), value,
base, sign == sign_type::minus_sign));
return ranges::next(source.begin(), ranges::distance(source.data(), ptr));
}

Expand Down
28 changes: 22 additions & 6 deletions tests/unittests/compilefail_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
include(icm_build_failure_testing)

icm_add_multiple_build_failure_tests(
SOURCES
set(buildfail_sources
brace_as_fill_character.cpp
charset_empty.cpp
charset_reversed_range.cpp
Expand All @@ -24,8 +23,25 @@ icm_add_multiple_build_failure_tests(
string_view_non_contiguous_source.cpp
unterminated_argument_id.cpp
unterminated_format_specifier.cpp
PREFIX
scn-compilefail
LIBRARIES
scn
)

if (SCN_CXX_FRONTEND STREQUAL "GNU" OR SCN_CXX_FRONTEND STREQUAL "Clang")
icm_add_multiple_build_failure_tests(
SOURCES ${buildfail_sources}
PREFIX scn-compilefail
LIBRARIES scn
)
else ()
# We expect the error messages to contain the call to on_error("..."),
# which contains the library error message.
# Seemingly, only gcc and clang do that.
message(STATUS "Compiler frontend not gcc or clang -- build failure tests don't check the compiler output")
foreach (source ${buildfail_sources})
get_filename_component(source_name ${source} NAME_WE)
icm_add_build_failure_test(
NAME scn-compilefail-${source_name}
LIBRARIES scn
SOURCES ${source}
)
endforeach ()
endif ()
21 changes: 19 additions & 2 deletions tests/unittests/integer_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ namespace {
auto result = scn::scan<Args...>(src, fmt);
if (!result) {
return {testing::AssertionFailure()
<< "scan failed with " << result.error().code(),
<< "scan failed with " << result.error().code() << ": "
<< result.error().msg(),
Args{}...};
}
if (result->begin() != src.end()) {
Expand Down Expand Up @@ -93,6 +94,22 @@ TEST(IntegerTest, UnsignedWithUnsignedFormat)
EXPECT_EQ(val, 42);
}

TEST(IntegerTest, LeadingZeroesInDecimal) {
auto [result, val] = do_test<short>("0000000000000000100", "{:d}");
ASSERT_TRUE(result);
EXPECT_EQ(val, 100);
}
TEST(IntegerTest, LeadingZeroesInHexadecimalWithoutPrefix) {
auto [result, val] = do_test<short>("0000000000000000100", "{:x}");
ASSERT_TRUE(result);
EXPECT_EQ(val, 0x100);
}
TEST(IntegerTest, LeadingZeroesInHexadecimalWithPrefix) {
auto [result, val] = do_test<short>("0x0000000000000000100", "{}");
ASSERT_TRUE(result);
EXPECT_EQ(val, 0x100);
}

TEST(IntegerTest, Pointer)
{
char source_buf[64]{};
Expand All @@ -101,7 +118,7 @@ TEST(IntegerTest, Pointer)
std::string_view source{source_buf};

auto [result, val] = do_test<void*>(source, "{}");
EXPECT_TRUE(result);
ASSERT_TRUE(result);
ASSERT_NE(val, nullptr);
EXPECT_EQ(val, &value);
EXPECT_EQ(*static_cast<const int*>(val), value);
Expand Down

0 comments on commit 46deb9a

Please sign in to comment.