#line #wrap

line-wrap

Efficiently insert line separators

2 releases

Uses old Rust 2015

0.1.1 Oct 27, 2018
0.1.0 Oct 27, 2018

#1228 in Encoding

Download history 54520/week @ 2023-08-10 50693/week @ 2023-08-17 64645/week @ 2023-08-24 52767/week @ 2023-08-31 54254/week @ 2023-09-07 48186/week @ 2023-09-14 46703/week @ 2023-09-21 49003/week @ 2023-09-28 50748/week @ 2023-10-05 53626/week @ 2023-10-12 52288/week @ 2023-10-19 58798/week @ 2023-10-26 58309/week @ 2023-11-02 58193/week @ 2023-11-09 60950/week @ 2023-11-16 45697/week @ 2023-11-23

233,755 downloads per month
Used in 386 crates (2 directly)

Apache-2.0

21KB
379 lines

Build Status

See the docs for usage info.

This line-wrapping logic originally was part of rust-base64.


lib.rs:

Efficiently insert line endings.

If you have a buffer full of data and want to insert any sort of regularly-spaced separator, this will do it with a minimum of data copying. Commonly, this is to insert \n (see lf()) or \r\n (crlf()), but any byte sequence can be used.

  1. Pick a line ending. For single byte separators, see ByteLineEnding, or for two bytes, TwoByteLineEnding. For arbitrary byte slices, use SliceLineEnding.
  2. Call line_wrap.
  3. Your data has been rearranged in place with the specified line ending inserted.

Examples

use line_wrap::*;
// suppose we have 80 bytes of data in a buffer and we want to wrap as per MIME.
// Buffer is large enough to hold line endings.
let mut data = vec![0; 82];

assert_eq!(2, line_wrap(&mut data, 80, 76, &crlf()));

// first line of zeroes
let mut expected_data = vec![0; 76];
// line ending
expected_data.extend_from_slice(b"\r\n");
// next line
expected_data.extend_from_slice(&[0, 0, 0, 0]);
assert_eq!(expected_data, data);

Performance

On an i7 6850k:

  • 10 byte input, 1 byte line length takes ~60ns (~160MiB/s)
  • 100 byte input, 10 byte lines takes ~60ns (~1.6GiB/s)
    • Startup costs dominate at these small lengths
  • 1,000 byte input, 100 byte lines takes ~65ns (~15GiB/s)
  • 10,000 byte input, 100 byte lines takes ~550ns (~17GiB/s)
  • In general, SliceLineEncoding is about 75% the speed of the fixed-length impls.

Naturally, try cargo +nightly bench on your hardware to get more representative data.

Dependencies

~8KB