Skip to content

Commit

Permalink
Fix documentation: default format specifier for ints is i, not d
Browse files Browse the repository at this point in the history
  • Loading branch information
eliaskosunen committed May 18, 2024
1 parent f5d24c1 commit ff77a65
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 4 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ _Released 2024-05-xx_

## Fixes

* Fix documentation: default format type specifier for integers is `i`, not `d`:
when not explicitly specified by a format specifier, the base of an integer is determined based on its prefix:
`0x...` is hexadecimal, `0...` or `0o...` is octal, `0b...` is binary, and everything else is decimal.
* Small CMake adjustments to better support use as a subproject (#113, thanks [@frankmiller (Frank Miller)](https://github.com/frankmiller))
* Fix misplaced include of `GNUInstallDirs` in CMake (#111, thanks [@WangWeiLin-MV](https://github.com/WangWeiLin-MV))
* Allow for externally installed versions for GTest and Google Benchmark (#112, thanks [@xvitaly (Vitaly)](https://github.com/xvitaly))
Expand Down
2 changes: 1 addition & 1 deletion include/scn/detail/format_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@
* <tr>
* <td>none</td>
* <td>
* Same as `d`.
* Same as `i`.
* </td>
* </tr>
* </table>
Expand Down
4 changes: 2 additions & 2 deletions include/scn/detail/format_string_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -191,14 +191,14 @@ struct format_specs {

constexpr format_specs() = default;

SCN_NODISCARD constexpr int get_base(int default_base) const
SCN_NODISCARD constexpr int get_base() const
{
SCN_GCC_COMPAT_PUSH
SCN_GCC_COMPAT_IGNORE("-Wswitch-enum")
switch (type) {
case presentation_type::none:
case presentation_type::int_generic:
return default_base;
return 0;
case presentation_type::int_arbitrary_base:
return arbitrary_base;

Expand Down
2 changes: 1 addition & 1 deletion src/scn/impl/reader/integer_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ class reader_impl_for_int
T& value,
detail::locale_ref loc)
{
SCN_TRY(prefix_result, parse_integer_prefix(range, specs.get_base(0))
SCN_TRY(prefix_result, parse_integer_prefix(range, specs.get_base())
.transform_error(make_eof_scan_error));

if (prefix_result.sign == sign_type::minus_sign) {
Expand Down
28 changes: 28 additions & 0 deletions tests/unittests/impl_tests/integer_reader_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ class IntValueReaderTest : public testing::Test {
{
return std::make_pair(int_type{077}, "0o77"sv);
}
static auto get_oct_followed_by_dec()
{
return std::make_pair(int_type{0}, "08"sv);
}

static auto get_bin()
{
Expand Down Expand Up @@ -564,6 +568,28 @@ TYPED_TEST_P(IntValueReaderTest, OctAltDetected)
EXPECT_TRUE(a);
EXPECT_EQ(val, orig_val);
}
TYPED_TEST_P(IntValueReaderTest, OctFollowedByDec)
{
const auto [orig_val, src] = this->get_oct_followed_by_dec();
const auto [result, val] = this->simple_specs_test(
src, this->make_format_specs_with_presentation_and_base(
scn::detail::presentation_type::int_octal));
ASSERT_TRUE(result);
EXPECT_NE(scn::detail::to_address(result.value()),
scn::detail::to_address(this->widened_source->end()));
EXPECT_EQ(val, orig_val);
}
TYPED_TEST_P(IntValueReaderTest, OctFollowedByDecDetected)
{
const auto [orig_val, src] = this->get_oct_followed_by_dec();
const auto [result, val] = this->simple_specs_test(
src, this->make_format_specs_with_presentation_and_base(
scn::detail::presentation_type::none));
ASSERT_TRUE(result);
EXPECT_NE(scn::detail::to_address(result.value()),
scn::detail::to_address(this->widened_source->end()));
EXPECT_EQ(val, orig_val);
}

TYPED_TEST_P(IntValueReaderTest, Bin)
{
Expand Down Expand Up @@ -881,6 +907,8 @@ REGISTER_TYPED_TEST_SUITE_P(IntValueReaderTest,
Oct,
OctDetect,
OctAltDetected,
OctFollowedByDec,
OctFollowedByDecDetected,
Bin,
BinDetect,
Ternary,
Expand Down
25 changes: 25 additions & 0 deletions tests/unittests/scan_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,3 +180,28 @@ TEST(ScanTest, FuzzerFailDequeInput)
auto result = scn::scan<std::string>(rng, "{}");
ASSERT_FALSE(result);
}

TEST(ScanTest, DeconstructedTimestamp)
{
auto res = scn::scan<int, int, int, int, int, double>(
"2024-03-23T09:20:33.576864", "{:4d}-{:2d}-{:2d}T{:2d}:{:2d}:{}");
ASSERT_TRUE(res);
EXPECT_EQ(std::get<0>(res->values()), 2024);
EXPECT_EQ(std::get<1>(res->values()), 3);
EXPECT_EQ(std::get<2>(res->values()), 23);
EXPECT_EQ(std::get<3>(res->values()), 9);
EXPECT_EQ(std::get<4>(res->values()), 20);
EXPECT_DOUBLE_EQ(std::get<5>(res->values()), 33.576864);
}
TEST(ScanTest, DeconstructedTimestamp2)
{
auto res = scn::scan<int, int, int, int, int>(
"2024-03-23T09:20:33.576864", "{:4d}-{:2d}-{:2d}T{:2d}:{:2d}:");
ASSERT_TRUE(res);
EXPECT_EQ(std::get<0>(res->values()), 2024);
EXPECT_EQ(std::get<1>(res->values()), 3);
EXPECT_EQ(std::get<2>(res->values()), 23);
EXPECT_EQ(std::get<3>(res->values()), 9);
EXPECT_EQ(std::get<4>(res->values()), 20);
EXPECT_STREQ(res->range().data(), "33.576864");
}

0 comments on commit ff77a65

Please sign in to comment.