1
Fork 0

std::str: Use CharIterator in NormalizationIterator

Just to simplify and not have the iteration logic repeated in multiple places.
This commit is contained in:
blake2-ppc 2013-08-29 17:11:11 +02:00
parent 518bd073b4
commit d8801ceabc

View file

@ -621,8 +621,7 @@ enum NormalizationForm {
#[deriving(Clone)] #[deriving(Clone)]
struct NormalizationIterator<'self> { struct NormalizationIterator<'self> {
priv kind: NormalizationForm, priv kind: NormalizationForm,
priv index: uint, priv iter: CharIterator<'self>,
priv string: &'self str,
priv buffer: ~[(char, u8)], priv buffer: ~[(char, u8)],
priv sorted: bool priv sorted: bool
} }
@ -650,16 +649,17 @@ impl<'self> Iterator<char> for NormalizationIterator<'self> {
NFKD => char::decompose_compatible NFKD => char::decompose_compatible
}; };
while !self.sorted && self.index < self.string.len() { if !self.sorted {
let CharRange {ch, next} = self.string.char_range_at(self.index); for ch in self.iter {
self.index = next; do decomposer(ch) |d| {
do decomposer(ch) |d| { let class = canonical_combining_class(d);
let class = canonical_combining_class(d); if class == 0 && !self.sorted {
if class == 0 && !self.sorted { canonical_sort(self.buffer);
canonical_sort(self.buffer); self.sorted = true;
self.sorted = true; }
self.buffer.push((d, class));
} }
self.buffer.push((d, class)); if self.sorted { break }
} }
} }
@ -678,7 +678,10 @@ impl<'self> Iterator<char> for NormalizationIterator<'self> {
} }
} }
fn size_hint(&self) -> (uint, Option<uint>) { (self.string.len(), None) } fn size_hint(&self) -> (uint, Option<uint>) {
let (lower, _) = self.iter.size_hint();
(lower, None)
}
} }
/// Replace all occurrences of one string with another /// Replace all occurrences of one string with another
@ -1628,8 +1631,7 @@ impl<'self> StrSlice<'self> for &'self str {
/// Returns the string in Unicode Normalization Form D (canonical decomposition) /// Returns the string in Unicode Normalization Form D (canonical decomposition)
fn nfd_iter(&self) -> NormalizationIterator<'self> { fn nfd_iter(&self) -> NormalizationIterator<'self> {
NormalizationIterator { NormalizationIterator {
index: 0, iter: self.iter(),
string: *self,
buffer: ~[], buffer: ~[],
sorted: false, sorted: false,
kind: NFD kind: NFD
@ -1639,8 +1641,7 @@ impl<'self> StrSlice<'self> for &'self str {
/// Returns the string in Unicode Normalization Form KD (compatibility decomposition) /// Returns the string in Unicode Normalization Form KD (compatibility decomposition)
fn nfkd_iter(&self) -> NormalizationIterator<'self> { fn nfkd_iter(&self) -> NormalizationIterator<'self> {
NormalizationIterator { NormalizationIterator {
index: 0, iter: self.iter(),
string: *self,
buffer: ~[], buffer: ~[],
sorted: false, sorted: false,
kind: NFKD kind: NFKD