From 3879d752190018fc06cb34251c22ad6273cab8a4 Mon Sep 17 00:00:00 2001 From: Shannon Booth Date: Sat, 15 Feb 2020 13:04:09 +1300 Subject: [PATCH] LibCore: Simplify some of DirIterator's code The main changes are in advance_next() where we flatten some of the nesting to improve readability --- Libraries/LibCore/DirIterator.cpp | 33 +++++++++++++------------------ 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/Libraries/LibCore/DirIterator.cpp b/Libraries/LibCore/DirIterator.cpp index 066a30d818..f009d6c9f3 100644 --- a/Libraries/LibCore/DirIterator.cpp +++ b/Libraries/LibCore/DirIterator.cpp @@ -33,14 +33,14 @@ DirIterator::DirIterator(const StringView& path, Flags flags) : m_flags(flags) { m_dir = opendir(String(path).characters()); - if (m_dir == nullptr) { + if (!m_dir) { m_error = errno; } } DirIterator::~DirIterator() { - if (m_dir != nullptr) { + if (m_dir) { closedir(m_dir); m_dir = nullptr; } @@ -48,32 +48,27 @@ DirIterator::~DirIterator() bool DirIterator::advance_next() { - if (m_dir == nullptr) + if (!m_dir) return false; - bool keep_advancing = true; - while (keep_advancing) { + while (true) { errno = 0; auto* de = readdir(m_dir); - if (de) { - m_next = de->d_name; - } else { + if (!de) { m_error = errno; m_next = String(); + return false; } - if (m_next.is_null()) { - keep_advancing = false; - } else if (m_flags & Flags::SkipDots) { - if (m_next.length() < 1 || m_next[0] != '.') { - keep_advancing = false; - } - } else { - keep_advancing = false; - } + m_next = de->d_name; + if (m_next.is_null()) + return false; + + if (m_flags & Flags::SkipDots && m_next.starts_with('.')) + continue; + + return !m_next.is_empty(); } - - return m_next.length() > 0; } bool DirIterator::has_next()