]> git.proxmox.com Git - rustc.git/blob - src/vendor/unicode-normalization/src/lib.rs
New upstream version 1.23.0+dfsg1
[rustc.git] / src / vendor / unicode-normalization / src / lib.rs
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::UnicodeNormalization;
20 //!
21 //! fn main() {
22 //! assert_eq!(compose('A','\u{30a}'), Some('Å'));
23 //!
24 //! let s = "ÅΩ";
25 //! let c = s.nfc().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.1.3"
38 //! ```
39
40 #![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")]
43
44 pub use tables::UNICODE_VERSION;
45 pub use decompose::Decompositions;
46 pub use recompose::Recompositions;
47 use std::str::Chars;
48
49 mod decompose;
50 mod normalize;
51 mod recompose;
52 mod tables;
53
54 #[cfg(test)]
55 mod test;
56 #[cfg(test)]
57 mod testdata;
58
59 /// Methods for composing and decomposing characters.
60 pub mod char {
61 pub use normalize::{decompose_canonical, decompose_compatible, compose};
62
63 /// Look up the canonical combining class of a character.
64 pub use tables::normalization::canonical_combining_class;
65
66 /// Return whether the given character is a combining mark (`General_Category=Mark`)
67 pub use tables::normalization::is_combining_mark;
68 }
69
70
71 /// Methods for iterating over strings while applying Unicode normalizations
72 /// as described in
73 /// [Unicode Standard Annex #15](http://www.unicode.org/reports/tr15/).
74 pub trait UnicodeNormalization<I: Iterator<Item=char>> {
75 /// Returns an iterator over the string in Unicode Normalization Form D
76 /// (canonical decomposition).
77 #[inline]
78 fn nfd(self) -> Decompositions<I>;
79
80 /// Returns an iterator over the string in Unicode Normalization Form KD
81 /// (compatibility decomposition).
82 #[inline]
83 fn nfkd(self) -> Decompositions<I>;
84
85 /// An Iterator over the string in Unicode Normalization Form C
86 /// (canonical decomposition followed by canonical composition).
87 #[inline]
88 fn nfc(self) -> Recompositions<I>;
89
90 /// An Iterator over the string in Unicode Normalization Form KC
91 /// (compatibility decomposition followed by canonical composition).
92 #[inline]
93 fn nfkc(self) -> Recompositions<I>;
94 }
95
96 impl<'a> UnicodeNormalization<Chars<'a>> for &'a str {
97 #[inline]
98 fn nfd(self) -> Decompositions<Chars<'a>> {
99 decompose::new_canonical(self.chars())
100 }
101
102 #[inline]
103 fn nfkd(self) -> Decompositions<Chars<'a>> {
104 decompose::new_compatible(self.chars())
105 }
106
107 #[inline]
108 fn nfc(self) -> Recompositions<Chars<'a>> {
109 recompose::new_canonical(self.chars())
110 }
111
112 #[inline]
113 fn nfkc(self) -> Recompositions<Chars<'a>> {
114 recompose::new_compatible(self.chars())
115 }
116 }
117
118 impl<I: Iterator<Item=char>> UnicodeNormalization<I> for I {
119 #[inline]
120 fn nfd(self) -> Decompositions<I> {
121 decompose::new_canonical(self)
122 }
123
124 #[inline]
125 fn nfkd(self) -> Decompositions<I> {
126 decompose::new_compatible(self)
127 }
128
129 #[inline]
130 fn nfc(self) -> Recompositions<I> {
131 recompose::new_canonical(self)
132 }
133
134 #[inline]
135 fn nfkc(self) -> Recompositions<I> {
136 recompose::new_compatible(self)
137 }
138 }