Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
maybe implement arbitrary for unicode classes
  • Loading branch information
addisoncrump committed May 6, 2023
commit 2648e3addca0e7db9684f49ee9c110eaa9076144
3 changes: 2 additions & 1 deletion fuzz/fuzz_targets/ast_roundtrip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ struct FuzzData {
impl std::fmt::Debug for FuzzData {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut builder = f.debug_struct("FuzzData");
builder.field("ast", &format!("{}", self.ast));
builder.field("ast", &self.ast);
builder.field("stringified", &format!("{}", self.ast));
builder.finish()
}
}
Expand Down
113 changes: 112 additions & 1 deletion regex-syntax/src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,6 @@ impl ClassUnicode {

/// The available forms of Unicode character classes.
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
pub enum ClassUnicodeKind {
/// A one letter abbreviated class, e.g., `\pN`.
OneLetter(char),
Expand All @@ -858,6 +857,118 @@ pub enum ClassUnicodeKind {
},
}

#[cfg(feature = "arbitrary")]
impl arbitrary::Arbitrary<'_> for ClassUnicodeKind {
fn arbitrary(u: &mut arbitrary::Unstructured) -> arbitrary::Result<Self> {
#[cfg(any(
feature = "unicode-age",
feature = "unicode-bool",
feature = "unicode-gencat",
feature = "unicode-perl",
feature = "unicode-script",
feature = "unicode-segment",
))]
{
use alloc::string::ToString;
match u.choose_index(3)? {
0 => {
let all = super::unicode_tables::property_values::PROPERTY_VALUES.iter()
.flat_map(|e| e.1.iter())
.filter(|(name, _)| name.len() == 1)
.count();
let idx = u.choose_index(all)?;
let value = super::unicode_tables::property_values::PROPERTY_VALUES.iter()
.flat_map(|e| e.1.iter())
.take(idx + 1)
.last().unwrap().0
.chars().next().unwrap();
Ok(ClassUnicodeKind::OneLetter(value))
}
variant => {
let all = super::unicode_tables::property_values::PROPERTY_VALUES.iter()
.map(|e| e.1.len())
.sum::<usize>();
let idx = u.choose_index(all)?;
let (prop, value) = super::unicode_tables::property_values::PROPERTY_VALUES.iter()
.flat_map(|e| e.1.iter().map(|(_, value)| (e.0, value)))
.take(idx + 1)
.last().unwrap();
if variant == 1 {
Ok(ClassUnicodeKind::Named(value.to_string()))
} else {
Ok(ClassUnicodeKind::NamedValue {
op: u.arbitrary()?,
name: prop.to_string(),
value: value.to_string(),
})
}
}
}
}
#[cfg(not(any(
feature = "unicode-age",
feature = "unicode-bool",
feature = "unicode-gencat",
feature = "unicode-perl",
feature = "unicode-script",
feature = "unicode-segment",
)))]
{
match u.choose_index(3)? {
0 => Ok(ClassUnicodeKind::OneLetter(u.arbitrary()?)),
1 => Ok(ClassUnicodeKind::Named(u.arbitrary()?)),
2 => Ok(ClassUnicodeKind::NamedValue {
op: u.arbitrary()?,
name: u.arbitrary()?,
value: u.arbitrary()?,
}),
}
}
}

fn size_hint(depth: usize) -> (usize, Option<usize>) {
#[cfg(any(
feature = "unicode-age",
feature = "unicode-bool",
feature = "unicode-gencat",
feature = "unicode-perl",
feature = "unicode-script",
feature = "unicode-segment",
))]
{
arbitrary::size_hint::and_all(&[
usize::size_hint(depth),
usize::size_hint(depth),
arbitrary::size_hint::or(
(0, Some(0)),
ClassUnicodeOpKind::size_hint(depth),
),
])
}
#[cfg(not(any(
feature = "unicode-age",
feature = "unicode-bool",
feature = "unicode-gencat",
feature = "unicode-perl",
feature = "unicode-script",
feature = "unicode-segment",
)))]
{
arbitrary::size_hint::and(
usize::size_hint(depth),
arbitrary::size_hint::or_all(&[
char::size_hint(depth),
String::size_hint(depth),
arbitrary::size_hint::and(
String::size_hint(depth),
String::size_hint(depth),
),
]),
)
}
}
}

/// The type of op used in a Unicode character class.
#[derive(Clone, Debug, Eq, PartialEq)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
Expand Down