Prusa MINI Firmware overview
int_to_cstr.h
Go to the documentation of this file.
1 // convert int32_t to c string
2 #ifndef _INT_TO_CSTR_H
3 #define _INT_TO_CSTR_H
4 #include "stdint.h"
5 #include "stdlib.h"
6 
7 /*****************************************************************************/
8 //intended api
9 extern constexpr bool is_negative(int32_t i);
10 extern constexpr size_t count_digits(int32_t i);
11 extern constexpr char nth_char(int32_t i, size_t n);
12 extern constexpr int pow_int(int val, size_t pow);
13 extern constexpr int remove_front_digit(int32_t i);
14 /*****************************************************************************/
15 
16 //0 == 0 digits
17 static constexpr size_t _count_digits_unsigned(uint32_t ui) {
18  return ui > 0 ? _count_digits_unsigned(ui / 10) + 1 : 0;
19 }
20 
21 //0 == 1 digits
22 static constexpr size_t count_digits_unsigned(uint32_t ui) {
23  return ui == 0 ? 1 : _count_digits_unsigned(ui);
24 }
25 
26 constexpr bool is_negative(int32_t i) {
27  return i < 0;
28 }
29 
30 constexpr size_t count_digits(int32_t i) {
32 }
33 
34 //n must be valid
35 static constexpr char valid_nth_char_unsigned(uint32_t ui, size_t n) {
36  return n == count_digits_unsigned(ui) - 1 ? (ui % 10) + '0' : valid_nth_char_unsigned(ui / 10, n);
37 }
38 
39 //return '/0' if n is too big
40 static constexpr char nth_char_unsigned(uint32_t ui, size_t n) {
41  return n >= count_digits_unsigned(ui) ? 0 : valid_nth_char_unsigned(ui, n);
42 }
43 
44 //returns nth char of -ui
45 static constexpr char nth_char_negative(int32_t i, size_t n) {
46  return n == 0 ? '-' : nth_char_unsigned(-i, n - 1);
47 }
48 
49 constexpr char nth_char(int32_t i, size_t n) {
50  return i < 0 ? nth_char_negative(i, n) : nth_char_unsigned(i, n);
51 }
52 
53 constexpr int pow_int(int val, size_t pow) {
54  return pow == 0 ? 1 : val * pow_int(val, pow - 1);
55 }
56 
57 static constexpr int remove_front_digit_unsigned(uint32_t ui) {
58  return ui % pow_int(10, count_digits_unsigned(ui));
59 }
60 
61 constexpr int remove_front_digit(int32_t i) {
62  return i < 0 ? -1 : remove_front_digit_unsigned(i);
63 }
64 
65 #endif //_INT_TO_CSTR_H
i
uint8_t i
Definition: screen_test_graph.c:72
nth_char_unsigned
static constexpr char nth_char_unsigned(uint32_t ui, size_t n)
Definition: int_to_cstr.h:40
count_digits_unsigned
static constexpr size_t count_digits_unsigned(uint32_t ui)
Definition: int_to_cstr.h:22
remove_front_digit
constexpr int remove_front_digit(int32_t i)
Definition: int_to_cstr.h:61
pow_int
constexpr int pow_int(int val, size_t pow)
Definition: int_to_cstr.h:53
_count_digits_unsigned
static constexpr size_t _count_digits_unsigned(uint32_t ui)
Definition: int_to_cstr.h:17
is_negative
constexpr bool is_negative(int32_t i)
Definition: int_to_cstr.h:26
remove_front_digit_unsigned
static constexpr int remove_front_digit_unsigned(uint32_t ui)
Definition: int_to_cstr.h:57
ui
MarlinUI ui
nth_char_negative
static constexpr char nth_char_negative(int32_t i, size_t n)
Definition: int_to_cstr.h:45
nth_char
constexpr char nth_char(int32_t i, size_t n)
Definition: int_to_cstr.h:49
count_digits
constexpr size_t count_digits(int32_t i)
Definition: int_to_cstr.h:30
valid_nth_char_unsigned
static constexpr char valid_nth_char_unsigned(uint32_t ui, size_t n)
Definition: int_to_cstr.h:35