@@ -79,13 +79,15 @@ pub(crate) fn dec_int<'i>(input: &mut Input<'i>) -> PResult<&'i str> {
79
79
) ) ,
80
80
)
81
81
. recognize ( )
82
+ // Safety: DIGIT1_9, digit(), and `_` only covers ASCII ranges
82
83
. map ( |b : & [ u8 ] | unsafe {
83
84
from_utf8_unchecked ( b, "`digit` and `_` filter out non-ASCII" )
84
85
} )
85
86
. context ( StrContext :: Label ( "integer" ) ) ,
86
87
)
87
88
. parse_next ( input)
88
89
}
90
+ /// Safety-usable invariant: DIGIT1_9 is only ASCII ranges
89
91
const DIGIT1_9 : RangeInclusive < u8 > = b'1' ..=b'9' ;
90
92
91
93
// hex-prefix = %x30.78 ; 0x
@@ -114,11 +116,13 @@ pub(crate) fn hex_int<'i>(input: &mut Input<'i>) -> PResult<&'i str> {
114
116
) )
115
117
. recognize ( ) ,
116
118
)
119
+ // Safety: HEX_PREFIX, hexdig(), and `_` only covers ASCII ranges
117
120
. map ( |b| unsafe { from_utf8_unchecked ( b, "`hexdig` and `_` filter out non-ASCII" ) } )
118
121
. context ( StrContext :: Label ( "hexadecimal integer" ) ) ,
119
122
)
120
123
. parse_next ( input)
121
124
}
125
+ /// Safety-usable invariant: HEX_PREFIX is ASCII only
122
126
const HEX_PREFIX : & [ u8 ] = b"0x" ;
123
127
124
128
// oct-prefix = %x30.6F ; 0o
@@ -147,12 +151,15 @@ pub(crate) fn oct_int<'i>(input: &mut Input<'i>) -> PResult<&'i str> {
147
151
) )
148
152
. recognize ( ) ,
149
153
)
154
+ // Safety: DIGIT0_7, OCT_PREFIX, and `_` only covers ASCII ranges
150
155
. map ( |b| unsafe { from_utf8_unchecked ( b, "`DIGIT0_7` and `_` filter out non-ASCII" ) } )
151
156
. context ( StrContext :: Label ( "octal integer" ) ) ,
152
157
)
153
158
. parse_next ( input)
154
159
}
160
+ /// Safety-usable invariant: OCT_PREFIX is ASCII only
155
161
const OCT_PREFIX : & [ u8 ] = b"0o" ;
162
+ /// Safety-usable invariant: DIGIT0_7 is ASCII only
156
163
const DIGIT0_7 : RangeInclusive < u8 > = b'0' ..=b'7' ;
157
164
158
165
// bin-prefix = %x30.62 ; 0b
@@ -181,12 +188,15 @@ pub(crate) fn bin_int<'i>(input: &mut Input<'i>) -> PResult<&'i str> {
181
188
) )
182
189
. recognize ( ) ,
183
190
)
191
+ // Safety: DIGIT0_1, BIN_PREFIX, and `_` only covers ASCII ranges
184
192
. map ( |b| unsafe { from_utf8_unchecked ( b, "`DIGIT0_1` and `_` filter out non-ASCII" ) } )
185
193
. context ( StrContext :: Label ( "binary integer" ) ) ,
186
194
)
187
195
. parse_next ( input)
188
196
}
197
+ /// Safety-usable invariant: BIN_PREFIX is ASCII only
189
198
const BIN_PREFIX : & [ u8 ] = b"0b" ;
199
+ /// Safety-usable invariant: DIGIT0_1 is ASCII only
190
200
const DIGIT0_1 : RangeInclusive < u8 > = b'0' ..=b'1' ;
191
201
192
202
// ;; Float
@@ -234,6 +244,7 @@ pub(crate) fn frac<'i>(input: &mut Input<'i>) -> PResult<&'i str> {
234
244
)
235
245
. recognize ( )
236
246
. map ( |b : & [ u8 ] | unsafe {
247
+ // Safety: `.` and `zero_prefixable_int` only handle ASCII
237
248
from_utf8_unchecked (
238
249
b,
239
250
"`.` and `parse_zero_prefixable_int` filter out non-ASCII" ,
@@ -243,6 +254,7 @@ pub(crate) fn frac<'i>(input: &mut Input<'i>) -> PResult<&'i str> {
243
254
}
244
255
245
256
// zero-prefixable-int = DIGIT *( DIGIT / underscore DIGIT )
257
+ /// Safety-usable invariant: only produces ASCII
246
258
pub ( crate ) fn zero_prefixable_int < ' i > ( input : & mut Input < ' i > ) -> PResult < & ' i str > {
247
259
(
248
260
digit,
@@ -261,8 +273,10 @@ pub(crate) fn zero_prefixable_int<'i>(input: &mut Input<'i>) -> PResult<&'i str>
261
273
. map ( |( ) | ( ) ) ,
262
274
)
263
275
. recognize ( )
276
+ // Safety: `digit()` and `_` are all ASCII
264
277
. map ( |b : & [ u8 ] | unsafe { from_utf8_unchecked ( b, "`digit` and `_` filter out non-ASCII" ) } )
265
278
. parse_next ( input)
279
+ /// Safety-usable invariant upheld by only using `digit` and `_` in the parser
266
280
}
267
281
268
282
// exp = "e" float-exp-part
@@ -275,6 +289,7 @@ pub(crate) fn exp<'i>(input: &mut Input<'i>) -> PResult<&'i str> {
275
289
)
276
290
. recognize ( )
277
291
. map ( |b : & [ u8 ] | unsafe {
292
+ // Safety: `e`, `E`, `+`, `-`, and `zero_prefixable_int` are all ASCII
278
293
from_utf8_unchecked (
279
294
b,
280
295
"`one_of` and `parse_zero_prefixable_int` filter out non-ASCII" ,
@@ -305,15 +320,20 @@ pub(crate) fn nan(input: &mut Input<'_>) -> PResult<f64> {
305
320
const NAN : & [ u8 ] = b"nan" ;
306
321
307
322
// DIGIT = %x30-39 ; 0-9
323
+ /// Safety-usable invariant: only parses ASCII
308
324
pub ( crate ) fn digit ( input : & mut Input < ' _ > ) -> PResult < u8 > {
325
+ // Safety: DIGIT is all ASCII
309
326
one_of ( DIGIT ) . parse_next ( input)
310
327
}
311
328
const DIGIT : RangeInclusive < u8 > = b'0' ..=b'9' ;
312
329
313
330
// HEXDIG = DIGIT / "A" / "B" / "C" / "D" / "E" / "F"
331
+ /// Safety-usable invariant: only parses ASCII
314
332
pub ( crate ) fn hexdig ( input : & mut Input < ' _ > ) -> PResult < u8 > {
333
+ // Safety: HEXDIG is all ASCII
315
334
one_of ( HEXDIG ) . parse_next ( input)
316
335
}
336
+ /// Safety-usable invariant: only ASCII ranges
317
337
pub ( crate ) const HEXDIG : ( RangeInclusive < u8 > , RangeInclusive < u8 > , RangeInclusive < u8 > ) =
318
338
( DIGIT , b'A' ..=b'F' , b'a' ..=b'f' ) ;
319
339
0 commit comments