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 no_std + alloc support by way of a default "std" feature flag #8

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,13 @@ license = "Unlicense/MIT"
[badges]
travis-ci = { repository = "BurntSushi/utf8-ranges" }

[features]
default = ["std"]
# When disabled, allows use in `no_std` builds with access to a heap by way of alloc
std = []
# Required for use in `no_std` builds, and mutually exclusive with "std" feature
alloc = []


[dev-dependencies]
quickcheck = "0.6"
7 changes: 7 additions & 0 deletions ci/script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,11 @@ if [ "$TRAVIS_RUST_VERSION" = "1.12.0" ]; then
exit
fi

# Run tests.
cargo test --verbose

# If we have nightly, test no_std mode by removing.
# the default feature, "std".
if [ "$TRAVIS_RUST_VERSION" = "nightly" ]; then
cargo test --lib --verbose --no-default-features --features "alloc"
fi
50 changes: 44 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,15 +85,52 @@ Russ Cox got it from Ken Thompson's `grep` (no source, folk lore?).
I also got the idea from
[Lucene](https://github.com/apache/lucene-solr/blob/ae93f4e7ac6a3908046391de35d4f50a0d3c59ca/lucene/core/src/java/org/apache/lucene/util/automaton/UTF32ToUTF8.java),
which uses it for executing automata on their term index.

# Use in no_std environments

This library can function in `#![no_std]` environments that have access to
a heap allocator. To do so, disable the library's default features
(to remove the on-by-default "std" feature) and add the "alloc" feature.

Presently using a nightly compiler toolchain is required for this option.

```toml
[dependencies.utf8-ranges]
version = "1"
default-features = false
features = ["alloc"]
```

*/

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

#![deny(missing_docs)]

#[cfg(test)] extern crate quickcheck;

use std::char;
use std::fmt;
use std::slice;
#[cfg(test)]
#[macro_use]
extern crate std as std_test;

#[cfg(feature = "std")]
#[macro_use]
extern crate std as core;

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

use core::char;
use core::fmt;
use core::slice;

#[cfg(feature = "std")]
use core::prelude::v1::*;
#[cfg(all(feature = "alloc", not(feature = "std")))]
use alloc::Vec;


use char_utf8::encode_utf8;

Expand Down Expand Up @@ -290,7 +327,7 @@ impl Utf8Sequences {
/// Create a new iterator over UTF-8 byte ranges for the scalar value range
/// given.
pub fn new(start: char, end: char) -> Self {
let mut it = Utf8Sequences { range_stack: vec![] };
let mut it = Utf8Sequences { range_stack: Vec::new() };
it.push(start as u32, end as u32);
it
}
Expand Down Expand Up @@ -440,7 +477,8 @@ fn max_scalar_value(nbytes: usize) -> u32 {

#[cfg(test)]
mod tests {
use std::char;
use core::char;
use std_test::vec::Vec;

use quickcheck::{TestResult, quickcheck};

Expand All @@ -454,7 +492,7 @@ mod tests {
fn never_accepts_surrogate_codepoints(start: char, end: char) {
let mut buf = [0; MAX_UTF8_BYTES];
for cp in 0xD800..0xE000 {
let c = unsafe { ::std::mem::transmute(cp) };
let c = unsafe { ::core::mem::transmute(cp) };
let n = encode_utf8(c, &mut buf).unwrap();
for r in Utf8Sequences::new(start, end) {
if r.matches(&buf[0..n]) {
Expand Down