Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion week9/splitlab/src/split_pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ where
/// Returns the next substring of the original `haystack` string, split by some delimiter
/// pattern.
///
/// Panics if the delimiter is empty (length 0).
/// Should panic if the delimiter is empty (length 0).
fn next(&mut self) -> Option<Self::Item> {
todo!("Implement me!")
}
Expand Down
9 changes: 1 addition & 8 deletions week9/splitlab/src/split_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,10 @@ pub struct Split {
impl Split {
/// Creates a new `Split` instance with the given haystack and delimiter.
///
/// Panics if the delimiter is empty (length 0).
/// Should panic if the delimiter is empty (length 0).
///
/// TODO(student): Replace the `'static` lifetimes with other lifetimes!
pub fn new(haystack: &'static str, delimiter: &'static str) -> Self {
assert!(
!delimiter.is_empty(),
"Delimiter must not be an empty string"
);

Self {
remainder: Some(haystack),
Expand All @@ -46,9 +42,6 @@ impl Iterator for Split {
type Item = &'static str;

/// Returns the next substring of the original `haystack` string, split by some delimiter.
///
/// If the delimiter is the empty string, returns the next character (as a string) to avoid
/// infinitely looping.
fn next(&mut self) -> Option<Self::Item> {
todo!("Implement me (make sure to fix the lifetimes!)")
}
Expand Down
6 changes: 6 additions & 0 deletions week9/splitlab/src/tests/pattern.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
use super::*;

#[test]
#[should_panic]
fn test_pattern_empty_delimiter() {
let _ = SplitPattern::new(&String::from("a b c"), "").collect::<Vec<&str>>();
}

test_pattern!(
test_crust_of_rust_until_char_test,
"hello world",
Expand Down
6 changes: 6 additions & 0 deletions week9/splitlab/src/tests/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ fn test_str_compiles() {
assert_eq!(results, vec!["a", "b", "c", "d", "e"]);
}

#[test]
#[should_panic]
fn test_str_empty_delimiter() {
let _ = Split::new(&String::from("a b c"), &String::from(""));
}

test_str!(
test_crust_of_rust_it_works,
"a b c d e",
Expand Down
6 changes: 4 additions & 2 deletions week9/splitlab_ref/src/split_pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ where
/// Returns the next substring of the original `haystack` string, split by some delimiter
/// pattern.
///
/// Panics if the delimiter is empty (length 0).
/// Should panic if the delimiter is empty (length 0).
fn next(&mut self) -> Option<Self::Item> {
// If `remainder` is `None`, then there is nothing left to yield, and we should return
// `None` immediately (with `?`).
Expand All @@ -42,7 +42,9 @@ where
// If there is no delimiter in the remainder, return the entire remainder.
return self.remainder.take();
};
assert_ne!(start, end, "Delimiter cannot be empty");
if start == end {
panic!("Delimiter cannot be empty");
}

// Compute the next string to yield as well as the new remainder.
let next_str = &remainder[..start];
Expand Down
12 changes: 4 additions & 8 deletions week9/splitlab_ref/src/split_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,11 @@ pub struct Split<'haystack, 'delimiter> {
impl<'haystack, 'delimiter> Split<'haystack, 'delimiter> {
/// Creates a new `Split` instance with the given haystack and delimiter.
///
/// Panics if the delimiter is empty (length 0).
/// Should panic if the delimiter is empty (length 0).
pub fn new(haystack: &'haystack str, delimiter: &'delimiter str) -> Self {
assert!(
!delimiter.is_empty(),
"Delimiter must not be an empty string"
);
if delimiter.is_empty() {
panic!("Delimiter cannot be empty")
}

Self {
remainder: Some(haystack),
Expand All @@ -37,8 +36,6 @@ impl<'haystack> Iterator for Split<'haystack, '_> {

/// Returns the next substring of the original `haystack` string, split by some delimiter.
///
/// If the delimiter is the empty string, returns the next character (as a string) to avoid
/// infinitely looping.
fn next(&mut self) -> Option<Self::Item> {
// If `remainder` is `None`, then there is nothing left to yield, and we should return
// `None` immediately (with `?`).
Expand All @@ -51,7 +48,6 @@ impl<'haystack> Iterator for Split<'haystack, '_> {
};

let (start, end) = (index, index + self.delimiter.len());
assert_ne!(start, end, "Delimiter cannot be empty");

// Compute the next string to yield as well as the new remainder.
let next_str = &remainder[..start];
Expand Down
6 changes: 6 additions & 0 deletions week9/splitlab_ref/src/tests/pattern.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
use super::*;

#[test]
#[should_panic]
fn test_pattern_empty_delimiter() {
let _ = SplitPattern::new(&String::from("a b c"), "").collect::<Vec<&str>>();
}

test_pattern!(
test_crust_of_rust_until_char_test,
"hello world",
Expand Down
6 changes: 6 additions & 0 deletions week9/splitlab_ref/src/tests/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ fn test_str_compiles() {
assert_eq!(results, vec!["a", "b", "c", "d", "e"]);
}

#[test]
#[should_panic]
fn test_str_empty_delimiter() {
let _ = Split::new(&String::from("a b c"), &String::from(""));
}

test_str!(
test_crust_of_rust_it_works,
"a b c d e",
Expand Down