commit 211c7b7e26a111f71666a95af47bf2b8634d07c3 Author: Austen Adler Date: Sat Jan 22 16:29:02 2022 -0500 Add initial code diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..452354c --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,42 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "aho-corasick" +version = "0.7.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f" +dependencies = [ + "memchr", +] + +[[package]] +name = "memchr" +version = "2.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "308cc39be01b73d0d18f82a0e7b2a3df85245f84af96fdddc5d202d27e47b86a" + +[[package]] +name = "regex" +version = "1.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" +dependencies = [ + "aho-corasick", + "memchr", + "regex-syntax", +] + +[[package]] +name = "regex-syntax" +version = "0.6.25" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b" + +[[package]] +name = "rust-selection-sort" +version = "0.1.0" +dependencies = [ + "regex", +] diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..2d62ccc --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "rust-selection-sort" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +regex = "1" diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..3078dcb --- /dev/null +++ b/src/main.rs @@ -0,0 +1,36 @@ +#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)] + +use regex::Regex; +use std::env; +fn main() { + let args = env::args().collect::>(); + assert!(args.len() < 2, "Usage: rust-selection-sort REGEX SEL1 [SEL2 ...]"); + + let replacement_re = &args[1]; + + let re = Regex::new(replacement_re).unwrap_or_else(|_| panic!( + "Invalid regular expression: {}", + replacement_re + )); + + let mut zipped = args + .iter() + .skip(2) + .zip(args.iter().skip(2).map(|a| { + let captures = re.captures(a)?; + captures.get(1).or_else(|| captures.get(0)).map(|m| m.as_str()) + })) + .collect::)>>(); + + zipped.sort_by(|(a, a_key), (b, b_key)| { + let a = a_key.unwrap_or(a); + let b = b_key.unwrap_or(b); + a.cmp(b) + }); + + for i in &zipped { + print!("{}\0", i.0); + // TODO: Allow debugging with -d + // println!("\n\tSort key: {:?}", i.1); + } +}