Skip to content

std/extra: Two small bugfixes and one simplification #8858

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

Closed
wants to merge 3 commits into from
Closed
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
6 changes: 5 additions & 1 deletion src/libextra/dlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ impl<A: Eq> Eq for DList<A> {
}

fn ne(&self, other: &DList<A>) -> bool {
self.len() != other.len() &&
self.len() != other.len() ||
iterator::order::ne(self.iter(), other.iter())
}
}
Expand Down Expand Up @@ -978,6 +978,10 @@ mod tests {
assert!(n != m);
m.push_back(1);
assert_eq!(&n, &m);

let n = list_from([2,3,4]);
let m = list_from([1,2,3]);
assert!(n != m);
}

#[test]
Expand Down
37 changes: 21 additions & 16 deletions src/libstd/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -621,8 +621,7 @@ enum NormalizationForm {
#[deriving(Clone)]
struct NormalizationIterator<'self> {
priv kind: NormalizationForm,
priv index: uint,
priv string: &'self str,
priv iter: CharIterator<'self>,
priv buffer: ~[(char, u8)],
priv sorted: bool
}
Expand Down Expand Up @@ -650,16 +649,17 @@ impl<'self> Iterator<char> for NormalizationIterator<'self> {
NFKD => char::decompose_compatible
};

while !self.sorted && self.index < self.string.len() {
let CharRange {ch, next} = self.string.char_range_at(self.index);
self.index = next;
do decomposer(ch) |d| {
let class = canonical_combining_class(d);
if class == 0 && !self.sorted {
canonical_sort(self.buffer);
self.sorted = true;
if !self.sorted {
for ch in self.iter {
do decomposer(ch) |d| {
let class = canonical_combining_class(d);
if class == 0 && !self.sorted {
canonical_sort(self.buffer);
self.sorted = true;
}
self.buffer.push((d, class));
}
self.buffer.push((d, class));
if self.sorted { break }
}
}

Expand All @@ -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
Expand Down Expand Up @@ -1628,8 +1631,7 @@ impl<'self> StrSlice<'self> for &'self str {
/// Returns the string in Unicode Normalization Form D (canonical decomposition)
fn nfd_iter(&self) -> NormalizationIterator<'self> {
NormalizationIterator {
index: 0,
string: *self,
iter: self.iter(),
buffer: ~[],
sorted: false,
kind: NFD
Expand All @@ -1639,8 +1641,7 @@ impl<'self> StrSlice<'self> for &'self str {
/// Returns the string in Unicode Normalization Form KD (compatibility decomposition)
fn nfkd_iter(&self) -> NormalizationIterator<'self> {
NormalizationIterator {
index: 0,
string: *self,
iter: self.iter(),
buffer: ~[],
sorted: false,
kind: NFKD
Expand Down Expand Up @@ -1712,6 +1713,7 @@ impl<'self> StrSlice<'self> for &'self str {
if count == end { end_byte = Some(idx); break; }
count += 1;
}
if begin_byte.is_none() && count == begin { begin_byte = Some(self.len()) }
if end_byte.is_none() && count == end { end_byte = Some(self.len()) }

match (begin_byte, end_byte) {
Expand Down Expand Up @@ -2699,8 +2701,11 @@ mod tests {
fn t(a: &str, b: &str, start: uint) {
assert_eq!(a.slice_chars(start, start + b.char_len()), b);
}
t("", "", 0);
t("hello", "llo", 2);
t("hello", "el", 1);
t("αβλ", "β", 1);
t("αβλ", "", 3);
assert_eq!("ะเทศไท", "ประเทศไทย中华Việt Nam".slice_chars(2, 8));
}

Expand Down