Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api: switch to extension traits #8

Merged
merged 1 commit into from
Jun 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 22 additions & 23 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
bstr
====
This crate provides a `BString` and `BStr` types that are conventionally UTF-8
for Rust. They differ from the standard library's `String` and `str` types in
that they are not required to be valid UTF-8, but may be fully or partially
valid UTF-8.
This crate provides extension traits for `&[u8]` and `Vec<u8>` that enable
their use as byte strings, where byte strings are _conventionally_ UTF-8. This
differs from the standard library's `String` and `str` types in that they are
not required to be valid UTF-8, but may be fully or partially valid UTF-8.

[![Linux build status](https://api.travis-ci.org/BurntSushi/bstr.svg)](https://travis-ci.org/BurntSushi/bstr)
[![Windows build status](https://ci.appveyor.com/api/projects/status/github/BurntSushi/bstr?svg=true)](https://ci.appveyor.com/project/BurntSushi/bstr)
Expand All @@ -18,7 +18,7 @@ https://docs.rs/bstr
### When should I use byte strings?

See this part of the documentation for more details:
https://docs.rs/bstr/0.1.0/bstr/#when-should-i-use-byte-strings.
https://docs.rs/bstr/0.2.0/bstr/#when-should-i-use-byte-strings.

The short story is that byte strings are useful when it is inconvenient or
incorrect to require valid UTF-8.
Expand All @@ -30,7 +30,7 @@ Add this to your `Cargo.toml`:

```toml
[dependencies]
bstr = "0.1"
bstr = "0.2"
```


Expand All @@ -46,15 +46,15 @@ stdin, and print out lines containing a particular substring:
use std::error::Error;
use std::io::{self, Write};

use bstr::io::BufReadExt;
use bstr::{ByteSlice, io::BufReadExt};

fn main() -> Result<(), Box<Error>> {
fn main() -> Result<(), Box<dyn Error>> {
let stdin = io::stdin();
let mut stdout = io::BufWriter::new(io::stdout());

stdin.lock().for_byte_line_with_terminator(|line| {
if line.contains("Dimension") {
stdout.write_all(line.as_bytes())?;
if line.contains_str("Dimension") {
stdout.write_all(line)?;
}
Ok(true)
})?;
Expand All @@ -69,9 +69,9 @@ line-by-line:
use std::error::Error;
use std::io;

use bstr::io::BufReadExt;
use bstr::{ByteSlice, io::BufReadExt};

fn main() -> Result<(), Box<Error>> {
fn main() -> Result<(), Box<dyn Error>> {
let stdin = io::stdin();
let mut words = 0;
stdin.lock().for_byte_line_with_terminator(|line| {
Expand All @@ -92,18 +92,17 @@ library APIs. (N.B. Any invalid UTF-8 bytes are passed through unchanged.)
use std::error::Error;
use std::io::{self, Write};

use bstr::BString;
use bstr::io::BufReadExt;
use bstr::{ByteSlice, io::BufReadExt};

fn main() -> Result<(), Box<Error>> {
fn main() -> Result<(), Box<dyn Error>> {
let stdin = io::stdin();
let mut stdout = io::BufWriter::new(io::stdout());

let mut upper = BString::new();
let mut upper = vec![];
stdin.lock().for_byte_line_with_terminator(|line| {
upper.clear();
line.to_uppercase_into(&mut upper);
stdout.write_all(upper.as_bytes())?;
stdout.write_all(&upper)?;
Ok(true)
})?;
Ok(())
Expand All @@ -118,9 +117,9 @@ as a single character and are passed through correctly:
use std::error::Error;
use std::io::{self, Write};

use bstr::io::BufReadExt;
use bstr::{ByteSlice, io::BufReadExt};

fn main() -> Result<(), Box<Error>> {
fn main() -> Result<(), Box<dyn Error>> {
let stdin = io::stdin();
let mut stdout = io::BufWriter::new(io::stdout());

Expand All @@ -131,7 +130,7 @@ fn main() -> Result<(), Box<Error>> {
.take(10)
.last()
.unwrap_or(line.len());
stdout.write_all(line[..end].trim_end().as_bytes())?;
stdout.write_all(line[..end].trim_end())?;
stdout.write_all(b"\n")?;
Ok(true)
})?;
Expand All @@ -146,7 +145,7 @@ This crates comes with a few features that control standard library, serde
and Unicode support.

* `std` - **Enabled** by default. This provides APIs that require the standard
library, such as `BString`.
library, such as `Vec<u8>`.
* `unicode` - **Enabled** by default. This provides APIs that require sizable
Unicode data compiled into the binary. This includes, but is not limited to,
grapheme/word/sentence segmenters. When this is disabled, basic support such
Expand Down Expand Up @@ -206,8 +205,8 @@ except for `memchr`, is optional.
### High level motivation

Strictly speaking, the `bstr` crate provides very little that can't already be
achieved with a `Vec<u8>`/`&[u8]` and the ecosystem of library crates. For
example:
achieved with the standard library `Vec<u8>`/`&[u8]` APIs and the ecosystem of
library crates. For example:

* The standard library's
[`Utf8Error`](https://doc.rust-lang.org/std/str/struct.Utf8Error.html)
Expand Down
Loading