cstring.c 814 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #include "toolbox/cstring.h"
  2. #include "toolbox/log.h"
  3. #include <stdbool.h>
  4. #include <stddef.h>
  5. #include <stdlib.h>
  6. #define TEST_FOR_NULL(POINTER) \
  7. if ( POINTER == NULL ) \
  8. { \
  9. LOG_ERROR("Null Pointer Not Expected"); \
  10. exit(EXIT_FAILURE); \
  11. }
  12. size_t
  13. cstring_len(const char *p_cstr) {
  14. TEST_FOR_NULL(p_cstr);
  15. size_t len = 0;
  16. while ( *(p_cstr + len++) ) {}
  17. return --len;
  18. }
  19. bool
  20. cstring_equal(const char *restrict p_cstr1, const char *restrict p_cstr2) {
  21. const size_t cstr1_size = cstring_len(p_cstr1);
  22. const size_t cstr2_size = cstring_len(p_cstr2);
  23. if ( cstr1_size != cstr2_size ) {
  24. return false;
  25. }
  26. for ( size_t i = 0; i < cstr1_size; ++i ) {
  27. if ( p_cstr1[i] != p_cstr2[i] ) {
  28. return false;
  29. }
  30. }
  31. return true;
  32. }
  33. #undef TEST_FOR_NULL