2019-09-13 20:06:22 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2019 Oticon A/S
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2022-05-06 17:23:05 +08:00
|
|
|
#include <zephyr/sys/util.h>
|
2019-09-13 20:06:22 +08:00
|
|
|
|
2020-05-28 00:26:57 +08:00
|
|
|
uint8_t u8_to_dec(char *buf, uint8_t buflen, uint8_t value)
|
2019-09-13 20:06:22 +08:00
|
|
|
{
|
2020-05-28 00:26:57 +08:00
|
|
|
uint8_t divisor = 100;
|
|
|
|
uint8_t num_digits = 0;
|
|
|
|
uint8_t digit;
|
2019-09-13 20:06:22 +08:00
|
|
|
|
lib: os: dec: add misra-c2012 compliance changes
1. change explicit type cast of essential character type, complying with
required [misra-c2012-10.2] rule which states; Expressions of
essentially character type shall not be used inappropriately in addition
and subtraction operations, and
2. add explicit boolean type to 'if' statement controlling expression,
consolidating it with 'buflen' type, thus improving code readability and
maintainability , complying with required [misra-c2012-14.4] rule which
states; ; The controlling expression of an if statement and the
controlling expression of an iteration-statement shall have essentially
boolean type, and
3. add enclosing parentheses enforcing and clarifying precedence of
operators, improving code readability and maintainability, complying
with *advisory* [misra-c2012-12.1] rule which states; The precedence of
operators within expressions should be made explicit.
Found as a coding guideline violation (Rules 10.2, 14.4), and coding
guideline recommendation (Rule 12.1) by static code scanning tool.
Note: Tested on STM32L5 Nucleo-144 board (stm32l552xx).
Signed-off-by: ferar alashkar <ferar.alashkar@gmail.com>
2023-06-11 04:53:19 +08:00
|
|
|
while ((buflen > 0) && (divisor > 0)) {
|
2019-09-13 20:06:22 +08:00
|
|
|
digit = value / divisor;
|
lib: os: dec: add misra-c2012 compliance changes
1. change explicit type cast of essential character type, complying with
required [misra-c2012-10.2] rule which states; Expressions of
essentially character type shall not be used inappropriately in addition
and subtraction operations, and
2. add explicit boolean type to 'if' statement controlling expression,
consolidating it with 'buflen' type, thus improving code readability and
maintainability , complying with required [misra-c2012-14.4] rule which
states; ; The controlling expression of an if statement and the
controlling expression of an iteration-statement shall have essentially
boolean type, and
3. add enclosing parentheses enforcing and clarifying precedence of
operators, improving code readability and maintainability, complying
with *advisory* [misra-c2012-12.1] rule which states; The precedence of
operators within expressions should be made explicit.
Found as a coding guideline violation (Rules 10.2, 14.4), and coding
guideline recommendation (Rule 12.1) by static code scanning tool.
Note: Tested on STM32L5 Nucleo-144 board (stm32l552xx).
Signed-off-by: ferar alashkar <ferar.alashkar@gmail.com>
2023-06-11 04:53:19 +08:00
|
|
|
if ((digit != 0) || (divisor == 1) || (num_digits != 0)) {
|
|
|
|
*buf = digit + (char)'0';
|
2019-09-13 20:06:22 +08:00
|
|
|
buf++;
|
|
|
|
buflen--;
|
|
|
|
num_digits++;
|
|
|
|
}
|
|
|
|
|
|
|
|
value -= digit * divisor;
|
|
|
|
divisor /= 10;
|
|
|
|
}
|
|
|
|
|
lib: os: dec: add misra-c2012 compliance changes
1. change explicit type cast of essential character type, complying with
required [misra-c2012-10.2] rule which states; Expressions of
essentially character type shall not be used inappropriately in addition
and subtraction operations, and
2. add explicit boolean type to 'if' statement controlling expression,
consolidating it with 'buflen' type, thus improving code readability and
maintainability , complying with required [misra-c2012-14.4] rule which
states; ; The controlling expression of an if statement and the
controlling expression of an iteration-statement shall have essentially
boolean type, and
3. add enclosing parentheses enforcing and clarifying precedence of
operators, improving code readability and maintainability, complying
with *advisory* [misra-c2012-12.1] rule which states; The precedence of
operators within expressions should be made explicit.
Found as a coding guideline violation (Rules 10.2, 14.4), and coding
guideline recommendation (Rule 12.1) by static code scanning tool.
Note: Tested on STM32L5 Nucleo-144 board (stm32l552xx).
Signed-off-by: ferar alashkar <ferar.alashkar@gmail.com>
2023-06-11 04:53:19 +08:00
|
|
|
if (buflen != 0) {
|
2019-09-13 20:06:22 +08:00
|
|
|
*buf = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
return num_digits;
|
|
|
|
}
|