Skip to content

Commit

Permalink
Try to minimise the String conversion on get_ordering_name. Add some …
Browse files Browse the repository at this point in the history
…more tests just to be sure the changes broke nothing
  • Loading branch information
Hukadan committed Mar 30, 2023
1 parent b247e19 commit 4f43bee
Showing 1 changed file with 25 additions and 8 deletions.
33 changes: 25 additions & 8 deletions src/game.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,19 @@ pub struct Game {
}

impl<'a> Game {
#[allow(dead_code)]
pub fn new() -> Self {
Self::default()
}
fn get_ordering_name(&'a self) -> String {
if let Some(name) = self.name.to_lowercase().strip_prefix("the ") {
return name.to_string();
}
if let Some(name) = self.name.to_lowercase().strip_prefix("a ") {
return name.to_string();
}
self.name.to_lowercase()
let name = self.name.to_lowercase();
let name = if name.starts_with("the ") {
name.strip_prefix("the ").unwrap().to_string()
} else if name.starts_with("a ") {
name.strip_prefix("a ").unwrap().to_string()
} else {
name
};
name
}
}

Expand Down Expand Up @@ -263,6 +264,14 @@ mod game_tests {
assert_eq!(game.get_ordering_name(), "champion");
}
#[test]
fn test_get_ordering_name_with_a_2() {
let mut game = create_game();
game.name = "Achampion".into();
assert_eq!(game.get_ordering_name(), "achampion");
game.name = "achampion".into();
assert_eq!(game.get_ordering_name(), "achampion");
}
#[test]
fn test_get_ordering_name_with_the() {
let mut game = create_game();
game.name = "The champion".into();
Expand All @@ -271,6 +280,14 @@ mod game_tests {
assert_eq!(game.get_ordering_name(), "champion");
}
#[test]
fn test_get_ordering_name_with_the_2() {
let mut game = create_game();
game.name = "Thechampion".into();
assert_eq!(game.get_ordering_name(), "thechampion");
game.name = "thechampion".into();
assert_eq!(game.get_ordering_name(), "thechampion");
}
#[test]
fn test_ordering() {
let mut game1 = create_game();
let mut game2 = create_game();
Expand Down

0 comments on commit 4f43bee

Please sign in to comment.