summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--example/fix_tag_example/example.cpp5
-rw-r--r--include/ocl/fix/checksum.hpp22
2 files changed, 12 insertions, 15 deletions
diff --git a/example/fix_tag_example/example.cpp b/example/fix_tag_example/example.cpp
index ba788de..cb758c2 100644
--- a/example/fix_tag_example/example.cpp
+++ b/example/fix_tag_example/example.cpp
@@ -1,7 +1,7 @@
#include <ocl/fix/parser.hpp>
#include <ocl/fix/checksum.hpp>
-constexpr char default_fix[] = {
+constexpr char const default_fix[] = {
'8', '=', 'F', 'I', 'X', '.', '4', '.', '2', 0x01,
'9', '=', '8', '7', 0x01, // BodyLength = 87
'3', '5', '=', 'A', 0x01,
@@ -42,8 +42,7 @@ int main(int argc, char** argv)
ocl::io::print(":value=", fix["49"], "\n");
ocl::io::print(":checksum=", ocl::fix::try_index_checksum(fix), "\n");
- ocl::io::print(":checksum=", ocl::fix::operators::fix::checksum(default_fix_unchecked,
- sizeof(default_fix_unchecked)) + 1, "\n");
+ ocl::io::print(":checksum=", ocl::fix::operators::checksum(default_fix_unchecked), "\n");
return 0;
}
diff --git a/include/ocl/fix/checksum.hpp b/include/ocl/fix/checksum.hpp
index 451e387..6492acf 100644
--- a/include/ocl/fix/checksum.hpp
+++ b/include/ocl/fix/checksum.hpp
@@ -27,7 +27,7 @@ namespace ocl::fix
}
/// \brief FIX message operators namespace.
- namespace operators::fix
+ namespace operators
{
using checksum_type = long long;
@@ -35,23 +35,21 @@ namespace ocl::fix
/// \param in_ Pointer to the message buffer.
/// \param len Length of the message in bytes.
/// \return The checksum value (sum of all bytes modulo 256).
- inline constexpr checksum_type
- checksum(const char* in_,
- const std::size_t len)
+ constexpr inline checksum_type
+ checksum(const std::string_view& in_) noexcept
{
- if (len < 1)
- return 0L;
-
checksum_type cks{};
- for (std::size_t idx{}; idx < len; ++idx)
- {
- cks += static_cast<uint8_t>(in_[idx]);
- }
+ for (std::size_t idx{};
+ idx < in_.size(); ++idx)
+ cks += static_cast<uint8_t>(in_[idx]);
+
+ // add \0
+ cks += 1;
return cks % 256;
}
- } // namespace operators::fix
+ } // namespace operators
} // namespace ocl::fix