Skip to content

Commit 59363bd

Browse files
committed
Add no_std + alloc support
1 parent 0d09a94 commit 59363bd

17 files changed

+1398
-14891
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: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22

33
name = "unicode-normalization"
4-
version = "0.1.12"
4+
version = "0.1.13"
55
authors = ["kwantam <[email protected]>"]
66

77
homepage = "https://github.com/unicode-rs/unicode-normalization"
@@ -18,8 +18,15 @@ Decomposition and Recomposition, as described in
1818
Unicode Standard Annex #15.
1919
"""
2020

21+
edition = "2018"
22+
2123
exclude = [ "target/*", "Cargo.lock", "scripts/tmp", "*.txt", "tests/*" ]
2224

2325
[dependencies.tinyvec]
24-
version = "0.3.2"
26+
version = "0.3.3"
2527
features = ["alloc"]
28+
29+
30+
[features]
31+
default = ["std"]
32+
std = []

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,5 +31,9 @@ to your `Cargo.toml`:
3131

3232
```toml
3333
[dependencies]
34-
unicode-normalization = "0.1.8"
34+
unicode-normalization = "0.1"
3535
```
36+
37+
## `no_std` + `alloc` support
38+
39+
This crate is completely `no_std` + `alloc` compatible. This can be enabled by disabling the `std` feature, i.e. specifying `default-features = false` for this crate on your `Cargo.toml`.

benches/bench.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![feature(test)]
2-
#![feature(iterator_step_by)]
3-
extern crate unicode_normalization;
2+
43
extern crate test;
4+
extern crate unicode_normalization;
55

66
use std::fs;
77
use test::Bencher;

src/__test_api.rs

Lines changed: 6 additions & 1 deletion
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 {
9-
StreamSafe::new(s.chars()).collect()
13+
StreamSafe::new(s.chars()).collect()
1014
}
15+
1116
pub mod quick_check {
1217
pub use crate::quick_check::*;
1318
}

src/decompose.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
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 core::fmt::{self, Write};
11+
use core::iter::Fuse;
12+
use core::ops::Range;
1013
use tinyvec::TinyVec;
11-
use std::fmt::{self, Write};
12-
use std::iter::Fuse;
13-
use std::ops::Range;
1414

1515
#[derive(Clone)]
1616
enum DecompositionType {
@@ -37,7 +37,7 @@ pub struct Decompositions<I> {
3737
}
3838

3939
#[inline]
40-
pub fn new_canonical<I: Iterator<Item=char>>(iter: I) -> Decompositions<I> {
40+
pub fn new_canonical<I: Iterator<Item = char>>(iter: I) -> Decompositions<I> {
4141
Decompositions {
4242
kind: self::DecompositionType::Canonical,
4343
iter: iter.fuse(),
@@ -47,7 +47,7 @@ pub fn new_canonical<I: Iterator<Item=char>>(iter: I) -> Decompositions<I> {
4747
}
4848

4949
#[inline]
50-
pub fn new_compatible<I: Iterator<Item=char>>(iter: I) -> Decompositions<I> {
50+
pub fn new_compatible<I: Iterator<Item = char>>(iter: I) -> Decompositions<I> {
5151
Decompositions {
5252
kind: self::DecompositionType::Compatible,
5353
iter: iter.fuse(),
@@ -99,7 +99,7 @@ impl<I> Decompositions<I> {
9999
}
100100
}
101101

102-
impl<I: Iterator<Item=char>> Iterator for Decompositions<I> {
102+
impl<I: Iterator<Item = char>> Iterator for Decompositions<I> {
103103
type Item = char;
104104

105105
#[inline]
@@ -149,7 +149,7 @@ impl<I: Iterator<Item=char>> Iterator for Decompositions<I> {
149149
}
150150
}
151151

152-
impl<I: Iterator<Item=char> + Clone> fmt::Display for Decompositions<I> {
152+
impl<I: Iterator<Item = char> + Clone> fmt::Display for Decompositions<I> {
153153
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
154154
for c in self.clone() {
155155
f.write_char(c)?;

src/lib.rs

Lines changed: 28 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -38,81 +38,76 @@
3838
//! ```
3939
4040
#![deny(missing_docs, unsafe_code)]
41-
#![doc(html_logo_url = "https://unicode-rs.github.io/unicode-rs_sm.png",
42-
html_favicon_url = "https://unicode-rs.github.io/unicode-rs_sm.png")]
41+
#![doc(
42+
html_logo_url = "https://unicode-rs.github.io/unicode-rs_sm.png",
43+
html_favicon_url = "https://unicode-rs.github.io/unicode-rs_sm.png"
44+
)]
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;
4352

4453
extern crate tinyvec;
4554

46-
pub use tables::UNICODE_VERSION;
47-
pub use decompose::Decompositions;
48-
pub use quick_check::{
55+
pub use crate::decompose::Decompositions;
56+
pub use crate::quick_check::{
57+
is_nfc, is_nfc_quick, is_nfc_stream_safe, is_nfc_stream_safe_quick, is_nfd, is_nfd_quick,
58+
is_nfd_stream_safe, is_nfd_stream_safe_quick, is_nfkc, is_nfkc_quick, is_nfkd, is_nfkd_quick,
4959
IsNormalized,
50-
is_nfc,
51-
is_nfc_quick,
52-
is_nfkc,
53-
is_nfkc_quick,
54-
is_nfc_stream_safe,
55-
is_nfc_stream_safe_quick,
56-
is_nfd,
57-
is_nfd_quick,
58-
is_nfkd,
59-
is_nfkd_quick,
60-
is_nfd_stream_safe,
61-
is_nfd_stream_safe_quick,
6260
};
63-
pub use recompose::Recompositions;
64-
pub use stream_safe::StreamSafe;
65-
use std::str::Chars;
61+
pub use crate::recompose::Recompositions;
62+
pub use crate::stream_safe::StreamSafe;
63+
pub use crate::tables::UNICODE_VERSION;
64+
use core::str::Chars;
65+
66+
mod no_std_prelude;
6667

6768
mod decompose;
6869
mod lookups;
6970
mod normalize;
7071
mod perfect_hash;
71-
mod recompose;
7272
mod quick_check;
73+
mod recompose;
7374
mod stream_safe;
7475
mod tables;
7576

76-
#[cfg(test)]
77-
mod test;
7877
#[doc(hidden)]
7978
pub mod __test_api;
79+
#[cfg(test)]
80+
mod test;
8081

8182
/// Methods for composing and decomposing characters.
8283
pub mod char {
83-
pub use normalize::{decompose_canonical, decompose_compatible, compose};
84+
pub use crate::normalize::{compose, decompose_canonical, decompose_compatible};
8485

85-
pub use lookups::{canonical_combining_class, is_combining_mark};
86+
pub use crate::lookups::{canonical_combining_class, is_combining_mark};
8687
}
8788

88-
8989
/// Methods for iterating over strings while applying Unicode normalizations
9090
/// as described in
9191
/// [Unicode Standard Annex #15](http://www.unicode.org/reports/tr15/).
92-
pub trait UnicodeNormalization<I: Iterator<Item=char>> {
92+
pub trait UnicodeNormalization<I: Iterator<Item = char>> {
9393
/// Returns an iterator over the string in Unicode Normalization Form D
9494
/// (canonical decomposition).
95-
#[inline]
9695
fn nfd(self) -> Decompositions<I>;
9796

9897
/// Returns an iterator over the string in Unicode Normalization Form KD
9998
/// (compatibility decomposition).
100-
#[inline]
10199
fn nfkd(self) -> Decompositions<I>;
102100

103101
/// An Iterator over the string in Unicode Normalization Form C
104102
/// (canonical decomposition followed by canonical composition).
105-
#[inline]
106103
fn nfc(self) -> Recompositions<I>;
107104

108105
/// An Iterator over the string in Unicode Normalization Form KC
109106
/// (compatibility decomposition followed by canonical composition).
110-
#[inline]
111107
fn nfkc(self) -> Recompositions<I>;
112108

113109
/// An Iterator over the string with Conjoining Grapheme Joiner characters
114110
/// inserted according to the Stream-Safe Text Process (UAX15-D4)
115-
#[inline]
116111
fn stream_safe(self) -> StreamSafe<I>;
117112
}
118113

@@ -143,7 +138,7 @@ impl<'a> UnicodeNormalization<Chars<'a>> for &'a str {
143138
}
144139
}
145140

146-
impl<I: Iterator<Item=char>> UnicodeNormalization<I> for I {
141+
impl<I: Iterator<Item = char>> UnicodeNormalization<I> for I {
147142
#[inline]
148143
fn nfd(self) -> Decompositions<I> {
149144
decompose::new_canonical(self)

src/lookups.rs

Lines changed: 51 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,46 +10,81 @@
1010

1111
//! Lookups of unicode properties using minimal perfect hashing.
1212
13-
use perfect_hash::mph_lookup;
14-
use tables::*;
13+
use crate::perfect_hash::mph_lookup;
14+
use crate::tables::*;
1515

1616
/// Look up the canonical combining class for a codepoint.
17-
///
17+
///
1818
/// The value returned is as defined in the Unicode Character Database.
1919
pub fn canonical_combining_class(c: char) -> u8 {
20-
mph_lookup(c.into(), CANONICAL_COMBINING_CLASS_SALT, CANONICAL_COMBINING_CLASS_KV,
21-
u8_lookup_fk, u8_lookup_fv, 0)
20+
mph_lookup(
21+
c.into(),
22+
CANONICAL_COMBINING_CLASS_SALT,
23+
CANONICAL_COMBINING_CLASS_KV,
24+
u8_lookup_fk,
25+
u8_lookup_fv,
26+
0,
27+
)
2228
}
2329

2430
pub(crate) fn composition_table(c1: char, c2: char) -> Option<char> {
2531
if c1 < '\u{10000}' && c2 < '\u{10000}' {
26-
mph_lookup((c1 as u32) << 16 | (c2 as u32),
27-
COMPOSITION_TABLE_SALT, COMPOSITION_TABLE_KV,
28-
pair_lookup_fk, pair_lookup_fv_opt, None)
32+
mph_lookup(
33+
(c1 as u32) << 16 | (c2 as u32),
34+
COMPOSITION_TABLE_SALT,
35+
COMPOSITION_TABLE_KV,
36+
pair_lookup_fk,
37+
pair_lookup_fv_opt,
38+
None,
39+
)
2940
} else {
3041
composition_table_astral(c1, c2)
3142
}
3243
}
3344

3445
pub(crate) fn canonical_fully_decomposed(c: char) -> Option<&'static [char]> {
35-
mph_lookup(c.into(), CANONICAL_DECOMPOSED_SALT, CANONICAL_DECOMPOSED_KV,
36-
pair_lookup_fk, pair_lookup_fv_opt, None)
46+
mph_lookup(
47+
c.into(),
48+
CANONICAL_DECOMPOSED_SALT,
49+
CANONICAL_DECOMPOSED_KV,
50+
pair_lookup_fk,
51+
pair_lookup_fv_opt,
52+
None,
53+
)
3754
}
3855

3956
pub(crate) fn compatibility_fully_decomposed(c: char) -> Option<&'static [char]> {
40-
mph_lookup(c.into(), COMPATIBILITY_DECOMPOSED_SALT, COMPATIBILITY_DECOMPOSED_KV,
41-
pair_lookup_fk, pair_lookup_fv_opt, None)
57+
mph_lookup(
58+
c.into(),
59+
COMPATIBILITY_DECOMPOSED_SALT,
60+
COMPATIBILITY_DECOMPOSED_KV,
61+
pair_lookup_fk,
62+
pair_lookup_fv_opt,
63+
None,
64+
)
4265
}
4366

4467
/// Return whether the given character is a combining mark (`General_Category=Mark`)
4568
pub fn is_combining_mark(c: char) -> bool {
46-
mph_lookup(c.into(), COMBINING_MARK_SALT, COMBINING_MARK_KV,
47-
bool_lookup_fk, bool_lookup_fv, false)
69+
mph_lookup(
70+
c.into(),
71+
COMBINING_MARK_SALT,
72+
COMBINING_MARK_KV,
73+
bool_lookup_fk,
74+
bool_lookup_fv,
75+
false,
76+
)
4877
}
4978

5079
pub fn stream_safe_trailing_nonstarters(c: char) -> usize {
51-
mph_lookup(c.into(), TRAILING_NONSTARTERS_SALT, TRAILING_NONSTARTERS_KV,
52-
u8_lookup_fk, u8_lookup_fv, 0) as usize
80+
mph_lookup(
81+
c.into(),
82+
TRAILING_NONSTARTERS_SALT,
83+
TRAILING_NONSTARTERS_KV,
84+
u8_lookup_fk,
85+
u8_lookup_fv,
86+
0,
87+
) as usize
5388
}
5489

5590
/// Extract the key in a 24 bit key and 8 bit value packed in a u32.

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+
};

0 commit comments

Comments
 (0)