summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlal@nekernel.org>2025-08-03 18:54:30 +0100
committerAmlal El Mahrouss <amlal@nekernel.org>2025-08-03 18:58:58 +0100
commit4b4d6299d28467b9c234e40004bae1d40b37b0dc (patch)
treea1d8b86f2a1a747200f8c97a43c46704b9869241
parentd05275f76cf96d577d51ee937ba3aac3be8b2d03 (diff)
feat! rework crc32 module, and refactored other modules for future usages.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
-rw-r--r--dev/examples/opt/opt.cc1
-rw-r--r--dev/lib/fix/parser.hpp12
-rw-r--r--dev/lib/utility/crc32.hpp27
3 files changed, 24 insertions, 16 deletions
diff --git a/dev/examples/opt/opt.cc b/dev/examples/opt/opt.cc
index d1324e7..c1c2745 100644
--- a/dev/examples/opt/opt.cc
+++ b/dev/examples/opt/opt.cc
@@ -6,6 +6,7 @@
#include <lib/logic/opt.hpp>
#include <lib/io/print.hpp>
+#include <lib/utility/crc32.hpp>
#include <string>
static const char do_hash(const std::string& in)
diff --git a/dev/lib/fix/parser.hpp b/dev/lib/fix/parser.hpp
index f2509c7..a195eb3 100644
--- a/dev/lib/fix/parser.hpp
+++ b/dev/lib/fix/parser.hpp
@@ -30,12 +30,12 @@ namespace snu::fix
struct basic_range_data;
/// @brief Buffer+Length structure
- template <typename char_type>
+ template <typename char_type = char>
using range_ptr_t = basic_range<char_type>;
namespace detail
{
- template <typename char_type>
+ template <typename char_type = char>
const char_type* begin_fix();
template <>
@@ -57,7 +57,7 @@ namespace snu::fix
}
} // namespace detail
- template <typename char_type>
+ template <typename char_type = char>
struct basic_range final
{
char_type* bytes_;
@@ -76,7 +76,7 @@ namespace snu::fix
/// @brief Convert basic_range to usable string.
/// @note This function assumes that the basic_range is valid and contains ASCII bytes.
- template <typename char_type>
+ template <typename char_type = char>
inline std::basic_string<char_type> to_string(basic_range<char_type>& basic_range) noexcept
{
if (basic_range.length_ < 0)
@@ -86,7 +86,7 @@ namespace snu::fix
}
/// @brief a basic_range object containing the FIX packet values.
- template <typename char_type>
+ template <typename char_type = char>
class basic_range_data final
{
public:
@@ -133,7 +133,7 @@ namespace snu::fix
};
/// @brief basic_visitor object which returns a fix::basic_range_data instance.
- template <typename char_type>
+ template <typename char_type = char>
class basic_visitor final
{
public:
diff --git a/dev/lib/utility/crc32.hpp b/dev/lib/utility/crc32.hpp
index de78d4e..fe9f770 100644
--- a/dev/lib/utility/crc32.hpp
+++ b/dev/lib/utility/crc32.hpp
@@ -1,6 +1,6 @@
/*
- * File: embfs.hpp
- * Purpose: Embedded File System.
+ * File: crc32.hpp
+ * Purpose: CRC32 module.
* Author: Amlal El Mahrouss,
* Copyright 2025, Amlal El Mahrouss all rights reserved.
*/
@@ -9,6 +9,7 @@
#define _SNU_CRC32_HPP
#include <cstdint>
+#include <string>
#include <cstddef>
/// @brief Crc32 implementation in C++
@@ -16,11 +17,11 @@
namespace snu::crc32
{
- namespace details
+ namespace detail
{
- inline constexpr const std::uint16_t g_crc_sz = 256;
+ inline constexpr const std::uint16_t crc_sz_ = 256U;
- inline std::uint32_t g_crc_table[g_crc_sz] = {
+ inline std::uint32_t crc_array_[crc_sz_] = {
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419, 0x706af48f, 0xe963a535, 0x9e6495a3,
0x0edb8832, 0x79dcb8a4, 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07, 0x90bf1d91,
0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de, 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7,
@@ -53,22 +54,28 @@ namespace snu::crc32
0xaed16a4a, 0xd9d65adc, 0x40df0b66, 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605, 0xcdd70693, 0x54de5729, 0x23d967bf,
0xb3667a2e, 0xc4614ab8, 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b, 0x2d02ef8d};
- ///
+ template <typename char_type>
inline std::uint32_t
- calculate_crc32_(const char* in, size_t len) noexcept
+ crc32(const char_type* in, size_t len) noexcept
{
if (!in || *in == 0)
- return ~0;
+ return 0;
std::uint32_t crc = 0xffffffff;
while ((len--) > 0)
- crc = (crc >> 8) ^ g_crc_table[(crc ^ *(in++)) & 0xFF];
+ crc = (crc >> 8) ^ crc_array_[(crc ^ *(in++)) & 0xFF];
return ~crc;
}
- } // namespace details
+ } // namespace detail
+
+ template <typename char_type = char>
+ inline std::uint32_t hash(const std::basic_string<char_type>& in)
+ {
+ return detail::crc32(in.c_str(), in.size());
+ }
} // namespace snu::crc32
#endif // !_SNU_CRC32_HPP \ No newline at end of file