From 8c26f0a0c06af6680df4e79d1fb47d93dc73a029 Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Tue, 14 Mar 2023 22:37:52 -0400 Subject: [PATCH] syntax: improve Debug impl for Class 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. --- regex-syntax/src/hir/mod.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/regex-syntax/src/hir/mod.rs b/regex-syntax/src/hir/mod.rs index 481682e6e..410053d85 100644 --- a/regex-syntax/src/hir/mod.rs +++ b/regex-syntax/src/hir/mod.rs @@ -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), @@ -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 {