10int int_arr_max(
int int_arr[],
int size)
13 for (
int i = 0; i < size; i++)
34static inline void num_str_truncate_trailing_zeros(
char* num_str,
int size)
36 while (size > 0 && num_str[size - 1] ==
'0')
61static inline void truncate_num_get_remainder_string(
62 uint32_t decimal_remainder,
63 uint32_t truncated_num,
66 char remainder_str[UINT_MAX_DIGITS + 1]
70 char* remainder_str_format;
76 remainder_str_format =
"%09lu";
79 remainder_str_format =
"%06lu";
82 remainder_str_format =
"%03lu";
86 remainder_str_format =
"%lu";
89 snprintf(remainder_str, UINT_MAX_DIGITS + 1, remainder_str_format, decimal_remainder);
92 int remaining_chars = num_req_chars -
u32_get_digits(truncated_num) - 1;
95 if (remaining_chars <= 0)
97 remainder_str[0] =
'\0';
102 if (remaining_chars > UINT_MAX_DIGITS)
104 remaining_chars = UINT_MAX_DIGITS;
106 remainder_str[remaining_chars] =
'\0';
108 num_str_truncate_trailing_zeros(remainder_str, remaining_chars);
110 if (remainder_str[0] !=
'\0')
119 char out_str_buff[UINT_MAX_DIGITS + 1]
122 uint32_t truncated_num = num;
124 uint32_t decimal_remainder = 0;
125 bool overflow = num_digits > num_req_chars;
127 char remainder_str[UINT_MAX_DIGITS + 1];
128 remainder_str[0] =
'\0';
137 uint32_t divisor = 1;
143 else if (num >= ONE_M)
148 else if (num >= ONE_K)
155 truncated_num = num / divisor;
156 decimal_remainder = num % divisor;
159 if (suffix[0] !=
'\0' && decimal_remainder != 0)
161 truncate_num_get_remainder_string(
170 snprintf(out_str_buff, UINT_MAX_DIGITS + 1,
"%lu%s%s", truncated_num, remainder_str, suffix);
177 return (a > (UINT32_MAX - b)) ? UINT32_MAX : (a + b);
182 return (a > (UINT16_MAX - b)) ? UINT16_MAX : (a + b);
187 return (a == 0 || b == 0) ? 0 : (a > (UINT32_MAX / b) ? UINT32_MAX : a * b);
192 return (a == 0 || b == 0) ? 0 : (a > (UINT16_MAX / b) ? UINT16_MAX : a * b);
204static inline uint32_t base36_digit_value(
char c)
228static inline char base36_digit_char(uint32_t n)
249static inline uint32_t get_base36_power(uint8_t i)
251 if (i >= BASE36_MAX_DIGITS)
256 static const uint32_t powers_of_36[BASE36_MAX_DIGITS] = {1, 36, 1296, 46656, 1679616, 60466176};
257 return powers_of_36[i];
264 for (uint8_t i = 0; i < BASE36_MAX_DIGITS; i++)
266 res += base36_digit_value(b36_str[i]) * get_base36_power(BASE36_MAX_DIGITS - i - 1);
276 for (
int i = 0; i < BASE36_MAX_DIGITS; i++)
278 power = get_base36_power(BASE36_MAX_DIGITS - i - 1);
279 b36_str[i] = base36_digit_char(acc / power);
283 b36_str[BASE36_MAX_DIGITS] =
'\0';
Utility file to interact with custom font.
char digit_char_to_font_point(char digit_char)
Get the decimal point char equivalent of 0-9 from char param.
Utilities relating around number string representation and protected arithmatic helper functions.
uint16_t u16_protected_mult(uint16_t a, uint16_t b)
Avoid overflow when multiplying two u16 integers.
uint32_t u32_protected_add(uint32_t a, uint32_t b)
Avoid overflow when adding two u32 integers.
#define MAX_BASE36
Hex value of "ZZZZZZ" in base 36.
static int u32_get_digits(uint32_t n)
Get the number of digits in a 32-bit unsigned number https://stackoverflow.com/questions/1068849/how-...
uint32_t base36_to_u32(const char b36_str[])
Convert a base-36 string representation to a 32-bit unsigned integer. Since we are dealing with base-...
uint16_t u16_protected_add(uint16_t a, uint16_t b)
Avoid overflow when adding two u16 integers.
void truncate_uint_to_suffixed_str(uint32_t num, int num_req_chars, char out_str_buff[UINT_MAX_DIGITS+1])
Truncate an unsigned number into a suffixed string representation e.g. 12000 -> "12K" The least signi...
uint32_t u32_protected_mult(uint32_t a, uint32_t b)
Avoid overflow when multiplying two u32 integers.
void u32_to_base36(uint32_t n, char b36_str[])
Convert a 32-bit unsigned integer to its base-36 string representation. This will perform 6 divisions...