8e9f8f924c
This commit adds the following impls: impl<T> Deref<[T]> for Vec<T> impl<T> DerefMut<[T]> for Vec<T> impl Deref<str> for String This commit also removes all duplicated inherent methods from vectors and strings as implementations will now silently call through to the slice implementation. Some breakage occurred at std and beneath due to inherent methods removed in favor of those in the slice traits and std doesn't use its own prelude, cc #18424
110 lines
4.0 KiB
Rust
110 lines
4.0 KiB
Rust
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
|
// file at the top-level directory of this distribution and at
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
//
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
// option. This file may not be copied, modified, or distributed
|
|
// except according to those terms.
|
|
|
|
fn check_contains_all_substrings(s: &str) {
|
|
assert!(s.contains(""));
|
|
for i in range(0, s.len()) {
|
|
for j in range(i+1, s.len() + 1) {
|
|
assert!(s.contains(s.slice(i, j)));
|
|
}
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn strslice_issue_16589() {
|
|
assert!("bananas".contains("nana"));
|
|
|
|
// prior to the fix for #16589, x.contains("abcdabcd") returned false
|
|
// test all substrings for good measure
|
|
check_contains_all_substrings("012345678901234567890123456789bcdabcdabcd");
|
|
}
|
|
|
|
#[test]
|
|
fn strslice_issue_16878() {
|
|
assert!(!"1234567ah012345678901ah".contains("hah"));
|
|
assert!(!"00abc01234567890123456789abc".contains("bcabc"));
|
|
}
|
|
|
|
|
|
#[test]
|
|
fn test_strslice_contains() {
|
|
let x = "There are moments, Jeeves, when one asks oneself, 'Do trousers matter?'";
|
|
check_contains_all_substrings(x);
|
|
}
|
|
|
|
#[test]
|
|
fn test_rsplitn_char_iterator() {
|
|
let data = "\nMäry häd ä little lämb\nLittle lämb\n";
|
|
|
|
let mut split: Vec<&str> = data.rsplitn(3, ' ').collect();
|
|
split.reverse();
|
|
assert_eq!(split, vec!["\nMäry häd ä", "little", "lämb\nLittle", "lämb\n"]);
|
|
|
|
let mut split: Vec<&str> = data.rsplitn(3, |c: char| c == ' ').collect();
|
|
split.reverse();
|
|
assert_eq!(split, vec!["\nMäry häd ä", "little", "lämb\nLittle", "lämb\n"]);
|
|
|
|
// Unicode
|
|
let mut split: Vec<&str> = data.rsplitn(3, 'ä').collect();
|
|
split.reverse();
|
|
assert_eq!(split, vec!["\nMäry häd ", " little l", "mb\nLittle l", "mb\n"]);
|
|
|
|
let mut split: Vec<&str> = data.rsplitn(3, |c: char| c == 'ä').collect();
|
|
split.reverse();
|
|
assert_eq!(split, vec!["\nMäry häd ", " little l", "mb\nLittle l", "mb\n"]);
|
|
}
|
|
|
|
#[test]
|
|
fn test_split_char_iterator() {
|
|
let data = "\nMäry häd ä little lämb\nLittle lämb\n";
|
|
|
|
let split: Vec<&str> = data.split(' ').collect();
|
|
assert_eq!( split, vec!["\nMäry", "häd", "ä", "little", "lämb\nLittle", "lämb\n"]);
|
|
|
|
let mut rsplit: Vec<&str> = data.split(' ').rev().collect();
|
|
rsplit.reverse();
|
|
assert_eq!(rsplit, vec!["\nMäry", "häd", "ä", "little", "lämb\nLittle", "lämb\n"]);
|
|
|
|
let split: Vec<&str> = data.split(|c: char| c == ' ').collect();
|
|
assert_eq!( split, vec!["\nMäry", "häd", "ä", "little", "lämb\nLittle", "lämb\n"]);
|
|
|
|
let mut rsplit: Vec<&str> = data.split(|c: char| c == ' ').rev().collect();
|
|
rsplit.reverse();
|
|
assert_eq!(rsplit, vec!["\nMäry", "häd", "ä", "little", "lämb\nLittle", "lämb\n"]);
|
|
|
|
// Unicode
|
|
let split: Vec<&str> = data.split('ä').collect();
|
|
assert_eq!( split, vec!["\nM", "ry h", "d ", " little l", "mb\nLittle l", "mb\n"]);
|
|
|
|
let mut rsplit: Vec<&str> = data.split('ä').rev().collect();
|
|
rsplit.reverse();
|
|
assert_eq!(rsplit, vec!["\nM", "ry h", "d ", " little l", "mb\nLittle l", "mb\n"]);
|
|
|
|
let split: Vec<&str> = data.split(|c: char| c == 'ä').collect();
|
|
assert_eq!( split, vec!["\nM", "ry h", "d ", " little l", "mb\nLittle l", "mb\n"]);
|
|
|
|
let mut rsplit: Vec<&str> = data.split(|c: char| c == 'ä').rev().collect();
|
|
rsplit.reverse();
|
|
assert_eq!(rsplit, vec!["\nM", "ry h", "d ", " little l", "mb\nLittle l", "mb\n"]);
|
|
}
|
|
|
|
#[test]
|
|
fn test_rev_split_char_iterator_no_trailing() {
|
|
let data = "\nMäry häd ä little lämb\nLittle lämb\n";
|
|
|
|
let mut split: Vec<&str> = data.split('\n').rev().collect();
|
|
split.reverse();
|
|
assert_eq!(split, vec!["", "Märy häd ä little lämb", "Little lämb", ""]);
|
|
|
|
let mut split: Vec<&str> = data.split_terminator('\n').rev().collect();
|
|
split.reverse();
|
|
assert_eq!(split, vec!["", "Märy häd ä little lämb", "Little lämb"]);
|
|
}
|