]>
git.proxmox.com Git - rustc.git/blob - vendor/regex/examples/shootout-regex-dna-single-cheat.rs
1 // The Computer Language Benchmarks Game
2 // https://benchmarksgame-team.pages.debian.net/benchmarksgame/
4 // contributed by the Rust Project Developers
5 // contributed by TeXitoi
6 // contributed by BurntSushi
10 use std
::io
::{self, Read}
;
14 ::regex
::Regex
::new($re
).unwrap()
19 let mut seq
= String
::with_capacity(50 * (1 << 20));
20 io
::stdin().read_to_string(&mut seq
).unwrap();
23 seq
= regex
!(">[^\n]*\n|\n").replace_all(&seq
, "").into_owned();
27 regex
!("agggtaaa|tttaccct"),
28 regex
!("[cgt]gggtaaa|tttaccc[acg]"),
29 regex
!("a[act]ggtaaa|tttacc[agt]t"),
30 regex
!("ag[act]gtaaa|tttac[agt]ct"),
31 regex
!("agg[act]taaa|ttta[agt]cct"),
32 regex
!("aggg[acg]aaa|ttt[cgt]ccct"),
33 regex
!("agggt[cgt]aa|tt[acg]accct"),
34 regex
!("agggta[cgt]a|t[acg]taccct"),
35 regex
!("agggtaa[cgt]|[acg]ttaccct"),
38 println
!("{} {}", re
.to_string(), re
.find_iter(&seq
).count());
53 ]; // combined into one regex in `replace_all`
54 let seq
= replace_all(&seq
, substs
);
56 println
!("\n{}\n{}\n{}", ilen
, clen
, seq
.len());
59 fn replace_all(text
: &str, substs
: Vec
<(u8, &str)>) -> String
{
60 let mut replacements
= vec
![""; 256];
61 let mut alternates
= vec
![];
62 for (re
, replacement
) in substs
{
63 replacements
[re
as usize] = replacement
;
64 alternates
.push((re
as char).to_string());
67 let re
= regex
!(&alternates
.join("|"));
68 let mut new
= String
::with_capacity(text
.len());
69 let mut last_match
= 0;
70 for m
in re
.find_iter(text
) {
71 new
.push_str(&text
[last_match
..m
.start()]);
72 new
.push_str(replacements
[text
.as_bytes()[m
.start()] as usize]);
75 new
.push_str(&text
[last_match
..]);