Skip to content

Commit

Permalink
Support no_std, and don't install cargo-travis for no reason. (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
progval authored and fflorent committed Jun 17, 2018
1 parent b1518af commit f47fe01
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 16 deletions.
30 changes: 21 additions & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,16 @@ addons:
- libdw-dev
- binutils-dev

rust:
- stable
- beta
- nightly

# load travis-cargo
before_script:
- |
pip install 'travis-cargo<0.2' --user &&
export PATH=$HOME/.local/bin:$PATH
- cargo install cargo-travis --force && export PATH=$HOME/.cargo/bin:$PATH;
script:
- |
travis-cargo build &&
travis-cargo test &&
travis-cargo bench &&
cargo build --no-default-features --features="$FEATURES" &&
cargo test --no-default-features --features="$FEATURES" &&
travis-cargo --only stable doc
# Inspired from geal/nom
Expand All @@ -45,3 +38,22 @@ after_success:
env:
global:
- TRAVIS_CARGO_NIGHTLY_FEATURE=""

matrix:
include:
- rust: stable
env: FEATURES='default'
- rust: stable
env: FEATURES=''

- rust: beta
env: FEATURES='default'
- rust: beta
env: FEATURES=''

- rust: nightly
env: FEATURES='default'
- rust: nightly
env: FEATURES='alloc'
- rust: nightly
env: FEATURES=''
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ version = "0.3.0"
repository = "fflorent/nom_locate"

[features]
default = ["std"]
std = ["nom/std", "alloc"]
alloc = ["nom/alloc"]
avx-accel = ["bytecount/avx-accel"]
simd-accel = ["bytecount/simd-accel"]
verbose-errors = ["nom/verbose-errors"]
Expand Down
56 changes: 50 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
//! pub bar: String,
//! }
//!
//! # #[cfg(feature = "alloc")]
//! named!(parse_foobar( Span ) -> Token, do_parse!(
//! take_until!("foo") >>
//! position: position!() >>
Expand All @@ -40,6 +41,7 @@
//! })
//! ));
//!
//! # #[cfg(feature = "alloc")]
//! fn main () {
//! let input = Span::new(CompleteStr("Lorem ipsum \n foobar"));
//! let output = parse_foobar(input);
Expand All @@ -51,23 +53,59 @@
//! });
//! assert_eq!(position.get_column(), 2);
//! }
//! # #[cfg(not(feature = "alloc"))]
//! fn main() {}
//! ````

#![cfg_attr(not(feature = "std"), no_std)]
#![cfg_attr(all(not(feature = "std"), feature = "alloc"), feature(alloc))]

#[cfg(all(not(feature = "std"), feature = "alloc"))]
#[cfg_attr(test, macro_use)]
extern crate alloc;


extern crate bytecount;
extern crate memchr;
extern crate nom;

#[cfg(test)]
mod tests;

use std::iter::{Enumerate, Map};
use std::ops::{Range, RangeFrom, RangeFull, RangeTo};
use std::slice::Iter;
use std::str::{CharIndices, Chars, FromStr};
mod lib {
#[cfg(feature = "std")]
pub mod std {
pub use std::iter::{Enumerate, Map};
pub use std::ops::{Range, RangeFrom, RangeFull, RangeTo};
pub use std::slice::Iter;
pub use std::str::{CharIndices, Chars, FromStr};
pub use std::slice;
pub use std::string::{String, ToString};
pub use std::vec::Vec;
}

#[cfg(not(feature = "std"))]
pub mod std {
pub use core::iter::{Enumerate, Map};
pub use core::ops::{Range, RangeFrom, RangeFull, RangeTo};
pub use core::slice::Iter;
pub use core::str::{CharIndices, Chars, FromStr};
pub use core::slice;
#[cfg(feature = "alloc")]
pub use alloc::string::{String, ToString};
#[cfg(feature = "alloc")]
pub use alloc::vec::Vec;
}
}

use lib::std::*;

