summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2026-01-01 17:47:57 +0100
committerAmlal El Mahrouss <amlal@nekernel.org>2026-01-01 17:47:57 +0100
commit7ea24be51ef95a864d4e3dde7395780244a69f15 (patch)
treed97a424905f8326374ae02d0c6b0313d7fdf17f8
parent665e433697247c4a43e055830dee7a72afdb810f (diff)
chore: improve checksum API, making checksum constexpr. Update example.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
-rw-r--r--example/fix_tag_example/example.cpp22
-rw-r--r--include/ocl/fix/checksum.hpp10
2 files changed, 24 insertions, 8 deletions
diff --git a/example/fix_tag_example/example.cpp b/example/fix_tag_example/example.cpp
index ac4f479..ba788de 100644
--- a/example/fix_tag_example/example.cpp
+++ b/example/fix_tag_example/example.cpp
@@ -3,7 +3,7 @@
constexpr char default_fix[] = {
'8', '=', 'F', 'I', 'X', '.', '4', '.', '2', 0x01,
- '9', '=', '6', '3', 0x01, // BodyLength = 63
+ '9', '=', '8', '7', 0x01, // BodyLength = 87
'3', '5', '=', 'A', 0x01,
'4', '9', '=', 'S', 'E', 'R', 'V', 'E', 'R', 0x01,
'5', '6', '=', 'C', 'L', 'I', 'E', 'N', 'T', 0x01,
@@ -11,9 +11,23 @@ constexpr char default_fix[] = {
'5', '2', '=', '2', '0', '0', '9', '0', '1', '0', '7', '-', '1', '8', ':', '1', '5', ':', '1', '6', 0x01,
'9', '8', '=', '0', 0x01,
'1', '0', '8', '=', '3', '0', 0x01,
- '1', '0', '=', '1', '4', '3', 0x01, 0x00 // CheckSum = 143
+ '1', '0', '=', '6', '0', 0x01, 0x00 // CheckSum = 143
};
+constexpr char default_fix_unchecked[] = {
+ '8', '=', 'F', 'I', 'X', '.', '4', '.', '2', 0x01,
+ '9', '=', '8', '0', 0x01, // BodyLength = 80
+ '3', '5', '=', 'A', 0x01,
+ '4', '9', '=', 'S', 'E', 'R', 'V', 'E', 'R', 0x01,
+ '5', '6', '=', 'C', 'L', 'I', 'E', 'N', 'T', 0x01,
+ '3', '4', '=', '1', '7', '7', 0x01,
+ '5', '2', '=', '2', '0', '0', '9', '0', '1', '0', '7', '-', '1', '8', ':', '1', '5', ':', '1', '6', 0x01,
+ '9', '8', '=', '0', 0x01,
+ '1', '0', '8', '=', '3', '0', 0x01,
+ 0x00
+};
+
+
int main(int argc, char** argv)
{
ocl::fix::visitor basic_visitor;
@@ -27,7 +41,9 @@ int main(int argc, char** argv)
ocl::io::print(":key=49\n");
ocl::io::print(":value=", fix["49"], "\n");
- ocl::io::print("checksum=", ocl::fix::try_index_checksum(fix), "\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");
return 0;
}
diff --git a/include/ocl/fix/checksum.hpp b/include/ocl/fix/checksum.hpp
index 31aee75..451e387 100644
--- a/include/ocl/fix/checksum.hpp
+++ b/include/ocl/fix/checksum.hpp
@@ -35,18 +35,18 @@ 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 checksum_type
+ inline constexpr checksum_type
checksum(const char* in_,
- const long len)
+ const std::size_t len)
{
if (len < 1)
return 0L;
- long long cks{};
+ checksum_type cks{};
- for (long idx{}; idx < len; ++idx)
+ for (std::size_t idx{}; idx < len; ++idx)
{
- cks += static_cast<unsigned char>(in_[idx]);
+ cks += static_cast<uint8_t>(in_[idx]);
}
return cks % 256;