Skip to content

Commit

Permalink
clarify character generation in Name
Browse files Browse the repository at this point in the history
Co-authored-by: jedel1043 <[email protected]>
  • Loading branch information
addisoncrump and jedel1043 authored Mar 17, 2022
1 parent a615901 commit dd85b72
Showing 1 changed file with 13 additions and 8 deletions.
21 changes: 13 additions & 8 deletions boa_inputgen/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,21 @@ struct Name {
impl Arbitrary<'_> for Name {
fn arbitrary(u: &mut Unstructured<'_>) -> arbitrary::Result<Self> {
// generate a valid identifier; starts with at least one alphabetic character
let first = u8::arbitrary(u)?;
let first = ALPHA[(first as usize) % ALPHA.len()];
let mut chars = match Vec::<_>::arbitrary(u)? {
v if v.is_empty() => return Err(arbitrary::Error::NotEnoughData),
v => v,
};

let (first, rest) = chars
.split_first_mut()
.expect("Ensured above that the vec is not empty");

*first = ALPHA[(*first as usize) % ALPHA.len()];

// remaining characters are alphanumeric
let mut chars: Vec<u8> = vec![first];
let mut second: Vec<u8> = Arbitrary::arbitrary(u)?;
second
.iter_mut()
.for_each(|c| *c = ALPHANUM[(*c as usize) % ALPHANUM.len()]);
chars.extend(second);
for c in rest {
*c = ALPHANUM[(*c as usize) % ALPHANUM.len()];
}

Ok(Self {
name: String::from_utf8(chars).expect("Only valid characters used."),
Expand Down

0 comments on commit dd85b72

Please sign in to comment.