use memchr::Memchr;
use nom::{AsBytes, Compare, CompareResult, FindSubstring, FindToken, InputIter, InputLength,
Offset, ParseTo, Slice, InputTake, InputTakeAtPosition, AtEof, ExtendInto,
Offset, ParseTo, Slice, InputTake, InputTakeAtPosition, AtEof,
IResult, ErrorKind, Err, Context};
#[cfg(feature="alloc")]
use nom::ExtendInto;
use nom::types::{CompleteByteSlice, CompleteStr};
use bytecount::{naive_num_chars, num_chars};

Expand Down Expand Up @@ -129,7 +167,7 @@ impl<T: AsBytes> LocatedSpan<T> {
"offset is too big"
);
let orig_input_ptr = self_ptr.offset(-(self.offset as isize));
std::slice::from_raw_parts(orig_input_ptr, self.offset)
slice::from_raw_parts(orig_input_ptr, self.offset)
};

let column = match memchr::memrchr(b'\n', before_self) {
Expand Down Expand Up @@ -554,6 +592,7 @@ impl<T> Offset for LocatedSpan<T> {
}
}

#[cfg(feature="alloc")]
impl<T: ToString> ToString for LocatedSpan<T> {
#[inline]
fn to_string(&self) -> String {
Expand Down Expand Up @@ -601,14 +640,19 @@ macro_rules! impl_extend_into {
)
}

#[cfg(feature="alloc")]
impl_extend_into!(&'a str, char, String);
#[cfg(feature="alloc")]
impl_extend_into!(CompleteStr<'a>, char, String);
#[cfg(feature="alloc")]
impl_extend_into!(&'a [u8], u8, Vec<u8>);
#[cfg(feature="alloc")]
impl_extend_into!(CompleteByteSlice<'a>, u8, Vec<u8>);

#[macro_export]
macro_rules! impl_hex_display {
($fragment_type:ty) => (
#[cfg(feature="alloc")]
impl<'a> nom::HexDisplay for LocatedSpan<$fragment_type> {
fn to_hex(&self, chunk_size: usize) -> String {
self.fragment.to_hex(chunk_size)
Expand Down
23 changes: 22 additions & 1 deletion src/tests.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
mod lib {
#[cfg(feature = "std")]
pub mod std {
pub use std::vec::Vec;
pub use std::string::ToString;
}
#[cfg(all(not(feature = "std"), feature = "alloc"))]
pub mod std {
pub use alloc::vec::Vec;
pub use alloc::string::ToString;
}
}

#[cfg(feature="alloc")]
use lib::std::*;

use super::LocatedSpan;
use nom::{Compare, CompareResult, ErrorKind, FindSubstring,
FindToken, InputIter, InputTake, InputTakeAtPosition,
Offset, ParseTo, Slice};
Offset, Slice};
#[cfg(feature="alloc")]
use nom::ParseTo;

type StrSpan<'a> = LocatedSpan<&'a str>;
type BytesSpan<'a> = LocatedSpan<&'a [u8]>;
Expand Down Expand Up @@ -118,6 +136,7 @@ fn it_should_panic_when_getting_column_if_offset_is_too_big() {
s.get_column();
}

#[cfg(feature = "alloc")]
#[test]
fn it_should_iterate_indices() {
let str_slice = StrSpan::new("foobar");
Expand All @@ -133,6 +152,7 @@ fn it_should_iterate_indices() {
);
}

#[cfg(feature = "alloc")]
#[test]
fn it_should_iterate_elements() {
let str_slice = StrSpan::new("foobar");
Expand Down Expand Up @@ -198,6 +218,7 @@ fn it_should_find_substring() {
assert_eq!(BytesSpan::new(b"foobar").find_substring("baz"), None);
}

#[cfg(feature = "alloc")]
#[test]
fn it_should_parse_to_string() {
assert_eq!(
Expand Down
1 change: 1 addition & 0 deletions tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ fn find_substring<'a>(input: StrSpan<'a>, substr: &str) -> IResult<StrSpan<'a>,
}
}

#[cfg(feature = "alloc")]
#[test]
fn test_escaped_string() {
use nom::Needed; // https://github.com/Geal/nom/issues/780
Expand Down

0 comments on commit f47fe01

Please sign in to comment.