Skip to content

core::str::{chars_uppercase,chars_lowercase} iterators #98490

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 1 commit 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
1 change: 1 addition & 0 deletions library/alloc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
#![feature(unicode_internals)]
#![feature(unsize)]
#![feature(std_internals)]
#![feature(unicode_converter)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feature name is too generic, perhaps something more like #![feature(str_chars_casemapped)]?

//
// Language features:
#![feature(allocator_internals)]
Expand Down
1 change: 1 addition & 0 deletions library/alloc/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
#![feature(bench_black_box)]
#![feature(strict_provenance)]
#![feature(once_cell)]
#![feature(unicode_converter)]

use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};
Expand Down
102 changes: 102 additions & 0 deletions library/alloc/tests/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1794,6 +1794,108 @@ fn to_uppercase() {
assert_eq!("aéDžßfiᾀ".to_uppercase(), "AÉDŽSSFIἈΙ");
}

const CHARS_CONVERT_STRINGS: &[&str; 7] = &[
"aBcD",
"ὀδυσσεύς",
"ὈΔΥΣΣΕΎΣ",
"aößü💩στιγμαςDžfiᾀ",
"AÖßÜ💩ΣΤΙΓΜΑΣDžfiİ",
"İİİİİİİİİİİİİİİİİİİİİİİİ",
"i̇i̇i̇i̇i̇i̇i̇i̇i̇i̇i̇i̇i̇i̇i̇i̇i̇i̇i̇i̇i̇i̇i̇i̇",
];

// Run n times .next() then .next_back()
fn chars_fwdback<I: DoubleEndedIterator<Item = char>>(mut iter: I, n: usize) -> String {
let mut buf1 = String::new();
let mut buf2 = String::new();
for _ in 0..n {
if let Some(c) = iter.next() {
buf1.push(c);
} else {
break;
}
}
while let Some(c) = iter.next_back() {
buf2.push(c);
}
for c in buf2.chars().rev() {
buf1.push(c);
}
buf1
}

// Run n times .next_back() then .next()
fn chars_backfwd<I: DoubleEndedIterator<Item = char>>(mut iter: I, n: usize) -> String {
let mut buf1 = String::new();
let mut buf2 = String::new();
for _ in 0..n {
if let Some(c) = iter.next_back() {
buf2.push(c);
} else {
break;
}
}
while let Some(c) = iter.next() {
buf1.push(c);
}
for c in buf2.chars().rev() {
buf1.push(c);
}
buf1
}

#[test]
fn test_chars_uppercase() {
for s in CHARS_CONVERT_STRINGS {
let exp = s.to_uppercase();
assert_eq!(s.chars_uppercase().collect::<String>(), exp);
for i in 0..s.len() {
assert_eq!((i, &chars_fwdback(s.chars_uppercase(), i)), (i, &exp));
assert_eq!((i, &chars_backfwd(s.chars_uppercase(), i)), (i, &exp));
}
}
}

#[test]
fn test_chars_lowercase() {
for s in CHARS_CONVERT_STRINGS {
let exp = s.to_lowercase();
assert_eq!(s.chars_lowercase().collect::<String>(), exp);
for i in 0..s.len() {
assert_eq!((i, &chars_fwdback(s.chars_lowercase(), i)), (i, &exp));
assert_eq!((i, &chars_backfwd(s.chars_lowercase(), i)), (i, &exp));
}
}
}

#[test]
fn test_chars_uppercase_clone_debug() {
let mut iter = "abc".chars_uppercase();
assert_eq!(iter.next(), Some('A'));
assert_eq!(iter.next(), Some('B'));
let mut iterc = iter.clone();
assert_eq!(&format!("{:?}", &iterc), "CharsUppercase(['C'])");
assert_eq!(iter.next(), Some('C'));
assert_eq!(iter.next(), None);
assert_eq!(iterc.clone().last(), Some('C'));
assert_eq!(iterc.next(), Some('C'));
assert_eq!(iterc.next(), None);
}

#[test]
fn test_chars_lowercase_clone_debug() {
let mut iter = "ABC".chars_lowercase();
assert_eq!(iter.next(), Some('a'));
assert_eq!(iter.next(), Some('b'));
let mut iterc = iter.clone();
assert_eq!(&format!("{:?}", &iterc), "CharsLowercase(['c'])");
assert_eq!(iter.next(), Some('c'));
assert_eq!(iter.next(), None);
assert_eq!(iterc.clone().last(), Some('c'));
assert_eq!(iterc.next(), Some('c'));
assert_eq!(iterc.next(), None);
}

#[test]
fn test_into_string() {
// The only way to acquire a Box<str> in the first place is through a String, so just
Expand Down
1 change: 1 addition & 0 deletions library/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
#![feature(const_slice_from_ref)]
#![feature(const_slice_index)]
#![feature(const_is_char_boundary)]
#![feature(unicode_converter)]
//
// Language features:
#![feature(abi_unadjusted)]
Expand Down
Loading