|
1 |
| -#[test] |
2 |
| -fn it_works() { |
| 1 | +// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT |
| 2 | +// file at the top-level directory of this distribution and at |
| 3 | +// http://rust-lang.org/COPYRIGHT. |
| 4 | +// |
| 5 | +// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
| 7 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 8 | +// option. This file may not be copied, modified, or distributed |
| 9 | +// except according to those terms. |
| 10 | + |
| 11 | +//! Unicode character composition and decomposition utilities |
| 12 | +//! as described in |
| 13 | +//! [Unicode Standard Annex #15](http://www.unicode.org/reports/tr15/). |
| 14 | +//! |
| 15 | +//! ```rust |
| 16 | +//! extern crate unicode_normalization; |
| 17 | +//! |
| 18 | +//! use unicode_normalization::char::compose; |
| 19 | +//! use unicode_normalization::str::UnicodeNormalization; |
| 20 | +//! |
| 21 | +//! fn main() { |
| 22 | +//! assert_eq!(compose('A','\u{30a}'), Some('Å')); |
| 23 | +//! |
| 24 | +//! let s = "ÅΩ"; |
| 25 | +//! let c = UnicodeNormalization::nfc_chars(s).collect::<String>(); |
| 26 | +//! assert_eq!(c, "ÅΩ"); |
| 27 | +//! } |
| 28 | +//! ``` |
| 29 | +//! |
| 30 | +//! # crates.io |
| 31 | +//! |
| 32 | +//! You can use this package in your project by adding the following |
| 33 | +//! to your `Cargo.toml`: |
| 34 | +//! |
| 35 | +//! ```toml |
| 36 | +//! [dependencies] |
| 37 | +//! unicode-normalization = "0.0.1" |
| 38 | +//! ``` |
| 39 | +
|
| 40 | +#![deny(missing_docs, unsafe_code)] |
| 41 | + |
| 42 | +pub use tables::UNICODE_VERSION; |
| 43 | + |
| 44 | +mod decompose; |
| 45 | +mod normalize; |
| 46 | +mod recompose; |
| 47 | +mod tables; |
| 48 | + |
| 49 | +#[cfg(test)] |
| 50 | +mod test; |
| 51 | + |
| 52 | +/// Methods for composing and decomposing characters. |
| 53 | +pub mod char { |
| 54 | + pub use normalize::{decompose_canonical, decompose_compatible, compose}; |
| 55 | + |
| 56 | + /// Look up the canonical combining class of a character. |
| 57 | + pub use tables::normalization::canonical_combining_class; |
| 58 | +} |
| 59 | + |
| 60 | +/// Methods for applying composition and decomposition to strings. |
| 61 | +pub mod str { |
| 62 | + pub use super::decompose::Decompositions; |
| 63 | + pub use super::recompose::Recompositions; |
| 64 | + |
| 65 | + /// Methods for iterating over strings while applying Unicode normalizations |
| 66 | + /// as described in |
| 67 | + /// [Unicode Standard Annex #15](http://www.unicode.org/reports/tr15/). |
| 68 | + pub trait UnicodeNormalization { |
| 69 | + /// Returns an iterator over the string in Unicode Normalization Form D |
| 70 | + /// (canonical decomposition). |
| 71 | + #[inline] |
| 72 | + fn nfd_chars(&self) -> Decompositions; |
| 73 | + |
| 74 | + /// Returns an iterator over the string in Unicode Normalization Form KD |
| 75 | + /// (compatibility decomposition). |
| 76 | + #[inline] |
| 77 | + fn nfkd_chars(&self) -> Decompositions; |
| 78 | + |
| 79 | + /// An Iterator over the string in Unicode Normalization Form C |
| 80 | + /// (canonical decomposition followed by canonical composition). |
| 81 | + #[inline] |
| 82 | + fn nfc_chars(&self) -> Recompositions; |
| 83 | + |
| 84 | + /// An Iterator over the string in Unicode Normalization Form KC |
| 85 | + /// (compatibility decomposition followed by canonical composition). |
| 86 | + #[inline] |
| 87 | + fn nfkc_chars(&self) -> Recompositions; |
| 88 | + } |
| 89 | + |
| 90 | + impl UnicodeNormalization for str { |
| 91 | + #[inline] |
| 92 | + fn nfd_chars(&self) -> Decompositions { |
| 93 | + super::decompose::new_canonical(self) |
| 94 | + } |
| 95 | + |
| 96 | + #[inline] |
| 97 | + fn nfkd_chars(&self) -> Decompositions { |
| 98 | + super::decompose::new_compatible(self) |
| 99 | + } |
| 100 | + |
| 101 | + #[inline] |
| 102 | + fn nfc_chars(&self) -> Recompositions { |
| 103 | + super::recompose::new_canonical(self) |
| 104 | + } |
| 105 | + |
| 106 | + #[inline] |
| 107 | + fn nfkc_chars(&self) -> Recompositions { |
| 108 | + super::recompose::new_compatible(self) |
| 109 | + } |
| 110 | + } |
3 | 111 | }
|
0 commit comments