Skip to content

Commit

Permalink
syntax: improve Debug impl for Class
Browse files Browse the repository at this point in the history
Previously, classes would show up in the debug representation as very
deeply nested things, making them more difficult to read than they need
to be. This removes at least a few pretty redundant layers and uses a
more compact range notation.
  • Loading branch information
BurntSushi committed Mar 19, 2023
1 parent 4e044a2 commit 8c26f0a
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion regex-syntax/src/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -787,7 +787,7 @@ impl core::fmt::Debug for Literal {
/// the author of the regular expression to disable Unicode mode, which in turn
/// impacts the semantics of case insensitive matching. For example, `(?i)k`
/// and `(?i-u)k` will not match the same set of strings.
#[derive(Clone, Debug, Eq, PartialEq)]
#[derive(Clone, Eq, PartialEq)]
pub enum Class {
/// A set of characters represented by Unicode scalar values.
Unicode(ClassUnicode),
Expand Down Expand Up @@ -986,6 +986,27 @@ impl Class {
}
}

impl core::fmt::Debug for Class {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
use crate::debug::Byte;

let mut fmter = f.debug_set();
match *self {
Class::Unicode(ref cls) => {
for r in cls.ranges().iter() {
fmter.entry(&(r.start..=r.end));
}
}
Class::Bytes(ref cls) => {
for r in cls.ranges().iter() {
fmter.entry(&(Byte(r.start)..=Byte(r.end)));
}
}
}
fmter.finish()
}
}

/// A set of characters represented by Unicode scalar values.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ClassUnicode {
Expand Down

0 comments on commit 8c26f0a

Please sign in to comment.