Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add experimental feature flagged no_std support #28

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.*.swp
.idea
doc
tags
examples/ss10pusa.csv
Expand Down
10 changes: 8 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ name = "aho_corasick"

[[bin]]
name = "aho-corasick-dot"
path = "src/main.rs"
path = "src/bin/aho-corasick-dot.rs"
test = false
doc = false
bench = false

[dependencies]
memchr = "2"
memchr = { version = "2", default-features = false }

[dev-dependencies]
csv = "1.0.0-beta.5"
Expand All @@ -34,6 +34,12 @@ quickcheck = { version = "0.6", default-features = false }
serde = "1"
serde_derive = "1"

[features]

default = ["std"]
std = ["memchr/use_std", "memchr/libc"]
alloc = []

[[bench]]
name = "bench"
path = "benches/bench.rs"
Expand Down
1 change: 1 addition & 0 deletions ci/script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ fi
cargo test --verbose
if [ "$TRAVIS_RUST_VERSION" = "nightly" ]; then
cargo bench --verbose --no-run
cargo test --verbose --no-default-features --features "alloc"
fi
22 changes: 16 additions & 6 deletions examples/dict-search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,19 @@ extern crate memmap;
extern crate serde;
#[macro_use]
extern crate serde_derive;

use std::error::Error;
#[cfg(feature = "std")]
use std::fs::File;
use std::io::{self, BufRead, Write};
use std::process;

#[cfg(feature = "std")]
use std::io::BufRead;
#[cfg(feature = "std")]
use aho_corasick::{Automaton, AcAutomaton, Match};
use docopt::Docopt;
#[cfg(feature = "std")]
use memmap::Mmap;
use std::error::Error;
use std::io::{self, Write};
use std::process;

use docopt::Docopt;
static USAGE: &'static str = "
Usage: dict-search [options] <input>
dict-search --help
Expand Down Expand Up @@ -58,7 +61,12 @@ fn main() {
}
}
}
#[cfg(not(feature = "std"))]
fn run(_: &Args) -> Result<(), Box<Error>> {
Err("The std feature is mandatory for the dict-search example to work".into())
}

#[cfg(feature = "std")]
fn run(args: &Args) -> Result<(), Box<Error>> {
let aut = try!(build_automaton(&args.flag_dict, args.flag_min_len));
if args.flag_memory_usage {
Expand Down Expand Up @@ -110,6 +118,7 @@ fn run(args: &Args) -> Result<(), Box<Error>> {
Ok(())
}

#[cfg(feature = "std")]
fn write_matches<A, I>(aut: &A, it: I) -> Result<(), Box<Error>>
where A: Automaton<String>, I: Iterator<Item=io::Result<Match>> {
let mut wtr = csv::Writer::from_writer(io::stdout());
Expand All @@ -122,6 +131,7 @@ fn write_matches<A, I>(aut: &A, it: I) -> Result<(), Box<Error>>
Ok(())
}

#[cfg(feature = "std")]
fn build_automaton(
dict_path: &str,
min_len: usize,
Expand Down
19 changes: 17 additions & 2 deletions src/autiter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::io::{self, BufRead};
use std::marker::PhantomData;
#[cfg(feature = "std")]
use core::io::{self, BufRead};
use core::marker::PhantomData;

use memchr::{memchr, memchr2, memchr3};

Expand Down Expand Up @@ -77,6 +78,9 @@ pub trait Automaton<P> {
}

/// Returns an iterator of non-overlapping matches in the given reader.
///
/// Only available when the feature flag "std" is present
#[cfg(feature = "std")]
fn stream_find<'a, R: io::Read>(
&'a self,
rdr: R,
Expand All @@ -92,6 +96,9 @@ pub trait Automaton<P> {
}

/// Returns an iterator of overlapping matches in the given reader.
///
/// Only available when the feature flag "std" is present
#[cfg(feature = "std")]
fn stream_find_overlapping<'a, R: io::Read>(
&'a self,
rdr: R,
Expand Down Expand Up @@ -361,6 +368,9 @@ impl<'a, 's, P, A: Automaton<P> + ?Sized> Iterator for Matches<'a, 's, P, A> {
///
/// `'a` is the lifetime of the automaton, `R` is the type of the underlying
/// `io::Read`er, and P is the type of the Automaton's pattern.
///
/// Only available when the feature flag "std" is present
#[cfg(feature = "std")]
#[derive(Debug)]
pub struct StreamMatches<'a, R, P, A: 'a + Automaton<P> + ?Sized> {
aut: &'a A,
Expand All @@ -370,6 +380,7 @@ pub struct StreamMatches<'a, R, P, A: 'a + Automaton<P> + ?Sized> {
_m: PhantomData<P>,
}

#[cfg(feature = "std")]
impl<'a, R: io::Read, P, A: Automaton<P>>
Iterator for StreamMatches<'a, R, P, A> {
type Item = io::Result<Match>;
Expand Down Expand Up @@ -475,6 +486,9 @@ impl<'a, 's, P, A: Automaton<P> + ?Sized>
///
/// `'a` is the lifetime of the automaton, `R` is the type of the underlying
/// `io::Read`er, and P is the type of the Automaton's pattern.
///
/// Only available when the feature flag "std" is present
#[cfg(feature = "std")]
#[derive(Debug)]
pub struct StreamMatchesOverlapping<'a, R, P, A: 'a + Automaton<P> + ?Sized> {
aut: &'a A,
Expand All @@ -485,6 +499,7 @@ pub struct StreamMatchesOverlapping<'a, R, P, A: 'a + Automaton<P> + ?Sized> {
_m: PhantomData<P>,
}

#[cfg(feature = "std")]
impl<'a, R: io::Read, P, A: Automaton<P> + ?Sized>
Iterator for StreamMatchesOverlapping<'a, R, P, A> {
type Item = io::Result<Match>;
Expand Down
8 changes: 2 additions & 6 deletions src/main.rs → src/bin/aho-corasick-dot.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
extern crate memchr;

extern crate aho_corasick;
use std::env;

use lib::AcAutomaton;

#[allow(dead_code)]
mod lib;
use aho_corasick::AcAutomaton;

fn main() {
let aut = AcAutomaton::new(env::args().skip(1));
Expand Down
8 changes: 6 additions & 2 deletions src/full.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
use std::fmt;
use std::mem;
use core::fmt;
use core::mem;
#[cfg(feature = "std")]
use std::vec::Vec;
#[cfg(all(feature = "alloc", not(feature = "std")))]
use alloc::vec::Vec;

use super::{
FAIL_STATE,
Expand Down
Loading