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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
use core::ascii::EscapeDefault;
use super::{
ascii::{Char, Digit, Graphic, Lowercase, NonNul, Uppercase},
*,
};
impl Uppercase {
/// Convert to ASCII lowercase.
///
/// ```rust
/// # use ranch::ascii::{Lowercase, Uppercase};
/// let uppercase_a = Uppercase::new::<65>();
///
/// assert_eq!(Lowercase::new::<97>(), uppercase_a.to_ascii_lowercase());
/// ```
pub const fn to_ascii_lowercase(self) -> Lowercase {
RangedNonZeroU8::from_ranged(RangedU8(self.get().to_ascii_lowercase()))
}
/// Convert to [`char`].
///
/// ```rust
/// # use ranch::ascii::Uppercase;
/// let uppercase_a = Uppercase::new::<65>();
///
/// assert_eq!(uppercase_a.to_char(), 'A');
/// ```
pub const fn to_char(self) -> char {
let chr: Char = self.to_ranged().to_ranged_u8();
chr.to_char()
}
}
impl Lowercase {
/// Convert to ASCII uppercase.
///
/// ```rust
/// # use ranch::ascii::{Lowercase, Uppercase};
/// let lowercase_a = Lowercase::new::<97>();
///
/// assert_eq!(Uppercase::new::<65>(), lowercase_a.to_ascii_uppercase());
/// ```
pub const fn to_ascii_uppercase(self) -> Uppercase {
RangedNonZeroU8::from_ranged(RangedU8(self.get().to_ascii_uppercase()))
}
/// Convert to [`char`].
///
/// ```rust
/// # use ranch::ascii::Lowercase;
/// let lowercase_a = Lowercase::new::<97>();
///
/// assert_eq!(lowercase_a.to_char(), 'a');
/// ```
pub const fn to_char(self) -> char {
let chr: Char = self.to_ranged().to_ranged_u8();
chr.to_char()
}
}
impl Graphic {
/// Convert to ASCII uppercase.
///
/// ASCII letters ‘a’ to ‘z’ are mapped to ‘A’ to ‘Z’, but non-alphabetic
/// letters are unchanged.
///
/// ```rust
/// # use ranch::ascii::Graphic;
/// let ascii = Graphic::new::<b'a'>();
/// let non_alphabetic = Graphic::new::<b'1'>();
///
/// assert_eq!(Graphic::new::<b'A'>(), ascii.to_ascii_uppercase());
/// assert_eq!(Graphic::new::<b'1'>(), non_alphabetic.to_ascii_uppercase());
/// ```
pub const fn to_ascii_uppercase(self) -> Self {
Self::from_ranged(RangedU8(self.get().to_ascii_uppercase()))
}
/// Convert to ASCII lowercase.
///
/// ASCII letters ‘A’ to ‘Z’ are mapped to ‘a’ to ‘z’, but non-alphabetic
/// letters are unchanged.
///
/// ```rust
/// # use ranch::ascii::Graphic;
/// let ascii = Graphic::new::<b'A'>();
/// let non_alphabetic = Graphic::new::<b'1'>();
///
/// assert_eq!(Graphic::new::<b'a'>(), ascii.to_ascii_lowercase());
/// assert_eq!(Graphic::new::<b'1'>(), non_alphabetic.to_ascii_lowercase());
/// ```
pub const fn to_ascii_lowercase(self) -> Self {
Self::from_ranged(RangedU8(self.get().to_ascii_lowercase()))
}
/// Convert to [`char`].
///
/// ```rust
/// # use ranch::ascii::Graphic;
/// let lowercase_a = Graphic::new::<97>();
///
/// assert_eq!(lowercase_a.to_char(), 'a');
/// ```
pub const fn to_char(self) -> char {
let chr: Char = self.to_ranged().to_ranged_u8();
chr.to_char()
}
}
impl Digit {
/// Convert from numeric digit to ASCII digit.
///
/// ```rust
/// # use ranch::{RangedU8, ascii::Digit};
/// assert_eq!(
/// Digit::from_digit(RangedU8::new::<5>()),
/// Digit::new::<b'5'>(),
/// );
/// ```
pub const fn from_digit(digit: RangedU8<0, 9>) -> Self {
Self::from_ranged(digit.add::<0x30, 0x30, 0x39>())
}
/// Convert from ASCII digit to numeric digit.
///
/// ```rust
/// # use ranch::{RangedU8, ascii::Digit};
/// assert_eq!(
/// Digit::new::<b'5'>().to_digit(),
/// RangedU8::new::<5>(),
/// );
/// ```
pub const fn to_digit(self) -> RangedU8<0, 9> {
self.to_ranged().sub::<0x30, 0, 9>()
}
/// Convert to [`char`].
///
/// ```rust
/// # use ranch::ascii::Digit;
/// let one = Digit::new::<0x31>();
///
/// assert_eq!(one.to_char(), '1');
/// ```
pub const fn to_char(self) -> char {
let chr: Char = self.to_ranged().to_ranged_u8();
chr.to_char()
}
}
impl NonNul {
/// Convert to ASCII uppercase.
///
/// ASCII letters ‘a’ to ‘z’ are mapped to ‘A’ to ‘Z’, but non-alphabetic
/// letters are unchanged.
///
/// ```rust
/// # use ranch::ascii::NonNul;
/// let ascii = NonNul::new::<b'a'>();
/// let non_alphabetic = NonNul::new::<b'1'>();
///
/// assert_eq!(NonNul::new::<b'A'>(), ascii.to_ascii_uppercase());
/// assert_eq!(NonNul::new::<b'1'>(), non_alphabetic.to_ascii_uppercase());
/// ```
pub const fn to_ascii_uppercase(self) -> Self {
Self::from_ranged(RangedU8(self.get().to_ascii_uppercase()))
}
/// Convert to ASCII lowercase.
///
/// ASCII letters ‘A’ to ‘Z’ are mapped to ‘a’ to ‘z’, but non-alphabetic
/// letters are unchanged.
///
/// ```rust
/// # use ranch::ascii::NonNul;
/// let ascii = NonNul::new::<b'A'>();
/// let non_alphabetic = NonNul::new::<b'1'>();
///
/// assert_eq!(NonNul::new::<b'a'>(), ascii.to_ascii_lowercase());
/// assert_eq!(NonNul::new::<b'1'>(), non_alphabetic.to_ascii_lowercase());
/// ```
pub const fn to_ascii_lowercase(self) -> Self {
Self::from_ranged(RangedU8(self.get().to_ascii_lowercase()))
}
/// Convert to [`char`].
///
/// ```rust
/// # use ranch::ascii::NonNul;
/// let one = NonNul::new::<b'\n'>();
///
/// assert_eq!(one.to_char(), '\n');
/// ```
pub const fn to_char(self) -> char {
self.get() as char
}
}
impl Char {
/// Convert to ASCII uppercase.
///
/// ASCII letters ‘a’ to ‘z’ are mapped to ‘A’ to ‘Z’, but non-alphabetic
/// letters are unchanged.
///
/// ```rust
/// # use ranch::ascii::Char;
/// let ascii = Char::new::<b'a'>();
/// let non_alphabetic = Char::new::<b'1'>();
///
/// assert_eq!(Char::new::<b'A'>(), ascii.to_ascii_uppercase());
/// assert_eq!(Char::new::<b'1'>(), non_alphabetic.to_ascii_uppercase());
/// ```
pub const fn to_ascii_uppercase(self) -> Self {
Self(self.get().to_ascii_uppercase())
}
/// Convert to ASCII lowercase.
///
/// ASCII letters ‘A’ to ‘Z’ are mapped to ‘a’ to ‘z’, but non-alphabetic
/// letters are unchanged.
///
/// ```rust
/// # use ranch::ascii::Char;
/// let ascii = Char::new::<b'A'>();
/// let non_alphabetic = Char::new::<b'1'>();
///
/// assert_eq!(Char::new::<b'a'>(), ascii.to_ascii_lowercase());
/// assert_eq!(Char::new::<b'1'>(), non_alphabetic.to_ascii_lowercase());
/// ```
pub const fn to_ascii_lowercase(self) -> Self {
Self(self.get().to_ascii_lowercase())
}
/// Convert to [`char`].
///
/// ```rust
/// # use ranch::ascii::Char;
/// let one = Char::new::<b'\n'>();
///
/// assert_eq!(one.to_char(), '\n');
/// ```
pub const fn to_char(self) -> char {
self.get() as char
}
}
impl<const MIN: u8, const MAX: u8> RangedU8<MIN, MAX> {
/// Return an iterator that produces an escaped version of a `u8`, treating
/// it as an ASCII character.
///
/// The behavior is identical to [`u8::escape_ascii()`].
///
/// ```rust
/// # use ranch::ascii::Char;
/// assert_eq!("0", Char::new::<b'0'>().escape_ascii().to_string());
/// assert_eq!("\\t", Char::new::<b'\t'>().escape_ascii().to_string());
/// assert_eq!("\\r", Char::new::<b'\r'>().escape_ascii().to_string());
/// assert_eq!("\\n", Char::new::<b'\n'>().escape_ascii().to_string());
/// assert_eq!("\\'", Char::new::<b'\''>().escape_ascii().to_string());
/// assert_eq!("\\\"", Char::new::<b'"'>().escape_ascii().to_string());
/// assert_eq!("\\\\", Char::new::<b'\\'>().escape_ascii().to_string());
/// ```
///
/// Won't compile if out of ASCII range:
///
/// ```rust,compile_fail
/// assert_eq!("\\x9d", Char::new::<b'\x9d'>().escape_ascii().to_string());
/// ```
pub fn escape_ascii(self) -> EscapeDefault {
self.get().escape_ascii()
}
}