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

Support no_std builds #10

Open
wants to merge 2 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
5 changes: 5 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1 +1,6 @@
language: rust

script:
- cargo build
- cargo test
- cargo test --no-default-features
12 changes: 10 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
[package]
name = "caseless"
version = "0.2.1"
version = "0.2.2"
authors = ["Simon Sapin <[email protected]>"]
description = "Unicode caseless matching"
repository = "https://github.com/SimonSapin/rust-caseless"
license = "MIT"
keywords = ["unicode", "string", "case-insensitive", "normalization", "no_std"]
categories = ["text-processing"]
readme = "README.md"

build = "src/build.rs"

[build-dependencies]
regex = "1.0"

[dependencies]
unicode-normalization = "0.1"
unicode-normalization = { version = "0.1", optional = true }

[features]
default = ["alloc", "normalization"]
alloc = []
normalization = ["unicode-normalization"]
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# rust-caseless

Unicode caseless matching

This crate can be used without libstd, when build without the default `alloc`
and `normalization` features.
22 changes: 21 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
#![cfg_attr(not(feature = "alloc"), no_std)]

#[cfg(feature = "normalization")]
use unicode_normalization::UnicodeNormalization;

#[cfg(feature = "normalization")]
extern crate unicode_normalization;

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


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

#[cfg(not(feature = "normalization"))]
// This trait is private when the `normalization` feature is disabled. Otherwise, external crates
// implementing the version without the `normalization` methods might get linked into projects that
// do use the `normalization` feature, causing a trait mismatch error.
trait Caseless {
fn default_case_fold(self) -> CaseFold<Self> where Self: Sized;
fn default_caseless_match<J: Iterator<Item=char>>(self, other: J) -> bool;
}

impl<I: Iterator<Item=char>> Caseless for I {
fn default_case_fold(self) -> CaseFold<I> {
CaseFold {
Expand All @@ -25,6 +39,7 @@ impl<I: Iterator<Item=char>> Caseless for I {
other.default_case_fold())
}

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

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

}

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

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

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

#[cfg(test)]
mod tests {
use super::default_case_fold_str;
use super::*;

#[test]
#[cfg(feature = "alloc")]
fn test_strs() {
assert_eq!(default_case_fold_str("Test Case"), "test case");
assert_eq!(default_case_fold_str("Teſt Caſe"), "test case");
Expand Down