Skip to content

Commit

Permalink
syntax: add Hir::literal example for char
Browse files Browse the repository at this point in the history
The example shows a succinct way of creating an HIR literal from a
`char` value by first encoding it to UTF-8.

Closes #1114
  • Loading branch information
BurntSushi committed Oct 25, 2023
1 parent 20b5317 commit 6b72eec
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions regex-syntax/src/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,22 @@ impl Hir {
/// let expected = HirKind::Literal(Literal(Box::from("☃".as_bytes())));
/// assert_eq!(&expected, concat.kind());
/// ```
///
/// # Example: building a literal from a `char`
///
/// This example shows how to build a single `Hir` literal from a `char`
/// value. Since a [`Literal`] is just bytes, we just need to UTF-8
/// encode a `char` value:
///
/// ```
/// use regex_syntax::hir::{Hir, HirKind, Literal};
///
/// let ch = '☃';
/// let got = Hir::literal(ch.encode_utf8(&mut [0; 4]).as_bytes());
///
/// let expected = HirKind::Literal(Literal(Box::from("☃".as_bytes())));
/// assert_eq!(&expected, got.kind());
/// ```
#[inline]
pub fn literal<B: Into<Box<[u8]>>>(lit: B) -> Hir {
let bytes = lit.into();
Expand Down

0 comments on commit 6b72eec

Please sign in to comment.