Giter Site home page Giter Site logo

glob-grep's Introduction

A dumb find+grep

This is not a real project. It was an experiment to see how productive a developer is working with various system programming languages. We all know that C is obsolete and should be replaced. And there are many talks why the alternatives are better from the technical perspective. Here I wanted to compare how the languages vary from the UX (well, developer experience perspective).

glob-grep's People

Contributors

zserge avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

glob-grep's Issues

Rust implementation could be more idiomatic

Running example on Rust Playground.

Note: the example above uses two external libraries. This adds some complexity in the event that you want to avoid cargo for some reason.

Summary of changes

Use byte literals instead of casting byte to char.

-            match pattern[p] as char {
-                '*' => {
+            match pattern[p] {
+                b'*' => {

Use an existing path walker instead of rolling our own. Yours works fine, but using an existing function is more in line with i.e. the go implementation.

-     for entry in fs::read_dir(dir)? {
-         let path = entry?.path();
-         if path.is_dir() {
-             walk(&path, &pattern)?;
-             continue;
-         }
+    for entry in WalkDir::new(".") {
+        let entry = entry?;
+        if entry.file_type().is_dir() {
+            continue;
+        }

Use standard convenience combinators:

-            let line = match line {
-                Ok(s) => s,
-                Err(_) => "".to_string(),
-            };
+            let line = line.unwrap_or_default();

Show an approximation of the path instead of an empty string if the path contains non-unicode characters:

-                println!("{}:{}\t{}", path.to_str().unwrap_or(""), lineno, line);
+                println!("{}:{}\t{}", path.display(), lineno, line);

Print any error message to stderr and set an appropriate error code instead of panicing, printing to stdout, etc.

-    match walk(Path::new("."), &argv[1]) {
-        Ok(()) => (),
-        Err(error) => panic!("Error: {:?}", error),
-    };
+    let exit_code = match main_inner() {
+        Ok(_) => 0,
+        Err(err) => {
+            eprintln!("{}", err);
+            1
+        }
+    };
+    std::process::exit(exit_code);

Run each unit test case independently.

-    fn test_glob() {
-        assert!(glob(b"", b""));
-        assert!(glob(b"hello", b"hello"));
+    tests! {
+        empty(b"", b"");
+        literal(b"hello", b"hello");

Try Crystal

I read you post on this project. Very nice and thoughtfully expressed. Given your critiques, I would highly recommend you give Crystal (https://crystal-lang.org/) a try in this regard as well.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.