Skip to content

Commit

Permalink
c++/isbn-verifier: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
vpayno committed Oct 26, 2023
1 parent f2a4bef commit b51a849
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 49 deletions.
15 changes: 2 additions & 13 deletions cpp/isbn-verifier/isbn_verifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,22 @@ namespace isbn_verifier {
bool is_valid(isbn_number_t number) {
bool result{false};

if (number.empty()) {
return result;
}

const std::string re_valid_str{R"--(^[0-9][0-9-]+[0-9X]$)--"};
const std::string re_valid_str{
R"--(^[0-9]-?[0-9]{3}-?[0-9]{5}-?[0-9X]$)--"};
const std::regex re_valid_exp(re_valid_str, std::regex_constants::egrep);
if (!std::regex_search(number, re_valid_exp)) {
return result;
}

const std::string re_digits_str{R"--([0-9]|X)--"};
const std::regex re_digits_exp(re_digits_str, std::regex_constants::egrep);

auto digits_begin =
std::sregex_iterator(number.begin(), number.end(), re_digits_exp);
auto digits_end = std::sregex_iterator();

int sum{0};
int pos{10};

auto digit_count = std::distance(digits_begin, digits_end);

if (digit_count != 10) {
return result;
}

for (auto iter = digits_begin; iter != digits_end; iter++) {
auto re_match = *iter;
const std::string digit_str = re_match[0];
Expand All @@ -44,7 +34,6 @@ bool is_valid(isbn_number_t number) {
}

sum += digit * pos;

pos -= 1;
}

Expand Down
2 changes: 1 addition & 1 deletion cpp/isbn-verifier/isbn_verifier.plist
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<plist version="1.0">
<dict>
<key>clang_version</key>
<string>Debian clang version 16.0.6 (++20230610113348+7cbf1a259152-1~exp1~20230610233446.99)</string>
<string>Debian clang version 16.0.6 (++20230919124242+7cbf1a259152-1~exp1~20230919124333.105)</string>
<key>diagnostics</key>
<array>
</array>
Expand Down
2 changes: 1 addition & 1 deletion cpp/isbn-verifier/isbn_verifier_test.plist
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<plist version="1.0">
<dict>
<key>clang_version</key>
<string>Debian clang version 16.0.6 (++20230610113348+7cbf1a259152-1~exp1~20230610233446.99)</string>
<string>Debian clang version 16.0.6 (++20230919124242+7cbf1a259152-1~exp1~20230919124333.105)</string>
<key>diagnostics</key>
<array>
</array>
Expand Down
72 changes: 38 additions & 34 deletions cpp/isbn-verifier/run-tests-cpp.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ Running automated test file(s):
Running: make clean
rm -rf ./build

real 0m0.011s
user 0m0.002s
sys 0m0.007s
real 0m0.017s
user 0m0.001s
sys 0m0.016s

===============================================================================

Expand Down Expand Up @@ -55,12 +55,16 @@ make[1]: Leaving directory '/home/vpayno/git_vpayno/exercism-workspace/cpp/isbn-

=== All Tests Passed ===

find . -regextype posix-egrep -regex "^.*(tests-main|CompilerId).*[.](gcda|gcno)$" -print -delete
find . -regextype posix-egrep -regex '^.*(tests-main|CompilerId).*[.](gcda|gcno)$' -print -delete
./build/CMakeFiles/3.22.2/CompilerIdCXX/CMakeCXXCompilerId.gcno
./build/CMakeFiles/isbn-verifier.dir/test/tests-main.cpp.gcno
./build/CMakeFiles/isbn-verifier.dir/test/tests-main.cpp.gcda

find . -regextype posix-egrep -regex "^.*[.](gcda|gcno)9260"
find . -regextype posix-egrep -regex '^.*[.](gcda|gcno)$'
./build/CMakeFiles/isbn-verifier.dir/isbn_verifier_test.cpp.gcno
./build/CMakeFiles/isbn-verifier.dir/isbn_verifier.cpp.gcno
./build/CMakeFiles/isbn-verifier.dir/isbn_verifier.cpp.gcda
./build/CMakeFiles/isbn-verifier.dir/isbn_verifier_test.cpp.gcda

gcovr --print-summary
------------------------------------------------------------------------------
Expand All @@ -69,28 +73,28 @@ Directory: .
------------------------------------------------------------------------------
File Lines Exec Cover Missing
------------------------------------------------------------------------------
isbn_verifier.cpp 29 29 100%
isbn_verifier.cpp 24 24 100%
isbn_verifier_test.cpp 51 51 100%
test/catch.hpp 34 29 85% 1642-1643,2301-2302,2560
------------------------------------------------------------------------------
TOTAL 114 109 95%
TOTAL 109 104 95%
------------------------------------------------------------------------------
lines: 95.6% (109 out of 114)
branches: 41.1% (156 out of 380)
lines: 95.4% (104 out of 109)
branches: 40.3% (149 out of 370)

real 0m12.048s
user 0m11.206s
sys 0m0.836s
real 0m10.677s
user 0m9.866s
sys 0m0.807s

===============================================================================

cmake-format --in-place CMakeLists.txt
WARNING config_util.py:307: The following configuration options were ignored:
max_subargs_per_line

real 0m0.158s
user 0m0.117s
sys 0m0.043s
real 0m0.198s
user 0m0.118s
sys 0m0.081s

===============================================================================

Expand All @@ -110,31 +114,31 @@ found lint:
Convention: 3


real 0m0.173s
user 0m0.139s
sys 0m0.037s
real 0m0.186s
user 0m0.120s
sys 0m0.067s

===============================================================================

Running: clang-format-16 -style=file -i ./isbn_verifier.cpp ./isbn_verifier_test.cpp ./isbn_verifier.hpp

real 0m0.028s
user 0m0.014s
sys 0m0.014s
real 0m0.018s
user 0m0.006s
sys 0m0.012s

===============================================================================

Running: ../../.github/citools/cpp/clang-check
clang-check-16 --analyze ./isbn_verifier.cpp ./isbn_verifier_test.cpp ./isbn_verifier.hpp
Running: clang-check-16 --analyze ./isbn_verifier.cpp ./isbn_verifier_test.cpp ./isbn_verifier.hpp

real 0m3.501s
user 0m3.396s
sys 0m0.105s
real 0m3.189s
user 0m3.085s
sys 0m0.103s


real 0m3.503s
user 0m3.397s
sys 0m0.106s
real 0m3.191s
user 0m3.085s
sys 0m0.105s

===============================================================================

Expand All @@ -145,15 +149,15 @@ Running: ../../.github/citools/cpp/clang-tidy | head -n 100
Suppressed 116244 warnings (116239 in non-user code, 5 NOLINT).
Use -header-filter=.* to display errors from all non-system headers. Use -system-headers to display errors from system headers as well.

real 0m21.584s
user 0m21.439s
sys 0m0.142s
real 0m19.478s
user 0m19.345s
sys 0m0.130s
Running: clang-tidy-16 ./isbn_verifier.cpp ./isbn_verifier_test.cpp ./isbn_verifier.hpp


real 0m21.586s
user 0m21.442s
sys 0m0.142s
real 0m19.480s
user 0m19.346s
sys 0m0.132s

===============================================================================

0 comments on commit b51a849

Please sign in to comment.