Skip to content

Commit 8ebdf90

Browse files
committed
Add no_std + alloc support
1 parent 84ca78b commit 8ebdf90

File tree

10 files changed

+44
-10
lines changed

10 files changed

+44
-10
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ sudo: false
77
script:
88
- cargo build --verbose
99
- cargo test --verbose
10+
- cargo test --verbose --no-default-features
1011
- cargo package
1112
- cd target/package/unicode-normalization-*
1213
- cargo test --verbose
14+
- cargo test --verbose --no-default-features
1315
notifications:
1416
email:
1517
on_success: never

Cargo.toml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,8 @@ exclude = [ "target/*", "Cargo.lock", "scripts/tmp", "*.txt", "tests/*" ]
2525
[dependencies.tinyvec]
2626
version = "0.3.3"
2727
features = ["alloc"]
28+
29+
30+
[features]
31+
default = ["std"]
32+
std = []

src/__test_api.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@
44
//
55
// If you're caught using this outside this crates tests/, you get to clean up the mess.
66

7+
#[cfg(not(feature = "std"))]
8+
use crate::no_std_prelude::*;
9+
710
use crate::stream_safe::StreamSafe;
11+
812
pub fn stream_safe(s: &str) -> String {
913
StreamSafe::new(s.chars()).collect()
1014
}
15+
1116
pub mod quick_check {
1217
pub use crate::quick_check::*;
1318
}

src/decompose.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
10-
use std::fmt::{self, Write};
11-
use std::iter::Fuse;
12-
use std::ops::Range;
10+
use core::fmt::{self, Write};
11+
use core::iter::Fuse;
12+
use core::ops::Range;
1313
use tinyvec::TinyVec;
1414

1515
#[derive(Clone)]

src/lib.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@
4242
html_logo_url = "https://unicode-rs.github.io/unicode-rs_sm.png",
4343
html_favicon_url = "https://unicode-rs.github.io/unicode-rs_sm.png"
4444
)]
45+
#![cfg_attr(not(feature = "std"), no_std)]
46+
47+
#[cfg(not(feature = "std"))]
48+
extern crate alloc;
49+
50+
#[cfg(feature = "std")]
51+
extern crate core;
4552

4653
extern crate tinyvec;
4754

@@ -54,7 +61,9 @@ pub use crate::quick_check::{
5461
pub use crate::recompose::Recompositions;
5562
pub use crate::stream_safe::StreamSafe;
5663
pub use crate::tables::UNICODE_VERSION;
57-
use std::str::Chars;
64+
use core::str::Chars;
65+
66+
mod no_std_prelude;
5867

5968
mod decompose;
6069
mod lookups;

src/no_std_prelude.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#[cfg(not(feature = "std"))]
2+
pub use alloc::{
3+
str::Chars,
4+
string::{String, ToString},
5+
vec::Vec,
6+
};

src/normalize.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212
use crate::lookups::{
1313
canonical_fully_decomposed, compatibility_fully_decomposed, composition_table,
1414
};
15-
use std::char;
16-
use std::ops::FnMut;
15+
16+
use core::{char, ops::FnMut};
1717

1818
/// Compute canonical Unicode decomposition for character.
1919
/// See [Unicode Standard Annex #15](http://www.unicode.org/reports/tr15/)

src/recompose.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
use crate::decompose::Decompositions;
12-
use std::fmt::{self, Write};
12+
use core::fmt::{self, Write};
1313
use tinyvec::TinyVec;
1414

1515
#[derive(Clone)]

src/stream_safe.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,11 @@ mod tests {
107107
use super::{classify_nonstarters, StreamSafe};
108108
use crate::lookups::canonical_combining_class;
109109
use crate::normalize::decompose_compatible;
110-
use std::char;
110+
111+
#[cfg(not(feature = "std"))]
112+
use crate::no_std_prelude::*;
113+
114+
use core::char;
111115

112116
fn stream_safe(s: &str) -> String {
113117
StreamSafe::new(s.chars()).collect()
@@ -131,7 +135,7 @@ mod tests {
131135
None => continue,
132136
};
133137
let c = classify_nonstarters(ch);
134-
let mut s = vec![];
138+
let mut s = Vec::new();
135139
decompose_compatible(ch, |c| s.push(c));
136140

137141
assert_eq!(s.len(), c.decomposition_len);

src/test.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010

1111
use super::char::is_combining_mark;
1212
use super::UnicodeNormalization;
13-
use std::char;
13+
use core::char;
14+
15+
#[cfg(not(feature = "std"))]
16+
use crate::no_std_prelude::*;
1417

1518
#[test]
1619
fn test_nfd() {

0 commit comments

Comments
 (0)