8000 Merge pull request #5 from unicode-rs/char_iter_alternative_test · Florob/unicode-normalization@4354885 · GitHub
[go: up one dir, main page]

Skip to content

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 4354885

Browse files
committed
Merge pull request unicode-rs#5 from unicode-rs/char_iter_alternative_test
Minor changes for unicode-rs#4
2 parents 4f6a6ca + 0e7791f commit 4354885

File tree

5 files changed

+80
-165
lines changed

5 files changed

+80
-165
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ as described in
88
extern crate unicode_normalization;
99

1010
use unicode_normalization::char::compose;
11-
use unicode_normalization::str::UnicodeNormalization;
11+
use unicode_normalization::UnicodeNormalization;
1212

1313
fn main() {
1414
assert_eq!(compose('A','\u{30a}'), Some('Å'));
1515

1616
let s = "ÅΩ";
17-
let c = UnicodeNormalization::nfc(s).collect::<String>();
17+
let c = s.nfc().collect::<String>();
1818
assert_eq!(c, "ÅΩ");
1919
}
2020
```

src/lib.rs

Lines changed: 69 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@
1616
//! extern crate unicode_normalization;
1717
//!
1818
//! use unicode_normalization::char::compose;
19-
//! use unicode_normalization::str::UnicodeNormalization;
19+
//! use unicode_normalization::UnicodeNormalization;
2020
//!
2121
//! fn main() {
2222
//! assert_eq!(compose('A','\u{30a}'), Some('Å'));
23-
//!
23+
//!
2424
//! let s = "ÅΩ";
25-
//! let c = UnicodeNormalization::nfc(s).collect::<String>();
25+
//! let c = s.nfc().collect::<String>();
2626
//! assert_eq!(c, "ÅΩ");
2727
//! }
2828
//! ```
@@ -42,6 +42,9 @@
4242
html_favicon_url = "https://unicode-rs.github.io/unicode-rs_sm.png")]
4343

4444
pub use tables::UNICODE_VERSION;
45+
pub use decompose::Decompositions;
46+
pub use recompose::Recompositions;
47+
use std::str::Chars;
4548

4649
mod decompose;
4750
mod normalize;
@@ -61,78 +64,72 @@ pub mod char {
6164
pub use tables::normalization::canonical_combining_class;
6265
}
6366

64-
/// Methods for applying composition and decomposition to strings and char iterators.
65-
pub mod str {
66-
pub use super::decompose::Decompositions;
67-
pub use super::recompose::Recompositions;
68-
use std::str::Chars;
69-
70-
/// Methods for iterating over strings while applying Unicode normalizations
71-
/// as described in
72-
/// [Unicode Standard Annex #15](http://www.unicode.org/reports/tr15/).
73-
pub trait UnicodeNormalization<I: Iterator<Item=char>> {
74-
/// Returns an iterator over the string in Unicode Normalization Form D
75-
/// (canonical decomposition).
76-
#[inline]
77-
fn nfd(self) -> Decompositions<I>;
78-
79-
/// Returns an iterator over the string in Unicode Normalization Form KD
80-
/// (compatibility decomposition).
81-
#[inline]
82-
fn nfkd(self) -> Decompositions<I>;
83-
84-
/// An Iterator over the string in Unicode Normalization Form C
85-
/// (canonical decomposition followed by canonical composition).
86-
#[inline]
87-
fn nfc(self) -> Recompositions<I>;
88-
89-
/// An Iterator over the string in Unicode Normalization Form KC
90-
/// (compatibility decomposition followed by canonical composition).
91-
#[inline]
92-
fn nfkc(self) -> Recompositions<I>;
67+
68+
/// Methods for iterating over strings while applying Unicode normalizations
69+
/// as described in
70+
/// [Unicode Standard Annex #15](http://www.unicode.org/reports/tr15/).
71+
pub trait UnicodeNormalization<I: Iterator<Item=char>> {
72+
/// Returns an iterator over the string in Unicode Normalization Form D
73+
/// (canonical decomposition).
74+
#[inline]
75+
fn nfd(self) -> Decompositions<I>;
76+
77+
/// Returns an iterator over the string in Unicode Normalization Form KD
78+
/// (compatibility decomposition).
79+
#[inline]
80+
fn nfkd(self) -> Decompositions<I>;
81+
82+
/// An Iterator over the string in Unicode Normalization Form C
83+
/// (canonical decomposition followed by canonical composition).
84+
#[inline]
85+
fn nfc(self) -> Recompositions<I>;
86+
87+
/// An Iterator over the string in Unicode Normalization Form KC
88+
/// (compatibility decomposition followed by canonical composition).
89+
#[inline]
90+
fn nfkc(self) -> Recompositions<I>;
91+
}
92+
93+
impl<'a> UnicodeNormalization<Chars<'a>> for &'a str {
94+
#[inline]
95+
fn nfd(self) -> Decompositions<Chars<'a>> {
96+
decompose::new_canonical(self.chars())
97+
}
98+
99+
#[inline]
100+
fn nfkd(self) -> Decompositions<Chars<'a>> {
101+
decompose::new_compatible(self.chars())
102+
}
103+
104+
#[inline]
105+
fn nfc(self) -> Recompositions<Chars<'a>> {
106+
recompose::new_canonical(self.chars())
107+
}
108+
109+
#[inline]
110+
fn nfkc(self) -> Recompositions<Chars<'a>> {
111+
recompose::new_compatible(self.chars())
112+
}
113+
}
114+
115+
impl<I: Iterator<Item=char>> UnicodeNormalization<I> for I {
116+
#[inline]
117+
fn nfd(self) -> Decompositions<I> {
118+
decompose::new_canonical(self)
119+
}
120+
121+
#[inline]
122+
fn nfkd(self) -> Decompositions<I> {
123+
decompose::new_compatible(self)
93124
}
94125

95-
impl<'a> UnicodeNormalization<Chars<'a>> for &'a str {
96-
#[inline]
97-
fn nfd(self) -> Decompositions<Chars<'a>> {
98-
super::decompose::new_canonical(self.chars())
99-
}
100-
101-
#[inline]
102-
fn nfkd(self) -> Decompositions<Chars<'a>> {
103-
super::decompose::new_compatible(self.chars())
104-
}
105-
106-
#[inline]
107-
fn nfc(self) -> Recompositions<Chars<'a>> {
108-
super::recompose::new_canonical(self.chars())
109-
}
110-
111-
#[inline]
112-
fn nfkc(self) -> Recompositions<Chars<'a>> {
113-
super::recompose::new_compatible(self.chars())
114-
}
126+
#[inline]
127+
fn nfc(self) -> Recompositions<I> {
128+
recompose::new_canonical(self)
115129
}
116130

