rust-lang / regex

An implementation of regular expressions for Rust. This implementation uses finite automata and guarantees linear time matching on all inputs.

Home Page:https://docs.rs/regex

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add hir::literal_char or an example showing how to create an hir::Literal from a char

nwalfield opened this issue · comments

The documentation for hir::literal says:

Creates a literal HIR expression.

This accepts anything that can be converted into a Box<[u8]>.

I'm trying to create an hir::Literal from a char, which, as far as I know, doesn't provide a conversion to a Box<[u8]>. In the end, I did this:

        // "A buffer of length four is large enough to encode any
        // char."
        //
        // https://doc.rust-lang.org/std/primitive.char.html#method.encode_utf8
        let mut buffer = [0; 4];
        // Convert the Unicode character t to a string.
        let s = t.to_char().encode_utf8(&mut buffer);
        hir::Hir::literal(s.as_bytes())

It would be nice if the documentation for literal included this (or something better?) as an example, or Hir had a convenience function, perhaps literal_char, that does this.

When I first looked at this, I figured it would be better to add a convenience constructor (or From impl) for the hir::Literal type itself. But it turns out that you can't actually make an Hir value from a hir::Literal using the public API. That's... not great... But not bad enough to do much about now I think. I can revisit that next time I make a breaking change to regex-syntax. (I just did one, so it probably won't be soon.)

I don't think this case is worth a new convenience function on Hir itself, but I did add an example to the docs. Thanks!