37 #ifndef EMBEDDED_STRING_
38 #define EMBEDDED_STRING_
59 #define DEBUG_VARS_PROTOTYPES()
66 #define DPRINT_NOTAB( ... )
68 #define DRETURN( ... )
69 #define DENTER_ARG( ... )
70 #define DRETURN_ARG( ... )
238 static uint8_t
num_to_str( uint8_t num, uint8_t str_len,
char *str )
246 if ((
SAFETY_CHECKS ==
true) && ((str ==
nullptr) || (str_len == 0)))
257 const uint8_t base[Config::DIGIT8] =
275 for (t = 0;t < Config::DIGIT8; t++)
283 if ( (
SAFETY_CHECKS ==
true) && (index >= (str_len -Config::STRING_TERMINATOR_SIZE)) )
286 str[0] = Config::TERMINATOR;
288 DRETURN_ARG(
"ERR: Not enough space: %d\n", str_len);
292 str[ index ] =
'0' +tmp;
294 num = num - base[t] * tmp;
301 else if ( (flag ==
true) && (t != (Config::DIGIT8 -1)) )
309 if ( (
SAFETY_CHECKS ==
true) && (index >= (str_len -Config::STRING_TERMINATOR_SIZE)) )
312 str[0] = Config::TERMINATOR;
314 DRETURN_ARG(
"ERR: Not enough space: %d\n", str_len);
329 str[ index ] = Config::TERMINATOR;
330 DRETURN_ARG(
"digits written: %d | output >%s<\n", index, str);
346 static uint8_t
num_to_str( int8_t num, uint8_t str_len,
char *str )
362 if ((
SAFETY_CHECKS ==
true) && ((str ==
nullptr) || (str_len == 0)))
390 ret =
num_to_str( (uint8_t)positive, str_len -Config::STRING_SIGN_SIZE, &str[Config::STRING_SIGN_SIZE] );
403 DRETURN_ARG(
"digits written: %d | output >%s<\n", ret +Config::STRING_SIGN_SIZE, str);
404 return (ret +Config::STRING_SIGN_SIZE);
419 static uint8_t
num_to_str( uint16_t num, uint8_t str_len,
char *str )
427 if ((
SAFETY_CHECKS ==
true) && ((str ==
nullptr) || (str_len == 0)))
433 if (num <= UINT8_MAX)
436 uint8_t ret =
num_to_str( (uint8_t)num, str_len, str );
446 const uint16_t base[Config::DIGIT16] =
466 for (t = 0;t < Config::DIGIT16; t++)
474 if ( (
SAFETY_CHECKS ==
true) && (index >= (str_len -Config::STRING_TERMINATOR_SIZE)) )
477 str[0] = Config::TERMINATOR;
479 DRETURN_ARG(
"ERR: Not enough space: %d\n", str_len);
483 str[ index ] =
'0' +tmp;
485 num = num - base[t] * tmp;
492 else if ( (flag ==
true) && (t != (Config::DIGIT16 -1)) )
500 if ( (
SAFETY_CHECKS ==
true) && (index >= (str_len -Config::STRING_TERMINATOR_SIZE)) )
503 str[0] = Config::TERMINATOR;
505 DRETURN_ARG(
"ERR: Not enough space: %d\n", str_len);
520 str[ index ] = Config::TERMINATOR;
521 DRETURN_ARG(
"digits written: %d | output >%s<\n", index, str);
538 static uint8_t
num_to_str( int16_t num, uint8_t str_len,
char *str )
554 if ((
SAFETY_CHECKS ==
true) && ((str ==
nullptr) || (str_len == 0)))
583 if (positive < UINT8_MAX)
586 ret =
num_to_str( (uint8_t)positive, str_len -Config::STRING_SIGN_SIZE, &str[Config::STRING_SIGN_SIZE] );
591 ret =
num_to_str( (uint16_t)positive, str_len -Config::STRING_SIGN_SIZE, &str[Config::STRING_SIGN_SIZE] );
605 DRETURN_ARG(
"digits written: %d | output >%s<\n", ret +Config::STRING_SIGN_SIZE, str);
606 return (ret +Config::STRING_SIGN_SIZE);
621 static uint8_t
num_to_str( uint32_t num, uint8_t str_len,
char *str )
629 if ((
SAFETY_CHECKS ==
true) && ((str ==
nullptr) || (str_len == 0)))
635 if (num <= UINT8_MAX)
638 uint8_t ret =
num_to_str( (uint8_t)num, str_len, str );
643 else if (num <= UINT16_MAX)
646 uint8_t ret =
num_to_str( (uint16_t)num, str_len, str );
656 const uint32_t base[Config::DIGIT32] =
681 for (t = 0;t < Config::DIGIT32; t++)
689 if ( (
SAFETY_CHECKS ==
true) && (index >= (str_len -Config::STRING_TERMINATOR_SIZE)) )
692 str[0] = Config::TERMINATOR;
694 DRETURN_ARG(
"ERR: Not enough space: %d\n", str_len);
698 str[ index ] =
'0' +tmp;
700 num = num - base[t] * tmp;
707 else if ( (flag ==
true) && (t != (Config::DIGIT32 -1)) )
715 if ( (
SAFETY_CHECKS ==
true) && (index >= (str_len -Config::STRING_TERMINATOR_SIZE)) )
718 str[0] = Config::TERMINATOR;
720 DRETURN_ARG(
"ERR: Not enough space: %d\n", str_len);
735 str[ index ] = Config::TERMINATOR;
736 DRETURN_ARG(
"digits written: %d | output >%s<\n", index, str);
752 static uint8_t
num_to_str( int32_t num, uint8_t str_len,
char *str )
768 if ((
SAFETY_CHECKS ==
true) && ((str ==
nullptr) || (str_len == 0)))
797 if (positive < UINT8_MAX)
800 ret =
num_to_str( (uint8_t)positive, str_len -Config::STRING_SIGN_SIZE, &str[Config::STRING_SIGN_SIZE] );
803 else if (positive < UINT16_MAX)
806 ret =
num_to_str( (uint16_t)positive, str_len -Config::STRING_SIGN_SIZE, &str[Config::STRING_SIGN_SIZE] );
812 ret =
num_to_str( (uint32_t)positive, str_len -Config::STRING_SIGN_SIZE, &str[Config::STRING_SIGN_SIZE] );
826 DRETURN_ARG(
"digits written: %d | output >%s<\n", ret +Config::STRING_SIGN_SIZE, str);
827 return (ret +Config::STRING_SIGN_SIZE);
852 static uint8_t
num_to_eng( uint32_t num, int8_t num_exp, uint8_t str_len,
char *str )
854 DENTER_ARG(
"U32 num: %zu | base exponent 10^%d\n", num, num_exp);
859 if ((Config::SAFETY_CHECKS ==
true) && ((str ==
nullptr) || (str_len < Config::STRING_SIZE_UENG)) )
874 str[6] = Config::TERMINATOR;
875 return Config::STRING_SIZE_UENG;
886 bool f_continue =
true;
888 while (f_continue ==
true)
899 else if (num >= 10000)
902 int16_t num_tmp = num / 10;
927 shift = shift +num_exp;
931 uint8_t si_suffix_index;
934 point = (3- ((-shift-1) %3));
935 si_suffix_index = ((shift+1)/3) +6;
939 point = 1+ ((shift) %3);
940 si_suffix_index = (shift+3)/3 +6;
943 const char *suffix =
"afpnum KMGTPE";
945 str[5] = suffix[ si_suffix_index ];
953 const uint16_t base[Config::DIGIT_ENG] =
964 for (uint8_t t = 0;t < Config::DIGIT_ENG; t++)
972 str[ index ] =
'0' +tmp;
974 num = num - base[t] * tmp;
1005 str[index] = Config::TERMINATOR;
1010 DRETURN_ARG(
"digits written: %d | output >%s<\n", index, str);
1027 static uint8_t
num_to_eng( int32_t num, int8_t num_exp, uint8_t str_len,
char *str )
1029 DENTER_ARG(
"S32 num: %ld | base exponent 10^%d\n", (int32_t)num, num_exp);
1043 if ((
SAFETY_CHECKS ==
true) && ((str ==
nullptr) || (str_len == 0)))
1071 ret =
num_to_eng( (uint32_t)positive, num_exp, str_len -Config::STRING_SIGN_SIZE, &str[Config::STRING_SIGN_SIZE] );
1084 DRETURN_ARG(
"digits written: %d | output >%s<\n", ret +Config::STRING_SIGN_SIZE, str);
1085 return (ret +Config::STRING_SIGN_SIZE);
1100 static inline uint8_t
num_to_eng( uint32_t num, uint8_t str_len,
char *str )
1109 uint8_t ret =
num_to_eng( num, 0, str_len, str );
1114 DRETURN_ARG(
"digits written: %d | output >%s<\n", ret, str);
1130 static inline uint8_t
num_to_eng( int32_t num, uint8_t str_len,
char *str )
1139 uint8_t ret =
num_to_eng( num, 0, str_len, str );
1144 DRETURN_ARG(
"digits written: %d | output >%s<\n", ret, str);
1170 #warning "Multiple inclusion of hader file EMBEDDED_STRING_"