Initial commit: add the grep like from chapter 2
authorGreg Burri <greg.burri@gmail.com>
Thu, 1 Jul 2021 11:26:55 +0000 (13:26 +0200)
committerGreg Burri <greg.burri@gmail.com>
Thu, 1 Jul 2021 11:26:55 +0000 (13:26 +0200)
.gitignore [new file with mode: 0644]
ch2-str-simple-pattern/Cargo.lock [new file with mode: 0644]
ch2-str-simple-pattern/Cargo.toml [new file with mode: 0644]
ch2-str-simple-pattern/src/main.rs [new file with mode: 0644]
ch2-str-simple-pattern/text.txt [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..a76ebad
--- /dev/null
@@ -0,0 +1,3 @@
+target/\r
+.vscode/\r
+**/*.rs.bk
\ No newline at end of file
diff --git a/ch2-str-simple-pattern/Cargo.lock b/ch2-str-simple-pattern/Cargo.lock
new file mode 100644 (file)
index 0000000..3a88348
--- /dev/null
@@ -0,0 +1,146 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+[[package]]
+name = "aho-corasick"
+version = "0.7.18"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1e37cfd5e7657ada45f742d6e99ca5788580b5c529dc78faf11ece6dc702656f"
+dependencies = [
+ "memchr",
+]
+
+[[package]]
+name = "ansi_term"
+version = "0.11.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b"
+dependencies = [
+ "winapi",
+]
+
+[[package]]
+name = "atty"
+version = "0.2.14"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8"
+dependencies = [
+ "hermit-abi",
+ "libc",
+ "winapi",
+]
+
+[[package]]
+name = "bitflags"
+version = "1.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
+
+[[package]]
+name = "ch2-str-simple-pattern"
+version = "0.1.0"
+dependencies = [
+ "clap",
+ "regex",
+]
+
+[[package]]
+name = "clap"
+version = "2.33.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002"
+dependencies = [
+ "ansi_term",
+ "atty",
+ "bitflags",
+ "strsim",
+ "textwrap",
+ "unicode-width",
+ "vec_map",
+]
+
+[[package]]
+name = "hermit-abi"
+version = "0.1.19"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33"
+dependencies = [
+ "libc",
+]
+
+[[package]]
+name = "libc"
+version = "0.2.97"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "12b8adadd720df158f4d70dfe7ccc6adb0472d7c55ca83445f6a5ab3e36f8fb6"
+
+[[package]]
+name = "memchr"
+version = "2.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b16bd47d9e329435e309c58469fe0791c2d0d1ba96ec0954152a5ae2b04387dc"
+
+[[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 = "strsim"
+version = "0.8.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
+
+[[package]]
+name = "textwrap"
+version = "0.11.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060"
+dependencies = [
+ "unicode-width",
+]
+
+[[package]]
+name = "unicode-width"
+version = "0.1.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
+
+[[package]]
+name = "vec_map"
+version = "0.8.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
+
+[[package]]
+name = "winapi"
+version = "0.3.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419"
+dependencies = [
+ "winapi-i686-pc-windows-gnu",
+ "winapi-x86_64-pc-windows-gnu",
+]
+
+[[package]]
+name = "winapi-i686-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
+
+[[package]]
+name = "winapi-x86_64-pc-windows-gnu"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
diff --git a/ch2-str-simple-pattern/Cargo.toml b/ch2-str-simple-pattern/Cargo.toml
new file mode 100644 (file)
index 0000000..0ecbd52
--- /dev/null
@@ -0,0 +1,11 @@
+[package]
+name = "ch2-str-simple-pattern"
+version = "0.1.0"
+authors = ["Greg Burri <greg.burri@gmail.com>"]
+edition = "2018"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+regex = "1"
+clap = "2"
\ No newline at end of file
diff --git a/ch2-str-simple-pattern/src/main.rs b/ch2-str-simple-pattern/src/main.rs
new file mode 100644 (file)
index 0000000..b09af0a
--- /dev/null
@@ -0,0 +1,60 @@
+/*
+ * Goals:
+ * - Parse command line arguments.
+ * - Read a file line by line.
+ * - Read stdin
+ * - Use regexp to match a line.
+ */
+
+use std::{ fs::File, io::{ self, BufReader, prelude::* } };
+use regex::Regex;
+use clap::{ App, Arg };
+
+fn main() {
+    let args = App::new("grep-lite")
+        .version("0.1")
+        .about("searches for patterns")
+        .arg(Arg::with_name("pattern")
+            .help("The pattern to search for")
+            .takes_value(true)
+            .required(true)
+        )
+        .arg(Arg::with_name("file")
+            .help("File to search into. Read from stding if not given")
+            .takes_value(true)
+            .required(false)
+        )
+        .get_matches();
+
+    let pattern = args.value_of("pattern").unwrap();
+    let pattern_re = Regex::new(pattern).unwrap();
+
+    match args.value_of("file") {
+        Some(filename) => {
+            let file = File::open(filename).unwrap();
+            let reader = BufReader::new(file);
+            process_lines(reader, pattern_re);
+        },
+        None => {
+            let stdin = io::stdin();
+            let reader = stdin.lock();
+            process_lines(reader, pattern_re);
+        }
+    }
+}
+
+fn process_lines<T: BufRead + Sized>(reader: T, pattern_re: Regex) {
+    for (i, line_result) in reader.lines().enumerate() {
+        match line_result {
+            Ok(line) => {
+                let contains_substring = pattern_re.find(&line);
+                match contains_substring {
+                    Some(_) => println!("Found \"{}\" at line number {}", pattern_re, i + 1),
+                    None => (),
+                }
+            },
+            Err(err) =>
+                eprintln!("Can't read line: {}", err)
+        }
+    }
+}
diff --git a/ch2-str-simple-pattern/text.txt b/ch2-str-simple-pattern/text.txt
new file mode 100644 (file)
index 0000000..48bd08c
--- /dev/null
@@ -0,0 +1,4 @@
+Every face, every shop, bedroom window, public-house, and\r
+dark square is a picture feverishly turned--in search of what?\r
+It is the same with books.\r
+What do we seek through millions of pages?
\ No newline at end of file