diff --git a/nac3core/irrt/irrt/utils.hpp b/nac3core/irrt/irrt/utils.hpp index da175167..0c6b7c0b 100644 --- a/nac3core/irrt/irrt/utils.hpp +++ b/nac3core/irrt/irrt/utils.hpp @@ -19,4 +19,59 @@ bool arrays_match(int len, T* as, T* bs) { } return true; } + +namespace cstr_utils { +bool is_empty(const char* str) { + return str[0] == '\0'; +} + +int8_t compare(const char* a, const char* b) { + uint32_t i = 0; + while (true) { + if (a[i] < b[i]) { + return -1; + } else if (a[i] > b[i]) { + return 1; + } else { // a[i] == b[i] + if (a[i] == '\0') { + return 0; + } else { + i++; + } + } + } +} + +int8_t equal(const char* a, const char* b) { + return compare(a, b) == 0; +} + +uint32_t length(const char* str) { + uint32_t length = 0; + while (*str != '\0') { + length++; + str++; + } + return length; +} + +bool copy(const char* src, char* dst, uint32_t dst_max_size) { + for (uint32_t i = 0; i < dst_max_size; i++) { + bool is_last = i + 1 == dst_max_size; + if (is_last && src[i] != '\0') { + dst[i] = '\0'; + return false; + } + + if (src[i] == '\0') { + dst[i] = '\0'; + return true; + } + + dst[i] = src[i]; + } + + __builtin_unreachable(); +} +} // namespace cstr_utils } // namespace