1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#[inline]
pub const fn char_to_valid_digit_const(c: u8, radix: u32) -> u32 {
if radix <= 10 {
(c.wrapping_sub(b'0')) as u32
} else {
let digit = match c {
b'0'..=b'9' => c - b'0',
b'A'..=b'Z' => c - b'A' + 10,
b'a'..=b'z' => c - b'a' + 10,
_ => 0xFF,
};
digit as u32
}
}
#[inline]
pub const fn char_to_digit_const(c: u8, radix: u32) -> Option<u32> {
let digit = char_to_valid_digit_const(c, radix);
if digit < radix {
Some(digit)
} else {
None
}
}
#[inline]
pub const fn char_is_digit_const(c: u8, radix: u32) -> bool {
char_to_digit_const(c, radix).is_some()
}
#[inline]
#[cfg(any(feature = "write", feature = "floats"))]
pub const fn digit_to_char_const(digit: u32, radix: u32) -> u8 {
if radix <= 10 || digit < 10 {
digit as u8 + b'0'
} else {
digit as u8 + b'A' - 10
}
}
#[inline]
#[cfg(feature = "parse")]
pub const fn char_to_digit(c: u8, radix: u32) -> Option<u32> {
let digit = match c {
b'0'..=b'9' => c - b'0',
b'A'..=b'Z' => c - b'A' + 10,
b'a'..=b'z' => c - b'a' + 10,
_ => 0xFF,
} as u32;
if digit < radix {
Some(digit)
} else {
None
}
}
#[inline]
#[cfg(feature = "parse")]
pub const fn char_is_digit(c: u8, radix: u32) -> bool {
char_to_digit(c, radix).is_some()
}
#[inline]
#[cfg(feature = "write")]
pub unsafe fn digit_to_char(digit: u32) -> u8 {
const TABLE: [u8; 36] = [
b'0', b'1', b'2', b'3', b'4', b'5', b'6', b'7', b'8', b'9', b'A', b'B', b'C', b'D', b'E',
b'F', b'G', b'H', b'I', b'J', b'K', b'L', b'M', b'N', b'O', b'P', b'Q', b'R', b'S', b'T',
b'U', b'V', b'W', b'X', b'Y', b'Z',
];
debug_assert!(digit < 36, "digit_to_char() invalid character.");
unsafe { *TABLE.get_unchecked(digit as usize) }
}