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

Add ExtSlice::escape_debug_into #37

Closed
wants to merge 1 commit into from

Conversation

lopopolo
Copy link
Contributor

@lopopolo lopopolo commented Feb 2, 2020

Add a method to the ExtSlice trait that supports writing the BStr
escaped representation into a fmt::Write. This enables extracting the
escaped String into a buffer without going through fmt::Debug.

The written String does not contain the surrounding quotes present in
the fmt::Debug implementation.

This PR implements fmt::Debug on BStr with the new method.

Fixes GH-34.

Copy link
Owner

@BurntSushi BurntSushi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks so much for the PR! Overall this looks great and I appreciate the docs you added. However, I think there are a few issues I'd like to see addressed before merging though.

Also, for small PRs like this, could you make sure everything is squashed down to a single commit? Thanks. (Force pushing is okay.)

src/ext_slice.rs Outdated Show resolved Hide resolved
src/ext_slice.rs Outdated Show resolved Hide resolved
src/ext_slice.rs Show resolved Hide resolved
src/ext_slice.rs Outdated
/// assert_eq!(r"cannot load such file -- utf8-invalid-name-\xFF", message);
/// ```
#[inline]
fn escape_debug_into<T: fmt::Write>(&self, f: &mut T) -> fmt::Result {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why does this take an &mut T instead of a T? Also, could you use W as the type parameter?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought I needed to do this to allow this function to be used in the fmt::Debug impl, but I didn't realize &mut &mut fmt::Formatter is also a fmt::Write.

src/ext_slice.rs Outdated
/// ```
#[inline]
fn escape_debug_into<T: fmt::Write>(&self, f: &mut T) -> fmt::Result {
let buf = self.as_bstr();
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you converting the slice to a &BStr first? It appears unnecessary and also results in an extra as_bytes() call below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was a holdover from my own implementation of this function in my downstream project. I didn't realize all these functions are implemented on &[u8] too. fixed.

Add a method to the `ExtSlice` trait that supports writing the `BStr`
escaped representation into a `fmt::Write`. This enables extracting the
escaped `String` into a buffer without going through `fmt::Debug`.

The written `String` does not contain the surrounding quotes present in
the `fmt::Debug` implementation.

Fixes BurntSushiGH-34.

This change reimplements `fmt::Debug` for `BStr` with
`ExtSlice::escape_debug_into`.
@lopopolo
Copy link
Contributor Author

Hi @BurntSushi thanks for the feedback. I think I've addressed everything.

I did consider using [_]::get instead of slice indexing to avoid a panic pathway in non performance critical code, but erred toward local consistency of other extension functions using slice indexing.

lopopolo added a commit to artichoke/artichoke that referenced this pull request Feb 22, 2020
- Rename `string::escape_unicode` to
  `string::format_unicode_debug_into`.
- Rename writer type parameters to `W` for int and unicode debug
  formatters.
- Remove `&mut` from writer parameter.
- Remove the `impl From<fmt::Error> for WriteError` conversion.
- Sync docs for `format_unicode_debug_into` from BurntSushi/bstr#37.

While fixing up the use of `escape_unicode` in `artichoke-frontend`, I
got a bit distracted and let the scope of this change creep.

- Unify Ruby CLI errors on `Exception`. Imports `IOError` and
  `LoadError` to manually construct some errors from strings.
- Do not use `eprintln!` in `artichoke` binary to suppress broken pipe
  errors instead of panicking.
- Set exit code to 1 on error in `artichoke` binary.
lopopolo added a commit to artichoke/artichoke that referenced this pull request Feb 22, 2020
- Rename `string::escape_unicode` to
  `string::format_unicode_debug_into`.
- Rename writer type parameters to `W` for int and unicode debug
  formatters.
- Remove `&mut` from writer parameter.
- Remove the `impl From<fmt::Error> for WriteError` conversion.
- Sync docs for `format_unicode_debug_into` from BurntSushi/bstr#37.

While fixing up the use of `escape_unicode` in `artichoke-frontend`, I
got a bit distracted and let the scope of this change creep.

- Unify Ruby CLI errors on `Exception`. Imports `IOError` and
  `LoadError` to manually construct some errors from strings.
- Do not use `eprintln!` in `artichoke` binary to suppress broken pipe
  errors instead of panicking.
- Set exit code to 1 on error in `artichoke` binary.
Copy link
Owner

@BurntSushi BurntSushi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. Note:

I did consider using [_]::get instead of slice indexing to avoid a panic pathway in non performance critical code

Using get here wouldn't make sense, since the slice indexing can never fail. If it does fail, then it implies a bug in the char_indices implementation.

Copy link
Owner

@BurntSushi BurntSushi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All right, so I hate to do this, but I just spent some time reviewing how std does this, and this implementation path is inconsistent with std. I wish I realized this sooner instead of steering you down a wrong path.

Basically, I think the right way to do this is to mirror std: add an escape_debug method that returns an iterator of char values corresponding to the output. It's more complex to implement, but is a more flexible API and is consistent with std.

@lopopolo
Copy link
Contributor Author

@BurntSushi can I defer this change to you? While it may be the right thing to do for bstr, it is unnecessary to meet my needs.

@BurntSushi
Copy link
Owner

Sure, but I have no idea when I'll get to it.

@BurntSushi BurntSushi closed this May 10, 2020
@lopopolo lopopolo deleted the escape-debug-into branch January 21, 2023 03:06
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Write escaped string into a buffer
2 participants