Skip to content

Commit c6e1f2b

Browse files
committed
Allow building without depending on libstd
1 parent 7cb56f9 commit c6e1f2b

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

.travis.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,6 @@
11
language: rust
2+
3+
script:
4+
- cargo build
5+
- cargo test
6+
- cargo test --no-default-features

Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,9 @@ build = "src/build.rs"
1212
regex = "1.0"
1313

1414
[dependencies]
15-
unicode-normalization = "0.1"
15+
unicode-normalization = { version = "0.1", optional = true }
16+
17+
[features]
18+
default = ["alloc", "normalization"]
19+
alloc = []
20+
normalization = ["unicode-normalization"]

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
# rust-caseless
22

33
Unicode caseless matching
4+
5+
This crate can be used without libstd, when build without the default `alloc`
6+
and `normalization` features.

src/lib.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,31 @@
1+
#![cfg_attr(not(feature = "alloc"), no_std)]
2+
3+
#[cfg(feature = "normalization")]
14
use unicode_normalization::UnicodeNormalization;
25

6+
#[cfg(feature = "normalization")]
37
extern crate unicode_normalization;
48

59
include!(concat!(env!("OUT_DIR"), "/case_folding_data.rs"));
610

711

12+
#[cfg(feature = "normalization")]
813
pub trait Caseless {
914
fn default_case_fold(self) -> CaseFold<Self> where Self: Sized;
1015
fn default_caseless_match<J: Iterator<Item=char>>(self, other: J) -> bool;
1116
fn canonical_caseless_match<J: Iterator<Item=char>>(self, other: J) -> bool;
1217
fn compatibility_caseless_match<J: Iterator<Item=char>>(self, other: J) -> bool;
1318
}
1419

20+
#[cfg(not(feature = "normalization"))]
21+
// This trait is private when the `normalization` feature is disabled. Otherwise, external crates
22+
// implementing the version without the `normalization` methods might get linked into projects that
23+
// do use the `normalization` feature, causing a trait mismatch error.
24+
trait Caseless {
25+
fn default_case_fold(self) -> CaseFold<Self> where Self: Sized;
26+
fn default_caseless_match<J: Iterator<Item=char>>(self, other: J) -> bool;
27+
}
28+
1529
impl<I: Iterator<Item=char>> Caseless for I {
1630
fn default_case_fold(self) -> CaseFold<I> {
1731
CaseFold {
@@ -25,6 +39,7 @@ impl<I: Iterator<Item=char>> Caseless for I {
2539
other.default_case_fold())
2640
}
2741

42+
#[cfg(feature = "normalization")]
2843
fn canonical_caseless_match<J: Iterator<Item=char>>(self, other: J) -> bool {
2944
// FIXME: Inner NFD can be optimized:
3045
// "Normalization is not required before case folding,
@@ -39,6 +54,7 @@ impl<I: Iterator<Item=char>> Caseless for I {
3954
other.nfd().default_case_fold().nfd())
4055
}
4156

57+
#[cfg(feature = "normalization")]
4258
fn compatibility_caseless_match<J: Iterator<Item=char>>(self, other: J) -> bool {
4359
// FIXME: Unclear if the inner NFD can be optimized here like in canonical_caseless_match.
4460
iter_eq(self.nfd().default_case_fold().nfkd().default_case_fold().nfkd(),
@@ -47,6 +63,7 @@ impl<I: Iterator<Item=char>> Caseless for I {
4763

4864
}
4965

66+
#[cfg(feature = "alloc")]
5067
pub fn default_case_fold_str(s: &str) -> String {
5168
s.chars().default_case_fold().collect()
5269
}
@@ -55,10 +72,12 @@ pub fn default_caseless_match_str(a: &str, b: &str) -> bool {
5572
a.chars().default_caseless_match(b.chars())
5673
}
5774

75+
#[cfg(feature = "normalization")]
5876
pub fn canonical_caseless_match_str(a: &str, b: &str) -> bool {
5977
a.chars().canonical_caseless_match(b.chars())
6078
}
6179

80+
#[cfg(feature = "normalization")]
6281
pub fn compatibility_caseless_match_str(a: &str, b: &str) -> bool {
6382
a.chars().compatibility_caseless_match(b.chars())
6483
}
@@ -116,9 +135,10 @@ impl<I> Iterator for CaseFold<I> where I: Iterator<Item = char> {
116135

117136
#[cfg(test)]
118137
mod tests {
119-
use super::default_case_fold_str;
138+
use super::*;
120139

121140
#[test]
141+
#[cfg(feature = "alloc")]
122142
fn test_strs() {
123143
assert_eq!(default_case_fold_str("Test Case"), "test case");
124144
assert_eq!(default_case_fold_str("Teſt Caſe"), "test case");

0 commit comments

Comments
 (0)