Skip to content

Commit 9d1e619

Browse files
committed
initial commit
0 parents  commit 9d1e619

11 files changed

+259
-0
lines changed

.gitignore

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
.*.swp
2+
tags
3+
target
4+
*.lock
5+
tmp
6+
*.csv
7+
*.fst
8+
*-got
9+
*.csv.idx
10+
words
11+
98m*
12+
dict
13+
test
14+
months

.travis.yml

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
language: rust
2+
rust:
3+
- stable
4+
- beta
5+
- nightly
6+
script:
7+
- cargo build --verbose
8+
- cargo doc
9+
- cargo test --verbose
10+
- if [ "$TRAVIS_RUST_VERSION" = "nightly" ]; then
11+
cargo bench --verbose;
12+
fi

COPYING

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
This project is dual-licensed under the Unlicense and MIT licenses.
2+
3+
You may use this code under the terms of either license.

Cargo.toml

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "rep"
3+
version = "0.1.0" #:version
4+
authors = ["Andrew Gallant <jamslam@gmail.com>"]
5+
description = """
6+
Line oriented search tool using Rust's regex library.
7+
"""
8+
documentation = "https://github.com/BurntSushi/rep"
9+
homepage = "https://github.com/BurntSushi/rep"
10+
repository = "https://github.com/BurntSushi/rep"
11+
readme = "README.md"
12+
keywords = ["regex", "grep", "egrep", "search", "pattern"]
13+
license = "Unlicense/MIT"
14+
15+
[dependencies]
16+
docopt = "0.6"
17+
regex = { version = "0.1", path = "/home/andrew/rust/regex" }
18+
rustc-serialize = "0.3"
19+
20+
[profile.release]
21+
debug = true

LICENSE-MIT

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2015 Andrew Gallant
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in
13+
all copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.

