
* Moving the logic from Rope to text::Buffer makes it easier to keep the Rope in sync with the fragment tree. * Removing carriage return characters is lossier, but is much simpler than incrementally maintaining the invariant that there are no carriage returns followed by newlines. We may want to do something smarter in the future. Co-authored-by: Keith Simmons <keith@zed.dev>
36 lines
1.1 KiB
Rust
36 lines
1.1 KiB
Rust
use rand::prelude::*;
|
||
|
||
pub struct RandomCharIter<T: Rng>(T);
|
||
|
||
impl<T: Rng> RandomCharIter<T> {
|
||
pub fn new(rng: T) -> Self {
|
||
Self(rng)
|
||
}
|
||
}
|
||
|
||
impl<T: Rng> Iterator for RandomCharIter<T> {
|
||
type Item = char;
|
||
|
||
fn next(&mut self) -> Option<Self::Item> {
|
||
if std::env::var("SIMPLE_TEXT").map_or(false, |v| !v.is_empty()) {
|
||
return if self.0.gen_range(0..100) < 5 {
|
||
Some('\n')
|
||
} else {
|
||
Some(self.0.gen_range(b'a'..b'z' + 1).into())
|
||
};
|
||
}
|
||
|
||
match self.0.gen_range(0..100) {
|
||
// whitespace
|
||
0..=19 => [' ', '\n', '\r', '\t'].choose(&mut self.0).copied(),
|
||
// two-byte greek letters
|
||
20..=32 => char::from_u32(self.0.gen_range(('α' as u32)..('ω' as u32 + 1))),
|
||
// // three-byte characters
|
||
33..=45 => ['✋', '✅', '❌', '❎', '⭐'].choose(&mut self.0).copied(),
|
||
// // four-byte characters
|
||
46..=58 => ['🍐', '🏀', '🍗', '🎉'].choose(&mut self.0).copied(),
|
||
// ascii letters
|
||
_ => Some(self.0.gen_range(b'a'..b'z' + 1).into()),
|
||
}
|
||
}
|
||
}
|