Skip to content

Commit 95385df

Browse files
committed
small docs update and some code golf
1 parent cdea355 commit 95385df

File tree

1 file changed

+17
-9
lines changed

1 file changed

+17
-9
lines changed

src/chunked/encoder.rs

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,18 @@ impl<R: Read + Unpin> ChunkedEncoder<R> {
2424

2525
fn max_bytes_to_read(buf_len: usize) -> usize {
2626
if buf_len < 6 {
27+
// the minimum read size is of 6 represents one byte of
28+
// content from the body. the other five bytes are 1\r\n_\r\n
29+
// where _ is the actual content in question
2730
panic!("buffers of length {} are too small for this implementation. if this is a problem for you, please open an issue", buf_len);
2831
}
32+
33+
let bytes_remaining_after_two_cr_lns = (buf_len - 4) as f64;
34+
2935
// the maximum number of bytes that the hex representation of remaining bytes might take
30-
let max_bytes_of_hex_framing = (((buf_len - 5) as f64).log2() / 4f64).floor();
31-
buf_len - 5 - (max_bytes_of_hex_framing as usize)
36+
let max_bytes_of_hex_framing = bytes_remaining_after_two_cr_lns.log2() / 4f64;
37+
38+
(bytes_remaining_after_two_cr_lns - max_bytes_of_hex_framing.ceil()) as usize
3239
}
3340

3441
#[cfg(test)]
@@ -39,10 +46,10 @@ mod test_bytes_to_read {
3946
// and a nonobvious but intentional consequence of the
4047
// implementation. in order to avoid overflowing, we must use
4148
// one fewer than the available buffer bytes because
42-
// increasing the read size by one byte would increase the
43-
// number of framed bytes by two. This occurs when the hex
44-
// representation of the content bytes increases order of
45-
// magnitude (F->10, FF->100, FFF-> 1000, etc)
49+
// increasing the read size increase the number of framed
50+
// bytes by two. This occurs when the hex representation of
51+
// the content bytes is near an increase in order of magnitude
52+
// (F->10, FF->100, FFF-> 1000, etc)
4653
let values = vec![
4754
(6, 1), // 1
4855
(7, 2), // 2
@@ -52,12 +59,13 @@ mod test_bytes_to_read {
5259
(23, 17), // 11
5360
(260, 254), // FE
5461
(261, 254), // FE <-
55-
(262, 255), // FF
62+
(262, 255), // FF <-
5663
(263, 256), // 100
5764
(4100, 4093), // FFD
5865
(4101, 4093), // FFD <-
59-
(4102, 4094), // FFE
60-
(4103, 4095), // FFF
66+
(4102, 4094), // FFE <-
67+
(4103, 4095), // FFF <-
68+
(4104, 4096), // 1000
6169
];
6270

6371
for (input, expected) in values {

0 commit comments

Comments
 (0)