117-
impl<I: Iterator<Item=char>> UnicodeNormalization<I> for I {
118-
#[inline]
119-
fn nfd(self) -> Decompositions<I> {
120-
super::decompose::new_canonical(self)
121-
}
122-
123-
#[inline]
124-
fn nfkd(self) -> Decompositions<I> {
125-
super::decompose::new_compatible(self)
126-
}
127-
128-
#[inline]
129-
fn nfc(self) -> Recompositions<I> {
130-
super::recompose::new_canonical(self)
131-
}
132-
133-
#[inline]
134-
fn nfkc(self) -> Recompositions<I> {
135-
super::recompose::new_compatible(self)
136-
}
131+
#[inline]
132+
fn nfkc(self) -> Recompositions<I> {
133+
recompose::new_compatible(self)
137134
}
138135
}

src/recompose.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
// except according to those terms.
1010

1111
use std::collections::VecDeque;
12-
use super::str::Decompositions;
12+
use decompose::Decompositions;
1313

1414
#[derive(Clone)]
1515
enum RecompositionState {

src/str.rs

Lines changed: 0 additions & 74 deletions
This file was deleted.

src/test.rs

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,15 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11-
use super::str::UnicodeNormalization;
12-
13-
#[test]
14-
fn test_casefold() {
15-
let u = "ABCDEFGH";
16-
let l = "abcdefgh";
17-
assert_eq!(&u.nfd().map(|c| c.to_lowercase().next().unwrap_or(c))
18-
.nfkd().map(|c| c.to_lowercase().next().unwrap_or(c))
19-
.nfkd().collect::<String>()[..],
20-
l);
21-
}
11+
use UnicodeNormalization;
2212

2313
#[test]
2414
fn test_nfd() {
2515
macro_rules! t {
2616
($input: expr, $expected: expr) => {
27-
assert_eq!(UnicodeNormalization::nfd($input).collect::<String>(), $expected);
17+
assert_eq!($input.nfd().collect::<String>(), $expected);
18+
// A dummy iterator that is not std::str::Chars directly:
19+
assert_eq!($input.chars().map(|c| c).nfd().collect::<String>(), $expected);
2820
}
2921
}
3022
t!("abc", "abc");
@@ -43,7 +35,7 @@ fn test_nfd() {
4335
fn test_nfkd() {
4436
macro_rules! t {
4537
($input: expr, $expected: expr) => {
46-
assert_eq!(UnicodeNormalization::nfkd($input).collect::<String>(), $expected);
38+
assert_eq!($input.nfkd().collect::<String>(), $expected);
4739
}
4840
}
4941
t!("abc", "abc");
@@ -62,7 +54,7 @@ fn test_nfkd() {
6254
fn test_nfc() {
6355
macro_rules! t {
6456
($input: expr, $expected: expr) => {
65-
assert_eq!(UnicodeNormalization::nfc($input).collect::<String>(), $expected);
57+
assert_eq!($input.nfc().collect::<String>(), $expected);
6658
}
6759
}
6860
t!("abc", "abc");
@@ -82,7 +74,7 @@ fn test_nfc() {
8274
fn test_nfkc() {
8375
macro_rules! t {
8476
($input: expr, $expected: expr) => {
85-
assert_eq!(UnicodeNormalization::nfkc($input).collect::<String>(), $expected);
77+
assert_eq!($input.nfkc().collect::<String>(), $expected);
8678
}
8779
}
8880
t!("abc", "abc");
@@ -102,7 +94,7 @@ fn test_nfkc() {
10294
fn test_official() {
10395
use testdata::TEST_NORM;
10496
macro_rules! normString {
105-
($fun: ident, $input: expr) => { UnicodeNormalization::$fun($input).collect::<String>() }
97+
($method: ident, $input: expr) => { $input.$method().collect::<String>() }
10698
}
10799

108100
for &(s1, s2, s3, s4, s5) in TEST_NORM {

0 commit comments

Comments
 (0)
0