/// Parse a buffer of bytes as a chunk size.
///
/// The return value, if complete and successful, includes the index of the
/// buffer that parsing stopped at, and the size of the following chunk.
///
/// # Example
///
/// ```
/// let buf = b"4\r\nRust\r\n0\r\n\r\n";
/// assert_eq!(httparse::parse_chunk_size(buf),
///            Ok(httparse::Status::Complete((3, 4))));
/// ```
Parse a buffer of bytes as a chunk size.
The return value, if complete and successful, includes the index of the
buffer that parsing stopped at, and the size of the following chunk.
Parse a buffer of bytes as a chunk size . The return value if complete and successful includes the index of the buffer that parsing stopped at and the size of the following chunk .
pub fn parse_chunk_size(buf: &[u8])
    -> result::Result<Status<(usize, u64)>, InvalidChunkSize> {
    const RADIX: u64 = 16;
    let mut bytes = Bytes::new(buf);
    let mut size = 0;
    let mut in_chunk_size = true;
    let mut in_ext = false;
    let mut count = 0;
    loop {
        let b = next!(bytes);
        match b {
            b'0' ..= b'9' if in_chunk_size => {
                if count > 15 {
                    return Err(InvalidChunkSize);
                }
                count += 1;
                size *= RADIX;
                size += (b - b'0') as u64;
            },
            b'a' ..= b'f' if in_chunk_size => {
                if count > 15 {
                    return Err(InvalidChunkSize);
                }
                count += 1;
                size *= RADIX;
                size += (b + 10 - b'a') as u64;
            }
            b'A' ..= b'F' if in_chunk_size => {
                if count > 15 {
                    return Err(InvalidChunkSize);
                }
                count += 1;
                size *= RADIX;
                size += (b + 10 - b'A') as u64;
            }
            b'\r' => {
                match next!(bytes) {
                    b'\n' => break,
                    _ => return Err(InvalidChunkSize),
                }
            }
            // If we weren't in the extension yet, the ";" signals its start
            b';' if !in_ext => {
                in_ext = true;
                in_chunk_size = false;
            }
            // "Linear white space" is ignored between the chunk size and the
            // extension separator token (";") due to the "implied *LWS rule".
            b'\t' | b' ' if !in_ext && !in_chunk_size => {}
            // LWS can follow the chunk size, but no more digits can come
            b'\t' | b' ' if in_chunk_size => in_chunk_size = false,
            // We allow any arbitrary octet once we are in the extension, since
            // they all get ignored anyway. According to the HTTP spec, valid
            // extensions would have a more strict syntax:
            //     (token ["=" (token | quoted-string)])
            // but we gain nothing by rejecting an otherwise valid chunk size.
            _ if in_ext => {}
            // Finally, if we aren't in the extension and we're reading any
            // other octet, the chunk size line is invalid!
            _ => return Err(InvalidChunkSize),
        }
    }
    Ok(Status::Complete((bytes.pos(), size)))
}
pub fn parse_chunk_size ( buf : & [ u8 ] ) -> result :: Result < Status < ( usize , u64 ) > , InvalidChunkSize > { const RADIX : u64 = 16 ; let mut bytes = Bytes :: new ( buf ) ; let mut size = 0 ; let mut in_chunk_size = true ; let mut in_ext = false ; let mut count = 0 ; loop { let b = next ! ( bytes ) ; match b { b'0' ..= b'9' if in_chunk_size => { if count > 15 { return Err ( InvalidChunkSize ) ; } count += 1 ; size *= RADIX ; size += ( b - b'0' ) as u64 ; } , b'a' ..= b'f' if in_chunk_size => { if count > 15 { return Err ( InvalidChunkSize ) ; } count += 1 ; size *= RADIX ; size += ( b + 10 - b'a' ) as u64 ; } b'A' ..= b'F' if in_chunk_size => { if count > 15 { return Err ( InvalidChunkSize ) ; } count += 1 ; size *= RADIX ; size += ( b + 10 - b'A' ) as u64 ; } b'\r' => { match next ! ( bytes ) { b'\n' => break , _ => return Err ( InvalidChunkSize ) , } } // If we weren't in the extension yet, the ";" signals its start b';' if ! in_ext => { in_ext = true ; in_chunk_size = false ; } // "Linear white space" is ignored between the chunk size and the // extension separator token (";") due to the "implied *LWS rule". b'\t' | b' ' if ! in_ext && ! in_chunk_size => { } // LWS can follow the chunk size, but no more digits can come b'\t' | b' ' if in_chunk_size => in_chunk_size = false , // We allow any arbitrary octet once we are in the extension, since // they all get ignored anyway. According to the HTTP spec, valid // extensions would have a more strict syntax: //     (token ["=" (token | quoted-string)]) // but we gain nothing by rejecting an otherwise valid chunk size. _ if in_ext => { } // Finally, if we aren't in the extension and we're reading any // other octet, the chunk size line is invalid! _ => return Err ( InvalidChunkSize ) , } } Ok ( Status :: Complete ( ( bytes . pos ( ) , size ) ) ) }
Parse a buffer of bytes as a chunk size.
Parse a buffer of bytes as a chunk size .
buf
&[u8]
example
None