Skip to content

Commit

Permalink
refactor: Revisit palindrome
Browse files Browse the repository at this point in the history
  • Loading branch information
oboukli committed Jul 4, 2024
1 parent ef46829 commit 1d16c94
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions include/forfun/palindrome.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ is_palindrome(std::basic_string_view<CharT> const s) noexcept -> bool
{
using SizeType = decltype(s)::size_type;

auto const end{s.length() - 1};
auto const mid{s.length() / 2};
auto const length{s.length()};

for (SizeType i{0}; i < mid; ++i)
auto end{length - 1};
auto const mid{length / 2};

for (SizeType i{0}; i != mid; ++i)
{
if (s[i] != s[end - i])
if (s[i] != s[end--])
{
return false;
}
Expand Down Expand Up @@ -69,10 +71,14 @@ is_palindrome(std::basic_string_view<CharT> const s) noexcept -> bool
{
using ConstItr = decltype(s)::const_iterator;

auto const cbegin{s.cbegin()};
auto upper{s.crbegin()};
ConstItr const mid{s.cbegin() + (s.length() / 2)};

for (ConstItr lower{s.cbegin()}; lower < mid; ++lower)
static_assert(std::contiguous_iterator<ConstItr>);
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
ConstItr const mid{cbegin + (s.length() / 2)};

for (ConstItr lower{cbegin}; lower < mid; ++lower)
{
if ((*lower) != (*upper))
{
Expand Down Expand Up @@ -120,8 +126,9 @@ is_palindrome(std::basic_string_view<CharT> const s) noexcept -> bool
{
using ConstItr = decltype(s)::const_iterator;

ConstItr const begin{s.cbegin()};
return std::equal(begin, begin + (s.size() / 2), s.crbegin());
ConstItr const cbegin{s.cbegin()};

return std::equal(cbegin, cbegin + (s.length() / 2), s.crbegin());
}

namespace bloated {
Expand All @@ -137,7 +144,7 @@ is_palindrome(std::basic_string_view<CharT> const s) noexcept -> bool
s.cbegin(),
std::next(
s.cbegin(),
static_cast<decltype(s)::difference_type>(s.size() / 2)),
static_cast<decltype(s)::difference_type>(s.length() / 2)),
s.crbegin());
}

Expand All @@ -159,7 +166,7 @@ inline auto is_palindrome_ci(std::string_view const s) noexcept -> bool
s.cbegin(),
std::next(
s.cbegin(),
static_cast<decltype(s)::difference_type>(s.size() / 2)),
static_cast<decltype(s)::difference_type>(s.length() / 2)),
s.crbegin(),
detail::equal_case_insensitive);
}
Expand Down

0 comments on commit 1d16c94

Please sign in to comment.