Skip to content
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

A sorting algorithm library #25

Merged
merged 7 commits into from
Oct 7, 2024
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
chore: API change
`till_needle()` is now on strsplit and not directly on the implementors
of the trait. This allows for Strsplit type to be used in other contexts
also. Also this makes the API more explicit and does not hide the
implementation details.
  • Loading branch information
ishaan26 committed Oct 4, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
commit bbb6d686bac0de4f5a8b2876c3133c2869e21aaa
2 changes: 1 addition & 1 deletion zung_mini/src/lib.rs
Original file line number Diff line number Diff line change
@@ -128,7 +128,7 @@
println!("{:?}", result);
}
StrsplitCommands::Until { needle, string } => {
let result = string.till_needle(needle);
let result = string.strsplit(needle).till_needle();

Check warning on line 131 in zung_mini/src/lib.rs

Codecov / codecov/patch

zung_mini/src/lib.rs#L131

Added line #L131 was not covered by tests
println!("{:?}", result);
}
},
62 changes: 29 additions & 33 deletions zung_mini/src/strsplit/mod.rs
Original file line number Diff line number Diff line change
@@ -66,32 +66,6 @@ where
fn strsplit<P>(&'a self, needle: P) -> Strsplit<'a, P>
where
P: 'b + AsRef<str>;

/// Returns the substring before the first occurrence of the given `needle`
/// without scanning the entire string.
///
/// This function splits the string using the provided `needle` and immediately
/// returns the portion of the string before the first occurrence of the `needle`.
/// It stops searching once the `needle` is found, making it efficient as it
/// avoids iterating over the entire string unnecessarily. If the `needle` is not found,
/// the function returns the entire string.
///
/// # Example
///
/// ```rust
/// use zung_mini::strsplit::StrsplitExt;
/// let text = "hello world";
///
/// let result = text.till_needle(" ");
/// assert_eq!(result, "hello");
/// ```
fn till_needle<P>(&'a self, needle: P) -> &str
where
P: 'b + AsRef<str> + Clone,
{
let mut splitter = self.strsplit(needle.clone());
splitter.next().unwrap()
}
}

impl<'a, 'b> StrsplitExt<'a, 'b> for String
@@ -161,6 +135,28 @@ where
pub fn into_vec(self) -> Vec<&'a str> {
self.collect()
}

/// Returns the substring before the first occurrence of the given `needle`
/// without scanning the entire string.
///
/// This function splits the string using the provided `needle` and immediately
/// returns the portion of the string before the first occurrence of the `needle`.
/// It stops searching once the `needle` is found, making it efficient as it
/// avoids iterating over the entire string unnecessarily. If the `needle` is not found,
/// the function returns the entire string.
///
/// # Example
///
/// ```rust
/// use zung_mini::strsplit::StrsplitExt;
/// let text = "hello world";
///
/// let result = text.strsplit(" ").till_needle();
/// assert_eq!(result, "hello");
/// ```
pub fn till_needle(&mut self) -> &'a str {
self.next().unwrap()
}
}

impl<'a, N> Iterator for Strsplit<'a, N>
@@ -276,49 +272,49 @@ mod tests {
#[test]
fn till_needle_finds_substring() {
let text = "hello world";
let result = text.till_needle(" ");
let result = text.strsplit(" ").till_needle();
assert_eq!(result, "hello");
}

#[test]
fn till_needle_returns_entire_string_if_needle_not_found() {
let text = "hello";
let result = text.till_needle(",");
let result = text.strsplit(" ").till_needle();
assert_eq!(result, "hello");
}

#[test]
fn till_needle_with_multiple_occurrences() {
let text = "apple,banana,orange";
let result = text.till_needle(",");
let result = text.strsplit(",").till_needle();
assert_eq!(result, "apple"); // Stops at first occurrence
}

#[test]
fn till_needle_returns_none_for_empty_string() {
let text = "";
let result = text.till_needle(",");
let result = text.strsplit(" ").till_needle();
assert_eq!(result, "");
}

#[test]
#[should_panic(expected = "Empty needle is not allowed")]
fn till_needle_empty_needle_panics() {
let text = "example";
let _ = text.till_needle(""); // Should panic due to empty needle
let _ = text.strsplit("").till_needle(); // Should panic due to empty needle
}

#[test]
fn till_needle_handles_special_characters() {
let text = "[email protected]";
let result = text.till_needle("@");
let result = text.strsplit("@").till_needle();
assert_eq!(result, "foo");
}

#[test]
fn till_needle_works_with_longer_needle() {
let text = "this is a test string";
let result = text.till_needle("is");
let result = text.strsplit("is").till_needle();
assert_eq!(result, "th");
}
}
Loading