README.md

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
fst
2+
===
3+
This crate provides a fast implementation of ordered sets and maps using finite
4+
state machines. In particular, it makes use of finite state transducers to map
5+
keys to values as the machine is executed. Using finite state machines as data
6+
structures enables us to store keys in a compact format that is also easily
7+
searchable. For example, this crate levages memory maps to make range queries,
8+
regular expression queries and Levenshtein (edit) distance queries very fast.
9+
10+
Check out my blog post
11+
[Index 1,600,000,000 Keys with Automata and
12+
Rust](http://blog.burntsushi.net/transducers/)
13+
for extensive background, examples and experiments.
14+
15+
[![Linux build status](https://api.travis-ci.org/BurntSushi/fst.png)](https://travis-ci.org/BurntSushi/fst)
16+
[![Windows build status](https://ci.appveyor.com/api/projects/status/github/BurntSushi/fst?svg=true)](https://ci.appveyor.com/project/BurntSushi/fst)
17+
[![](http://meritbadge.herokuapp.com/fst)](https://crates.io/crates/fst)
18+
19+
Dual-licensed under MIT or the [UNLICENSE](http://unlicense.org).
20+
21+
22+
### Documentation
23+
24+
[Full API documentation and examples.](http://burntsushi.net/rustdoc/fst/)
25+
26+
27+
### Installation
28+
29+
Simply add a corresponding entry to your `Cargo.toml` dependency list:
30+
31+
```ignore
32+
[dependencies]
33+
fst = "0.1"
34+
```
35+
36+
And add this to your crate root:
37+
38+
```ignore
39+
extern crate fst;
40+
```
41+
42+
43+
### Example
44+
45+
This example demonstrates building a set in memory and executing a fuzzy query
46+
against it. Check out the documentation for a lot more examples!
47+
48+
```rust
49+
use fst::{IntoStreamer, Streamer, Levenshtein, Set};
50+
51+
// A convenient way to create sets in memory.
52+
let keys = vec!["fa", "fo", "fob", "focus", "foo", "food", "foul"];
53+
let set = try!(Set::from_iter(keys));
54+
55+
// Build our fuzzy query.
56+
let lev = try!(Levenshtein::new("foo", 1));
57+
58+
// Apply our fuzzy query to the set we built.
59+
let mut stream = set.search(lev).into_stream();
60+
61+
let keys = try!(stream.into_strs());
62+
assert_eq!(keys, vec!["fo", "fob", "foo", "food"]);
63+
```

UNLICENSE

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
This is free and unencumbered software released into the public domain.
2+
3+
Anyone is free to copy, modify, publish, use, compile, sell, or
4+
distribute this software, either in source code form or as a compiled
5+
binary, for any purpose, commercial or non-commercial, and by any
6+
means.
7+
8+
In jurisdictions that recognize copyright laws, the author or authors
9+
of this software dedicate any and all copyright interest in the
10+
software to the public domain. We make this dedication for the benefit
11+
of the public at large and to the detriment of our heirs and
12+
successors. We intend this dedication to be an overt act of
13+
relinquishment in perpetuity of all present and future rights to this
14+
software under copyright law.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
20+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22+
OTHER DEALINGS IN THE SOFTWARE.
23+
24+
For more information, please refer to <http://unlicense.org/>

appveyor.yml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
environment:
2+
matrix:
3+
- TARGET: x86_64-pc-windows-msvc
4+
- TARGET: i686-pc-windows-gnu
5+
install:
6+
- ps: Start-FileDownload "https://static.rust-lang.org/dist/rust-nightly-${env:TARGET}.exe"
7+
- rust-nightly-%TARGET%.exe /VERYSILENT /NORESTART /DIR="C:\Program Files (x86)\Rust"
8+
- SET PATH=%PATH%;C:\Program Files (x86)\Rust\bin
9+
- SET PATH=%PATH%;C:\MinGW\bin
10+
- rustc -V
11+
- cargo -V
12+
13+
build: false
14+
15+
test_script:
16+
- cargo build --verbose
17+
- cargo test --verbose

ctags.rust

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
--langdef=Rust
2+
--langmap=Rust:.rs
3+
--regex-Rust=/^[ \t]*(#\[[^\]]\][ \t]*)*(pub[ \t]+)?(extern[ \t]+)?("[^"]+"[ \t]+)?(unsafe[ \t]+)?fn[ \t]+([a-zA-Z0-9_]+)/\6/f,functions,function definitions/
4+
--regex-Rust=/^[ \t]*(pub[ \t]+)?type[ \t]+([a-zA-Z0-9_]+)/\2/T,types,type definitions/
5+
--regex-Rust=/^[ \t]*(pub[ \t]+)?enum[ \t]+([a-zA-Z0-9_]+)/\2/g,enum,enumeration names/
6+
--regex-Rust=/^[ \t]*(pub[ \t]+)?struct[ \t]+([a-zA-Z0-9_]+)/\2/s,structure names/
7+
--regex-Rust=/^[ \t]*(pub[ \t]+)?mod[ \t]+([a-zA-Z0-9_]+)/\2/m,modules,module names/
8+
--regex-Rust=/^[ \t]*(pub[ \t]+)?static[ \t]+([a-zA-Z0-9_]+)/\2/c,consts,static constants/
9+
--regex-Rust=/^[ \t]*(pub[ \t]+)?trait[ \t]+([a-zA-Z0-9_]+)/\2/t,traits,traits/
10+
--regex-Rust=/^[ \t]*(pub[ \t]+)?impl([ \t\n]+<.*>)?[ \t]+([a-zA-Z0-9_]+)/\3/i,impls,trait implementations/
11+
--regex-Rust=/^[ \t]*macro_rules![ \t]+([a-zA-Z0-9_]+)/\1/d,macros,macro definitions/

session.vim

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
au BufWritePost *.rs silent!make ctags > /dev/null 2>&1

src/main.rs

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#![allow(dead_code)]
2+
3+
extern crate docopt;
4+
extern crate regex;
5+
extern crate rustc_serialize;
6+
7+
const USAGE: &'static str = "
8+
Usage: rep [options] <pattern> [<file> ...]
9+
";
10+
11+
use std::error::Error;
12+
use std::io::{self, BufRead, Write};
13+
use std::process;
14+
use std::result;
15+
16+
use docopt::Docopt;
17+
use regex::internal::{ExecBuilder, Search};
18+
19+
type Result<T> = result::Result<T, Box<Error + Send + Sync>>;
20+
21+
#[derive(RustcDecodable)]
22+
struct Args {
23+
arg_pattern: String,
24+
arg_file: Vec<String>,
25+
}
26+
27+
fn main() {
28+
let args = Docopt::new(USAGE).and_then(|d| d.decode())
29+
.unwrap_or_else(|e| e.exit());
30+
match run(&args) {
31+
Ok(count) if count == 0 => process::exit(1),
32+
Ok(_) => process::exit(0),
33+
Err(err) => {
34+
let _ = writeln!(&mut io::stderr(), "{}", err);
35+
process::exit(1);
36+
}
37+
}
38+
}
39+
40+
fn run(args: &Args) -> Result<u64> {
41+
let _stdin = io::stdin();
42+
let mut rdr = io::BufReader::new(_stdin.lock());
43+
let mut wtr = io::BufWriter::new(io::stdout());
44+
let mut count = 0;
45+
let mut nline = 0;
46+
let mut line = vec![];
47+
let re = try!(ExecBuilder::new(&args.arg_pattern).only_utf8(false).build());
48+
let mut search = Search {
49+
captures: &mut [],
50+
matches: &mut [false],
51+
};
52+
loop {
53+
line.clear();
54+
let n = try!(rdr.read_until(b'\n', &mut line));
55+
if n == 0 {
56+
break;
57+
}
58+
nline += 1;
59+
if line.last().map_or(false, |&b| b == b'\n') {
60+
line.pop().unwrap();
61+
}
62+
search.matches[0] = false;
63+
if re.exec(&mut search, &line, 0) {
64+
count += 1;
65+
try!(wtr.write(nline.to_string().as_bytes()));
66+
try!(wtr.write(&[b':']));
67+
try!(wtr.write(&line));
68+
try!(wtr.write(&[b'\n']));
69+
}
70+
}
71+
Ok(count)
72+
}

0 commit comments

Comments
 (0)